From 12f2a596bb4768c1eb065b49505243dcc2907a26 Mon Sep 17 00:00:00 2001 From: huangsimin Date: Fri, 31 May 2019 16:45:29 +0800 Subject: [PATCH] test code is success! --- .../gateway/service/ServiceApplication.java | 3 ++ .../gateway/service/filters/TokenFilter.java | 27 +++++++++++++++-- .../gateway/service/routes/Business.java | 2 +- .../gateway/filters/TokenFilterTests.java | 29 +++++++++++++++++++ .../service/ServiceApplicationTests.java | 13 ++------- 5 files changed, 61 insertions(+), 13 deletions(-) create mode 100644 src/test/java/ocean/gateway/filters/TokenFilterTests.java diff --git a/src/main/java/ocean/gateway/service/ServiceApplication.java b/src/main/java/ocean/gateway/service/ServiceApplication.java index fcb87cf..21f2763 100644 --- a/src/main/java/ocean/gateway/service/ServiceApplication.java +++ b/src/main/java/ocean/gateway/service/ServiceApplication.java @@ -1,12 +1,15 @@ package ocean.gateway.service; import org.springframework.boot.SpringApplication; +import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.ComponentScans; @SpringBootApplication(scanBasePackages = {"ocean.gateway.service.*"}) @ComponentScan +@SpringBootConfiguration public class ServiceApplication { public static void main(String[] args) { SpringApplication.run(ServiceApplication.class, args); diff --git a/src/main/java/ocean/gateway/service/filters/TokenFilter.java b/src/main/java/ocean/gateway/service/filters/TokenFilter.java index 371d8df..3f75eb4 100644 --- a/src/main/java/ocean/gateway/service/filters/TokenFilter.java +++ b/src/main/java/ocean/gateway/service/filters/TokenFilter.java @@ -4,14 +4,21 @@ import org.springframework.boot.SpringBootConfiguration; import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.core.annotation.Order; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.server.reactive.ServerHttpRequest; +import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.stereotype.Component; import org.springframework.web.server.ServerWebExchange; +import lombok.extern.slf4j.Slf4j; import reactor.core.publisher.Mono; +@Slf4j @Component -@Order(0) +@SpringBootConfiguration +@Order(-200) public class TokenFilter implements GlobalFilter { public TokenFilter() { @@ -19,7 +26,23 @@ public class TokenFilter implements GlobalFilter { @Override public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { - return null; + ServerHttpRequest request = exchange.getRequest(); + HttpHeaders header = request.getHeaders(); + String token = header.getFirst("token"); + + if (token != null) { + // + + return chain.filter(exchange); + } else { + log.warn("request token is empty: {}", request.getURI()); + } + + ServerHttpResponse response = exchange.getResponse(); + if (!response.setStatusCode(HttpStatus.UNAUTHORIZED)) { + log.error("if the status code has not been set because the HTTP response is already committed"); + } + return response.setComplete(); } diff --git a/src/main/java/ocean/gateway/service/routes/Business.java b/src/main/java/ocean/gateway/service/routes/Business.java index 26d375e..1b6aa94 100644 --- a/src/main/java/ocean/gateway/service/routes/Business.java +++ b/src/main/java/ocean/gateway/service/routes/Business.java @@ -13,7 +13,7 @@ import lombok.extern.slf4j.Slf4j; public class Business { @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { - log.warn("path_route"); + log.trace("path_route"); return builder.routes().route("path_route", r -> r.path("/hello").uri("http://localhost:3030/hello")).build(); } diff --git a/src/test/java/ocean/gateway/filters/TokenFilterTests.java b/src/test/java/ocean/gateway/filters/TokenFilterTests.java new file mode 100644 index 0000000..3067fd2 --- /dev/null +++ b/src/test/java/ocean/gateway/filters/TokenFilterTests.java @@ -0,0 +1,29 @@ +package ocean.gateway.filters; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.util.Assert; + +import ocean.gateway.service.ServiceApplication; + + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = {ServiceApplication.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class TokenFilterTests { + + @Autowired + private TestRestTemplate restTemplate ; + + @Test + public void GetHello() { + ResponseEntity response = restTemplate.getForEntity("http://127.0.0.1:8080/hello", String.class); + Assert.notNull(response, "The class must not be null"); + Assert.state(response.getStatusCode() == HttpStatus.UNAUTHORIZED, "response.getStatusCode() must be HttpStatus.UNAUTHORIZED"); + } +} \ No newline at end of file diff --git a/src/test/java/ocean/gateway/service/ServiceApplicationTests.java b/src/test/java/ocean/gateway/service/ServiceApplicationTests.java index 610fefa..061d0ba 100644 --- a/src/test/java/ocean/gateway/service/ServiceApplicationTests.java +++ b/src/test/java/ocean/gateway/service/ServiceApplicationTests.java @@ -1,16 +1,9 @@ package ocean.gateway.service; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; - -@RunWith(SpringRunner.class) -@SpringBootTest +// @RunWith(SpringRunner.class) +// @SpringBootTest(classes = {SpringApplication.class}) public class ServiceApplicationTests { - @Test - public void contextLoads() { - } + }