|
@@ -20,15 +20,24 @@ import java.util.List;
|
|
import java.util.Optional;
|
|
import java.util.Optional;
|
|
|
|
|
|
import org.springframework.security.authentication.ReactiveAuthenticationManager;
|
|
import org.springframework.security.authentication.ReactiveAuthenticationManager;
|
|
|
|
+import org.springframework.security.authorization.AuthenticatedAuthorizationManager;
|
|
|
|
+import org.springframework.security.authorization.AuthorityAuthorizationManager;
|
|
|
|
+import org.springframework.security.authorization.AuthorizationDecision;
|
|
|
|
+import org.springframework.security.authorization.ReactiveAuthorizationManager;
|
|
import org.springframework.security.web.server.MatcherSecurityWebFilterChain;
|
|
import org.springframework.security.web.server.MatcherSecurityWebFilterChain;
|
|
import org.springframework.security.web.server.SecurityWebFilterChain;
|
|
import org.springframework.security.web.server.SecurityWebFilterChain;
|
|
|
|
+import org.springframework.security.web.server.authorization.AuthorizationContext;
|
|
|
|
+import org.springframework.security.web.server.authorization.AuthorizationWebFilter;
|
|
|
|
+import org.springframework.security.web.server.authorization.DelegatingReactiveAuthorizationManager;
|
|
import org.springframework.security.web.server.context.SecurityContextRepositoryWebFilter;
|
|
import org.springframework.security.web.server.context.SecurityContextRepositoryWebFilter;
|
|
import org.springframework.security.web.server.authorization.ExceptionTranslationWebFilter;
|
|
import org.springframework.security.web.server.authorization.ExceptionTranslationWebFilter;
|
|
import org.springframework.security.web.server.context.SecurityContextRepository;
|
|
import org.springframework.security.web.server.context.SecurityContextRepository;
|
|
import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher;
|
|
import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher;
|
|
|
|
+import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcherEntry;
|
|
import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatchers;
|
|
import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatchers;
|
|
import org.springframework.util.Assert;
|
|
import org.springframework.util.Assert;
|
|
import org.springframework.web.server.WebFilter;
|
|
import org.springframework.web.server.WebFilter;
|
|
|
|
+import reactor.core.publisher.Mono;
|
|
|
|
|
|
/**
|
|
/**
|
|
* @author Rob Winch
|
|
* @author Rob Winch
|
|
@@ -129,4 +138,73 @@ public class HttpSecurity {
|
|
}
|
|
}
|
|
|
|
|
|
private HttpSecurity() {}
|
|
private HttpSecurity() {}
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * @author Rob Winch
|
|
|
|
+ * @since 5.0
|
|
|
|
+ */
|
|
|
|
+ public class AuthorizeExchangeBuilder extends AbstractServerWebExchangeMatcherRegistry<AuthorizeExchangeBuilder.Access> {
|
|
|
|
+ private DelegatingReactiveAuthorizationManager.Builder managerBldr = DelegatingReactiveAuthorizationManager.builder();
|
|
|
|
+ private ServerWebExchangeMatcher matcher;
|
|
|
|
+ private boolean anyExchangeRegistered;
|
|
|
|
+
|
|
|
|
+ public HttpSecurity and() {
|
|
|
|
+ return HttpSecurity.this;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Access anyExchange() {
|
|
|
|
+ Access result = super.anyExchange();
|
|
|
|
+ anyExchangeRegistered = true;
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ protected Access registerMatcher(ServerWebExchangeMatcher matcher) {
|
|
|
|
+ if(anyExchangeRegistered) {
|
|
|
|
+ throw new IllegalStateException("Cannot register " + matcher + " which would be unreachable because anyExchange() has already been registered.");
|
|
|
|
+ }
|
|
|
|
+ if(this.matcher != null) {
|
|
|
|
+ throw new IllegalStateException("The matcher " + matcher + " does not have an access rule defined");
|
|
|
|
+ }
|
|
|
|
+ this.matcher = matcher;
|
|
|
|
+ return new Access();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ protected WebFilter build() {
|
|
|
|
+ if(this.matcher != null) {
|
|
|
|
+ throw new IllegalStateException("The matcher " + matcher + " does not have an access rule defined");
|
|
|
|
+ }
|
|
|
|
+ return new AuthorizationWebFilter(managerBldr.build());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public final class Access {
|
|
|
|
+
|
|
|
|
+ public AuthorizeExchangeBuilder permitAll() {
|
|
|
|
+ return access( (a,e) -> Mono.just(new AuthorizationDecision(true)));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public AuthorizeExchangeBuilder denyAll() {
|
|
|
|
+ return access( (a,e) -> Mono.just(new AuthorizationDecision(false)));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public AuthorizeExchangeBuilder hasRole(String role) {
|
|
|
|
+ return access(AuthorityAuthorizationManager.hasRole(role));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public AuthorizeExchangeBuilder hasAuthority(String authority) {
|
|
|
|
+ return access(AuthorityAuthorizationManager.hasAuthority(authority));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public AuthorizeExchangeBuilder authenticated() {
|
|
|
|
+ return access(AuthenticatedAuthorizationManager.authenticated());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public AuthorizeExchangeBuilder access(ReactiveAuthorizationManager<AuthorizationContext> manager) {
|
|
|
|
+ managerBldr.add(new ServerWebExchangeMatcherEntry<>(matcher, manager));
|
|
|
|
+ matcher = null;
|
|
|
|
+ return AuthorizeExchangeBuilder.this;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|