test code is success!
This commit is contained in:
parent
f825294e79
commit
12f2a596bb
|
@ -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);
|
||||
|
|
|
@ -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<Void> 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();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
29
src/test/java/ocean/gateway/filters/TokenFilterTests.java
Normal file
29
src/test/java/ocean/gateway/filters/TokenFilterTests.java
Normal file
|
@ -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<String> 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");
|
||||
}
|
||||
}
|
|
@ -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() {
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user