10 changed files with 281 additions and 15 deletions
@ -0,0 +1,98 @@
@@ -0,0 +1,98 @@
|
||||
/* |
||||
* |
||||
* * Copyright 2002-2017 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 |
||||
* * |
||||
* * http://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.security.config.annotation.web.reactive; |
||||
|
||||
import org.junit.Test; |
||||
import org.junit.experimental.runners.Enclosed; |
||||
import org.junit.runner.RunWith; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.core.Ordered; |
||||
import org.springframework.core.annotation.Order; |
||||
import org.springframework.security.config.web.server.HttpSecurity; |
||||
import org.springframework.security.core.userdetails.MapUserDetailsRepository; |
||||
import org.springframework.security.core.userdetails.User; |
||||
import org.springframework.security.core.userdetails.UserDetailsRepository; |
||||
import org.springframework.security.test.web.reactive.server.WebTestClientBuilder; |
||||
import org.springframework.security.web.server.SecurityWebFilterChain; |
||||
import org.springframework.security.web.server.WebFilterChainFilter; |
||||
import org.springframework.security.web.server.util.matcher.PathMatcherServerWebExchangeMatcher; |
||||
import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher; |
||||
import org.springframework.test.context.junit4.SpringRunner; |
||||
import org.springframework.test.web.reactive.server.WebTestClient; |
||||
|
||||
import static org.mockito.Mockito.mock; |
||||
|
||||
/** |
||||
* @author Rob Winch |
||||
* @since 5.0 |
||||
*/ |
||||
@RunWith(Enclosed.class) |
||||
public class EnableWebFluxSecurityTests { |
||||
|
||||
|
||||
@RunWith(SpringRunner.class) |
||||
public static class MultiHttpSecurity { |
||||
@Autowired |
||||
WebFilterChainFilter springSecurityFilterChain; |
||||
|
||||
@Test |
||||
public void multiWorks() { |
||||
WebTestClient client = WebTestClientBuilder.bindToWebFilters(springSecurityFilterChain).build(); |
||||
|
||||
client.get() |
||||
.uri("/api/test") |
||||
.exchange() |
||||
.expectStatus().isUnauthorized() |
||||
.expectBody().isEmpty(); |
||||
|
||||
client.get() |
||||
.uri("/test") |
||||
.exchange() |
||||
.expectStatus().isOk(); |
||||
} |
||||
|
||||
@EnableWebFluxSecurity |
||||
static class Config { |
||||
@Order(Ordered.HIGHEST_PRECEDENCE) |
||||
@Bean |
||||
public SecurityWebFilterChain apiHttpSecurity(HttpSecurity http) { |
||||
http |
||||
.securityMatcher(new PathMatcherServerWebExchangeMatcher("/api/**")) |
||||
.authorizeExchange() |
||||
.anyExchange().denyAll(); |
||||
return http.build(); |
||||
} |
||||
|
||||
@Bean |
||||
public SecurityWebFilterChain httpSecurity(HttpSecurity http) { |
||||
return http.build(); |
||||
} |
||||
|
||||
@Bean |
||||
public UserDetailsRepository userDetailsRepository() { |
||||
return new MapUserDetailsRepository(User.withUsername("user") |
||||
.password("password") |
||||
.roles("USER") |
||||
.build() |
||||
); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,56 @@
@@ -0,0 +1,56 @@
|
||||
/* |
||||
* |
||||
* * Copyright 2002-2017 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 |
||||
* * |
||||
* * http://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.security.web.server; |
||||
|
||||
import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher; |
||||
import org.springframework.web.server.ServerWebExchange; |
||||
import org.springframework.web.server.WebFilter; |
||||
import reactor.core.publisher.Flux; |
||||
import reactor.core.publisher.Mono; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @author Rob Winch |
||||
* @since 5.0 |
||||
*/ |
||||
public class MatcherSecurityWebFilterChain implements SecurityWebFilterChain { |
||||
private final ServerWebExchangeMatcher matcher; |
||||
private final Flux<WebFilter> filters; |
||||
|
||||
public MatcherSecurityWebFilterChain(ServerWebExchangeMatcher matcher, List<WebFilter> filters) { |
||||
this(matcher, Flux.fromIterable(filters)); |
||||
} |
||||
|
||||
public MatcherSecurityWebFilterChain(ServerWebExchangeMatcher matcher, Flux<WebFilter> filters) { |
||||
this.matcher = matcher; |
||||
this.filters = filters; |
||||
} |
||||
|
||||
@Override |
||||
public Mono<Boolean> matches(ServerWebExchange exchange) { |
||||
return matcher.matches(exchange) |
||||
.map( m -> m.isMatch() ); |
||||
} |
||||
|
||||
@Override |
||||
public Flux<WebFilter> getWebFilters() { |
||||
return filters; |
||||
} |
||||
} |
||||
@ -0,0 +1,35 @@
@@ -0,0 +1,35 @@
|
||||
/* |
||||
* |
||||
* * Copyright 2002-2017 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 |
||||
* * |
||||
* * http://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.security.web.server; |
||||
|
||||
import org.springframework.web.server.ServerWebExchange; |
||||
import org.springframework.web.server.WebFilter; |
||||
import reactor.core.publisher.Flux; |
||||
import reactor.core.publisher.Mono; |
||||
|
||||
/** |
||||
* @author Rob Winch |
||||
* @since 5.0 |
||||
*/ |
||||
public interface SecurityWebFilterChain { |
||||
|
||||
Mono<Boolean> matches(ServerWebExchange exchange); |
||||
|
||||
Flux<WebFilter> getWebFilters(); |
||||
} |
||||
Loading…
Reference in new issue