Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.springframework.context.annotation.AdviceMode;
import org.springframework.context.annotation.Import;
import org.springframework.core.Ordered;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.authorization.ReactiveAuthorizationManager;

/**
Expand Down Expand Up @@ -76,4 +77,20 @@
*/
boolean useAuthorizationManager() default true;

/**
* Determines if Spring Security's {@link Secured} annotation should be enabled.
* Requires {@link #useAuthorizationManager()} to be {@code true}. Default is false.
* @return true if {@link Secured} annotation should be enabled false otherwise
* @since 7.3
*/
boolean securedEnabled() default false;

/**
* Determines if JSR-250 annotations should be enabled. Requires
* {@link #useAuthorizationManager()} to be {@code true}. Default is false.
* @return true if JSR-250 should be enabled false otherwise
* @since 7.3
*/
boolean jsr250Enabled() default false;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2004-present 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.security.config.annotation.method.configuration;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

import org.springframework.aop.Pointcut;
import org.springframework.aop.framework.AopInfrastructureBean;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Role;
import org.springframework.security.access.hierarchicalroles.RoleHierarchy;
import org.springframework.security.authorization.AuthoritiesAuthorizationManager;
import org.springframework.security.authorization.ReactiveAuthorizationManager;
import org.springframework.security.authorization.method.AuthorizationManagerBeforeReactiveMethodInterceptor;
import org.springframework.security.authorization.method.Jsr250ReactiveAuthorizationManager;
import org.springframework.security.config.ObjectPostProcessor;
import org.springframework.security.config.core.GrantedAuthorityDefaults;

/**
* {@link Configuration} for enabling JSR-250 Spring Security Reactive Method Security.
*
* @author Rohit Behera
* @since 7.3
* @see EnableReactiveMethodSecurity
*/
@Configuration(value = "_reactiveJsr250MethodSecurityConfiguration", proxyBeanMethods = false)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
final class ReactiveJsr250MethodSecurityConfiguration implements AopInfrastructureBean {

private static final Pointcut pointcut = AuthorizationManagerBeforeReactiveMethodInterceptor.jsr250().getPointcut();

private final Jsr250ReactiveAuthorizationManager authorizationManager = new Jsr250ReactiveAuthorizationManager();

private final AuthorizationManagerBeforeReactiveMethodInterceptor methodInterceptor;

ReactiveJsr250MethodSecurityConfiguration(
ObjectProvider<ObjectPostProcessor<ReactiveAuthorizationManager<MethodInvocation>>> postProcessors) {
ReactiveAuthorizationManager<MethodInvocation> manager = postProcessors
.getIfUnique(ObjectPostProcessor::identity)
.postProcess(this.authorizationManager);
this.methodInterceptor = AuthorizationManagerBeforeReactiveMethodInterceptor.jsr250(manager);
}

@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
static MethodInterceptor jsr250AuthorizationMethodInterceptor(
ObjectProvider<ReactiveJsr250MethodSecurityConfiguration> _reactiveJsr250MethodSecurityConfiguration) {
return new DeferringMethodInterceptor<>(pointcut,
() -> _reactiveJsr250MethodSecurityConfiguration.getObject().methodInterceptor);
}

@Autowired(required = false)
void setGrantedAuthorityDefaults(GrantedAuthorityDefaults defaults) {
this.authorizationManager.setRolePrefix(defaults.getRolePrefix());
}

@Autowired(required = false)
void setRoleHierarchy(RoleHierarchy roleHierarchy) {
AuthoritiesAuthorizationManager authoritiesAuthorizationManager = new AuthoritiesAuthorizationManager();
authoritiesAuthorizationManager.setRoleHierarchy(roleHierarchy);
this.authorizationManager.setAuthoritiesAuthorizationManager(authoritiesAuthorizationManager);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,17 @@ public String[] selectImports(AnnotationMetadata importMetadata) {
List<String> imports = new ArrayList<>(Arrays.asList(this.autoProxy.selectImports(importMetadata)));
if (annotation.useAuthorizationManager()) {
imports.add(ReactiveAuthorizationManagerMethodSecurityConfiguration.class.getName());
if (annotation.securedEnabled()) {
imports.add(ReactiveSecuredMethodSecurityConfiguration.class.getName());
}
if (annotation.jsr250Enabled()) {
imports.add(ReactiveJsr250MethodSecurityConfiguration.class.getName());
}
}
else {
Assert.state(!annotation.securedEnabled() && !annotation.jsr250Enabled(),
"@EnableReactiveMethodSecurity securedEnabled and jsr250Enabled "
+ "require useAuthorizationManager = true");
Assert.state(
ClassUtils.isPresent(METHOD_SECURITY_METADATA_SOURCE_ADVISOR, ClassUtils.getDefaultClassLoader()),
() -> "@EnableReactiveMethodSecurity(useAuthorizationManager = false) requires the "
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2004-present 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.security.config.annotation.method.configuration;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

import org.springframework.aop.Pointcut;
import org.springframework.aop.framework.AopInfrastructureBean;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Role;
import org.springframework.security.access.hierarchicalroles.RoleHierarchy;
import org.springframework.security.authorization.AuthoritiesAuthorizationManager;
import org.springframework.security.authorization.ReactiveAuthorizationManager;
import org.springframework.security.authorization.method.AuthorizationManagerBeforeReactiveMethodInterceptor;
import org.springframework.security.authorization.method.SecuredReactiveAuthorizationManager;
import org.springframework.security.config.ObjectPostProcessor;

/**
* {@link Configuration} for enabling {@code @Secured} Spring Security Reactive Method
* Security.
*
* @author Rohit Behera
* @since 7.3
* @see EnableReactiveMethodSecurity
*/
@Configuration(value = "_reactiveSecuredMethodSecurityConfiguration", proxyBeanMethods = false)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
final class ReactiveSecuredMethodSecurityConfiguration implements AopInfrastructureBean {

private static final Pointcut pointcut = AuthorizationManagerBeforeReactiveMethodInterceptor.secured()
.getPointcut();

private final SecuredReactiveAuthorizationManager authorizationManager = new SecuredReactiveAuthorizationManager();

private final AuthorizationManagerBeforeReactiveMethodInterceptor methodInterceptor;

ReactiveSecuredMethodSecurityConfiguration(
ObjectProvider<ObjectPostProcessor<ReactiveAuthorizationManager<MethodInvocation>>> postProcessors) {
ReactiveAuthorizationManager<MethodInvocation> manager = postProcessors
.getIfUnique(ObjectPostProcessor::identity)
.postProcess(this.authorizationManager);
this.methodInterceptor = AuthorizationManagerBeforeReactiveMethodInterceptor.secured(manager);
}

@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
static MethodInterceptor securedAuthorizationMethodInterceptor(
ObjectProvider<ReactiveSecuredMethodSecurityConfiguration> _reactiveSecuredMethodSecurityConfiguration) {
return new DeferringMethodInterceptor<>(pointcut,
() -> _reactiveSecuredMethodSecurityConfiguration.getObject().methodInterceptor);
}

@Autowired(required = false)
void setRoleHierarchy(RoleHierarchy roleHierarchy) {
AuthoritiesAuthorizationManager authoritiesAuthorizationManager = new AuthoritiesAuthorizationManager();
authoritiesAuthorizationManager.setRoleHierarchy(roleHierarchy);
this.authorizationManager.setAuthoritiesAuthorizationManager(authoritiesAuthorizationManager);
}

}
Loading