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.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
......
...@@ -7,7 +7,8 @@ import org.springframework.web.bind.annotation.*; ...@@ -7,7 +7,8 @@ import org.springframework.web.bind.annotation.*;
public class FirstController { public class FirstController {
@GetMapping("/message") @GetMapping("/message")
public String test() { public String test(@RequestHeader("first-request") String header) {
return "Hello JavaInUse Called in First Service"; System.out.println(header);
return "Hello Java Called in First Service";
} }
} }
...@@ -13,7 +13,11 @@ repositories { ...@@ -13,7 +13,11 @@ repositories {
} }
dependencies { 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' compileOnly 'org.projectlombok:lombok'
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-gateway', version: '2.1.5.RELEASE' compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-gateway', version: '2.1.5.RELEASE'
annotationProcessor 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok'
......
package com.example.gatewayServiceUsingJava; package com.example;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; 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 @SpringBootApplication
public class GatewayServiceUsingJavaApplication { public class GatewayServiceUsingJavaApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(GatewayServiceUsingJavaApplication.class, 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 { ...@@ -12,10 +12,14 @@ public class SpringCloudConfig {
public RouteLocator gatewayRoutes(RouteLocatorBuilder builder) { public RouteLocator gatewayRoutes(RouteLocatorBuilder builder) {
return builder.routes() return builder.routes()
.route(r -> r.path("/employee/**") .route(r -> r.path("/employee/**")
.filters(f -> f.addRequestHeader("first-request", "first-request-header")
.addResponseHeader("first-response", "first-response-header"))
.uri("http://localhost:8081/") .uri("http://localhost:8081/")
.id("employeeModule")) .id("employeeModule"))
.route(r -> r.path("/consumer/**") .route(r -> r.path("/consumer/**")
.filters(f -> f.addRequestHeader("second-request", "second-request-header")
.addResponseHeader("second-response", "second-response-header"))
.uri("http://localhost:8082/") .uri("http://localhost:8082/")
.id("consumerModule")) .id("consumerModule"))
.build(); .build();
......
...@@ -13,7 +13,10 @@ repositories { ...@@ -13,7 +13,10 @@ repositories {
} }
dependencies { 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' compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok'
testImplementation('org.springframework.boot:spring-boot-starter-test') { testImplementation('org.springframework.boot:spring-boot-starter-test') {
......
package com.example.gatewayServiceUsingProperties; package com.example;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
......
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: ...@@ -4,6 +4,8 @@ server:
spring: spring:
cloud: cloud:
gateway: gateway:
default-filters:
- name: CustomFilter
routes: routes:
- id: employeeModule - id: employeeModule
uri: http://localhost:8081/ uri: http://localhost:8081/
......
package com.example.secondService; package com.example;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
...@@ -7,6 +7,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; ...@@ -7,6 +7,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
public class SecondServiceApplication { public class SecondServiceApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(SecondServiceApplication.class, args); SpringApplication.run(SecondServiceApplication.class, args);
} }
......
package com.example.controller; package com.example.controller;
import org.springframework.web.bind.annotation.GetMapping; 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.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
...@@ -9,8 +10,9 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -9,8 +10,9 @@ import org.springframework.web.bind.annotation.RestController;
public class SecondController { public class SecondController {
@GetMapping("/message") @GetMapping("/message")
public String test(){ public String test(@RequestHeader("second-request") String header) {
return "Hello JavaInUse Called in Second Service"; 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