Browse Source

Merge branch '2.1.x'

pull/1489/head
Spencer Gibb 5 years ago
parent
commit
4ad04e723b
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 154
      spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/HystrixGatewayFilterFactoryPrincipalTests.java
  2. 26
      spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/BaseWebClientTests.java

154
spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/HystrixGatewayFilterFactoryPrincipalTests.java

@ -1,154 +0,0 @@
/*
* Copyright 2013-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.gateway.filter.factory;
import java.security.Principal;
import java.util.Collections;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.cloud.gateway.filter.ratelimit.PrincipalNameKeyResolver;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.cloud.gateway.test.BaseWebClientTests;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.core.userdetails.MapReactiveUserDetailsService;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.server.SecurityWebFilterChain;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT, properties = "debug=true")
@DirtiesContext
public class HystrixGatewayFilterFactoryPrincipalTests extends BaseWebClientTests {
@Autowired
private TestPrincipalGatewayFilterFactory testFilterFactory;
@Test
public void hystrixPrincipalNotLost() {
testClient.get().uri("/hystrixprincipal").headers(httpHeaders -> {
httpHeaders.setBasicAuth("user", "password");
httpHeaders.set("Host", "www.hystrixsecurity.org");
}).exchange().expectStatus().isOk().expectBody().jsonPath("$.principal")
.isEqualTo("user");
assertThat(testFilterFactory.resolvedPrincipal).isEqualTo("user");
}
@RestController
@SpringBootConfiguration
@EnableAutoConfiguration
@RibbonClients({
@RibbonClient(name = "testservice", configuration = TestRibbonConfig.class) })
public static class TestConfig {
@Value("${test.uri}")
private String uri;
@RequestMapping("/httpbin/hystrixprincipal")
public Mono<Map<String, String>> hystrixPrincipal(Mono<Principal> principal) {
return principal.map(Principal::getName).defaultIfEmpty("Unknown")
.map(s -> Collections.singletonMap("principal", s));
}
@Bean
public RouteLocator hystrixRouteLocator(RouteLocatorBuilder builder,
TestPrincipalGatewayFilterFactory filterFactory) {
return builder.routes()
.route("hystrix_security", r -> r.host("**.hystrixsecurity.org")
.filters(f -> f.prefixPath("/httpbin")
.hystrix(config -> config.setName("securitycmd"))
.filter(filterFactory.apply("")))
.uri(uri))
.build();
}
@Bean
public TestPrincipalGatewayFilterFactory testPrincipalGatewayFilterFactory() {
return new TestPrincipalGatewayFilterFactory();
}
@Bean
public RecursiveHttpbinFilter recursiveHttpbinFilter() {
return new RecursiveHttpbinFilter();
}
@Bean
SecurityWebFilterChain springWebFilterChain(ServerHttpSecurity http) {
return http.httpBasic().and().authorizeExchange()
.pathMatchers("/hystrixprincipal").authenticated().anyExchange()
.permitAll().and().build();
}
@Bean
@SuppressWarnings("deprecation")
public MapReactiveUserDetailsService reactiveUserDetailsService() {
UserDetails user = User.withDefaultPasswordEncoder().username("user")
.password("password").roles("USER").build();
return new MapReactiveUserDetailsService(user);
}
}
public static class TestPrincipalGatewayFilterFactory
extends AbstractGatewayFilterFactory<Object> {
private final Log log = LogFactory
.getLog(TestPrincipalGatewayFilterFactory.class);
private KeyResolver keyResolver = new PrincipalNameKeyResolver();
private String resolvedPrincipal;
public TestPrincipalGatewayFilterFactory() {
super(Object.class);
}
@Override
public GatewayFilter apply(Object config) {
return (exchange, chain) -> keyResolver.resolve(exchange)
.defaultIfEmpty("Empty Principal").flatMap(name -> {
resolvedPrincipal = name;
return chain.filter(exchange);
});
}
}
}

26
spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/BaseWebClientTests.java

@ -28,7 +28,6 @@ import reactor.core.publisher.Mono;
import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.server.LocalServerPort; import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.cloud.gateway.route.Route; import org.springframework.cloud.gateway.route.Route;
import org.springframework.cloud.netflix.ribbon.RibbonClient; import org.springframework.cloud.netflix.ribbon.RibbonClient;
@ -42,7 +41,6 @@ import org.springframework.http.client.reactive.ClientHttpConnector;
import org.springframework.http.client.reactive.ReactorClientHttpConnector; import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.server.ServerWebExchange;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_HANDLER_MAPPER_ATTR; import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_HANDLER_MAPPER_ATTR;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR; import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR;
@ -94,8 +92,15 @@ public class BaseWebClientTests {
} }
@Bean @Bean
public RecursiveHttpbinFilter recursiveHttpbinFilter() { public GlobalFilter recursiveHttpbinFilter() {
return new RecursiveHttpbinFilter(); return (exchange, chain) -> {
if (exchange.getRequest().getPath().toString()
.contains("httpbin/httpbin")) {
return Mono.error(
new IllegalStateException("recursive call to /httpbin"));
}
return chain.filter(exchange);
};
} }
@Bean @Bean
@ -121,19 +126,6 @@ public class BaseWebClientTests {
} }
public static class RecursiveHttpbinFilter implements GlobalFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
if (exchange.getRequest().getPath().toString().contains("httpbin/httpbin")) {
return Mono
.error(new IllegalStateException("recursive call to /httpbin"));
}
return chain.filter(exchange);
}
}
@EnableAutoConfiguration @EnableAutoConfiguration
@SpringBootConfiguration @SpringBootConfiguration
@Import(DefaultTestConfig.class) @Import(DefaultTestConfig.class)

Loading…
Cancel
Save