Add method chaining for AuthorizeExchangeBuilder

Fixes gh-4345
This commit is contained in:
Rob Winch 2017-05-19 21:25:50 -05:00
parent 0428cdd934
commit 1cec497a50
3 changed files with 22 additions and 21 deletions

View File

@ -63,29 +63,30 @@ public class AuthorizeExchangeBuilder extends AbstractServerWebExchangeMatcherRe
public final class Access { public final class Access {
public void permitAll() { public AuthorizeExchangeBuilder permitAll() {
access( (a,e) -> Mono.just(new AuthorizationDecision(true))); return access( (a,e) -> Mono.just(new AuthorizationDecision(true)));
} }
public void denyAll() { public AuthorizeExchangeBuilder denyAll() {
access( (a,e) -> Mono.just(new AuthorizationDecision(false))); return access( (a,e) -> Mono.just(new AuthorizationDecision(false)));
} }
public void hasRole(String role) { public AuthorizeExchangeBuilder hasRole(String role) {
access(AuthorityAuthorizationManager.hasRole(role)); return access(AuthorityAuthorizationManager.hasRole(role));
} }
public void hasAuthority(String authority) { public AuthorizeExchangeBuilder hasAuthority(String authority) {
access(AuthorityAuthorizationManager.hasAuthority(authority)); return access(AuthorityAuthorizationManager.hasAuthority(authority));
} }
public void authenticated() { public AuthorizeExchangeBuilder authenticated() {
access(AuthenticatedAuthorizationManager.authenticated()); return access(AuthenticatedAuthorizationManager.authenticated());
} }
public void access(ReactiveAuthorizationManager<AuthorizationContext> manager) { public AuthorizeExchangeBuilder access(ReactiveAuthorizationManager<AuthorizationContext> manager) {
managerBldr.add(matcher, manager); managerBldr.add(matcher, manager);
matcher = null; matcher = null;
return AuthorizeExchangeBuilder.this;
} }
} }
} }

View File

@ -21,7 +21,6 @@ package sample;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.security.authorization.AuthorizationDecision; import org.springframework.security.authorization.AuthorizationDecision;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity; import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.AuthorizeExchangeBuilder;
import org.springframework.security.config.web.server.HttpSecurity; import org.springframework.security.config.web.server.HttpSecurity;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.web.server.authorization.AuthorizationContext; import org.springframework.security.web.server.authorization.AuthorizationContext;
@ -39,10 +38,11 @@ public class SecurityConfig {
WebFilter springSecurityFilterChain(HttpSecurity http) throws Exception { WebFilter springSecurityFilterChain(HttpSecurity http) throws Exception {
http.httpBasic(); http.httpBasic();
AuthorizeExchangeBuilder authorize = http.authorizeExchange(); http.authorizeExchange()
authorize.antMatchers("/admin/**").hasRole("ADMIN"); .antMatchers("/admin/**").hasRole("ADMIN")
authorize.antMatchers("/users/{user}/**").access(this::currentUserMatchesPath); .antMatchers("/users/{user}/**").access(this::currentUserMatchesPath)
authorize.anyExchange().authenticated(); .anyExchange().authenticated();
return http.build(); return http.build();
} }

View File

@ -21,7 +21,6 @@ package sample;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.security.authorization.AuthorizationDecision; import org.springframework.security.authorization.AuthorizationDecision;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity; import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.AuthorizeExchangeBuilder;
import org.springframework.security.config.web.server.HttpSecurity; import org.springframework.security.config.web.server.HttpSecurity;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.web.server.authorization.AuthorizationContext; import org.springframework.security.web.server.authorization.AuthorizationContext;
@ -39,10 +38,11 @@ public class SecurityConfig {
WebFilter springSecurityFilterChain(HttpSecurity http) throws Exception { WebFilter springSecurityFilterChain(HttpSecurity http) throws Exception {
http.httpBasic(); http.httpBasic();
AuthorizeExchangeBuilder authorize = http.authorizeExchange(); http.authorizeExchange()
authorize.antMatchers("/admin/**").hasRole("ADMIN"); .antMatchers("/admin/**").hasRole("ADMIN")
authorize.antMatchers("/users/{user}/**").access(this::currentUserMatchesPath); .antMatchers("/users/{user}/**").access(this::currentUserMatchesPath)
authorize.anyExchange().authenticated(); .anyExchange().authenticated();
return http.build(); return http.build();
} }