Commit 3e8b6767 authored by Divisha Agarwal's avatar Divisha Agarwal

added filters

parent 49c06e09
package com.example.firstService;
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
......
......@@ -7,7 +7,8 @@ import org.springframework.web.bind.annotation.*;
public class FirstController {
@GetMapping("/message")
public String test() {
return "Hello JavaInUse Called in First Service";
public String test(@RequestHeader("first-request") String header) {
System.out.println(header);
return "Hello Java Called in First Service";
}
}
......@@ -13,7 +13,11 @@ repositories {
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
//implementation 'org.springframework.boot:spring-boot-starter-web'
compile group: 'org.springframework.boot', name: 'spring-boot-starter', version: '2.2.8.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-validation', version: '2.2.8.RELEASE'
runtime group: 'org.springframework.cloud', name: 'spring-cloud-dependencies', version: 'Greenwich.SR2', ext: 'pom'
compileOnly 'org.projectlombok:lombok'
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-gateway', version: '2.1.5.RELEASE'
annotationProcessor 'org.projectlombok:lombok'
......
package com.example.gatewayServiceUsingJava;
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import reactor.core.publisher.Mono;
@SpringBootApplication
public class GatewayServiceUsingJavaApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayServiceUsingJavaApplication.class, args);
}
@Bean
public GlobalFilter globalFilter() {
return (exchange, chain) -> {
System.out.println("First Global filter");
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
System.out.println("Second Global filter");
}));
};
}
}
......@@ -12,10 +12,14 @@ public class SpringCloudConfig {
public RouteLocator gatewayRoutes(RouteLocatorBuilder builder) {
return builder.routes()
.route(r -> r.path("/employee/**")
.filters(f -> f.addRequestHeader("first-request", "first-request-header")
.addResponseHeader("first-response", "first-response-header"))
.uri("http://localhost:8081/")
.id("employeeModule"))
.route(r -> r.path("/consumer/**")
.filters(f -> f.addRequestHeader("second-request", "second-request-header")
.addResponseHeader("second-response", "second-response-header"))
.uri("http://localhost:8082/")
.id("consumerModule"))
.build();
......
......@@ -13,7 +13,10 @@ repositories {
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
compile group: 'org.springframework.boot', name: 'spring-boot-starter', version: '2.2.8.RELEASE'
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-gateway', version: '2.1.5.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-validation', version: '2.2.8.RELEASE'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
......
package com.example.config;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;
@Component
public class CustomFilter extends AbstractGatewayFilterFactory<CustomFilter.Config> {
public CustomFilter() {
super(Config.class);
}
@Override
public GatewayFilter apply(Config config) {
//Custom Pre Filter. Suppose we can extract JWT and perform Authentication
return (exchange, chain) -> {
System.out.println("First pre filter" + exchange.getRequest());
//Custom Post Filter.Suppose we can call error response handler based on error code.
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
System.out.println("First post filter");
}));
};
}
public static class Config {
// Put the configuration properties
}
}
......@@ -4,6 +4,8 @@ server:
spring:
cloud:
gateway:
default-filters:
- name: CustomFilter
routes:
- id: employeeModule
uri: http://localhost:8081/
......@@ -12,4 +14,4 @@ spring:
- id: consumerModule
uri: http://localhost:8082/
predicates:
- Path=/consumer/**
\ No newline at end of file
- Path=/consumer/**
package com.example.secondService;
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
......@@ -7,6 +7,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
public class SecondServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SecondServiceApplication.class, args);
}
......
package com.example.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
......@@ -9,8 +10,9 @@ import org.springframework.web.bind.annotation.RestController;
public class SecondController {
@GetMapping("/message")
public String test(){
return "Hello JavaInUse Called in Second Service";
public String test(@RequestHeader("second-request") String header) {
System.out.println(header);
return "Hello Java Called in Second Service";
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment