|
| 1 | +/* |
| 2 | + * Copyright 2002-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.config.annotation.method.configuration; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import org.aopalliance.intercept.MethodInvocation; |
| 23 | + |
| 24 | +import org.springframework.beans.factory.annotation.Autowired; |
| 25 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 26 | +import org.springframework.context.annotation.Bean; |
| 27 | +import org.springframework.context.annotation.Configuration; |
| 28 | +import org.springframework.context.annotation.ImportAware; |
| 29 | +import org.springframework.context.annotation.Role; |
| 30 | +import org.springframework.core.type.AnnotationMetadata; |
| 31 | +import org.springframework.security.access.annotation.SecuredAnnotationAuthorizationManagerBeforeAdvice; |
| 32 | +import org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler; |
| 33 | +import org.springframework.security.access.expression.method.MethodSecurityExpressionHandler; |
| 34 | +import org.springframework.security.access.expression.method.PostAnnotationAuthorizationManagerAfterAdvice; |
| 35 | +import org.springframework.security.access.expression.method.PreAnnotationAuthorizationManagerBeforeAdvice; |
| 36 | +import org.springframework.security.access.intercept.aopalliance.AuthorizationMethodInterceptor; |
| 37 | +import org.springframework.security.access.intercept.aopalliance.MethodSecurityAuthorizationManagerAdvisor; |
| 38 | +import org.springframework.security.access.method.AuthorizationManagerAfterAdvice; |
| 39 | +import org.springframework.security.access.method.AuthorizationManagerBeforeAdvice; |
| 40 | +import org.springframework.security.access.method.DelegatingAuthorizationManagerAfterAdvice; |
| 41 | +import org.springframework.security.access.method.DelegatingAuthorizationManagerBeforeAdvice; |
| 42 | +import org.springframework.security.access.method.MethodAuthorizationContext; |
| 43 | + |
| 44 | +/** |
| 45 | + * Base {@link Configuration} for enabling Spring Security Method Security. |
| 46 | + * |
| 47 | + * @author Evgeniy Cheban |
| 48 | + * @see EnableMethodSecurity |
| 49 | + */ |
| 50 | +@Configuration(proxyBeanMethods = false) |
| 51 | +@Role(BeanDefinition.ROLE_INFRASTRUCTURE) |
| 52 | +final class MethodSecurityConfiguration implements ImportAware { |
| 53 | + |
| 54 | + private MethodSecurityExpressionHandler methodSecurityExpressionHandler; |
| 55 | + |
| 56 | + private AuthorizationManagerBeforeAdvice<MethodInvocation> authorizationManagerBeforeAdvice; |
| 57 | + |
| 58 | + private AuthorizationManagerAfterAdvice<MethodInvocation> authorizationManagerAfterAdvice; |
| 59 | + |
| 60 | + private int advisorOrder; |
| 61 | + |
| 62 | + @Bean |
| 63 | + @Role(BeanDefinition.ROLE_INFRASTRUCTURE) |
| 64 | + MethodSecurityAuthorizationManagerAdvisor methodSecurityAdvisor(AuthorizationMethodInterceptor interceptor) { |
| 65 | + MethodSecurityAuthorizationManagerAdvisor advisor = new MethodSecurityAuthorizationManagerAdvisor(interceptor, |
| 66 | + getAuthorizationManagerBeforeAdvice(), getAuthorizationManagerAfterAdvice()); |
| 67 | + advisor.setOrder(this.advisorOrder); |
| 68 | + return advisor; |
| 69 | + } |
| 70 | + |
| 71 | + @Bean |
| 72 | + @Role(BeanDefinition.ROLE_INFRASTRUCTURE) |
| 73 | + AuthorizationMethodInterceptor authorizationMethodInterceptor() { |
| 74 | + return new AuthorizationMethodInterceptor(getAuthorizationManagerBeforeAdvice(), |
| 75 | + getAuthorizationManagerAfterAdvice()); |
| 76 | + } |
| 77 | + |
| 78 | + private MethodSecurityExpressionHandler getMethodSecurityExpressionHandler() { |
| 79 | + if (this.methodSecurityExpressionHandler == null) { |
| 80 | + this.methodSecurityExpressionHandler = new DefaultMethodSecurityExpressionHandler(); |
| 81 | + } |
| 82 | + return this.methodSecurityExpressionHandler; |
| 83 | + } |
| 84 | + |
| 85 | + @Autowired(required = false) |
| 86 | + void setMethodSecurityExpressionHandler(MethodSecurityExpressionHandler methodSecurityExpressionHandler) { |
| 87 | + this.methodSecurityExpressionHandler = methodSecurityExpressionHandler; |
| 88 | + } |
| 89 | + |
| 90 | + private AuthorizationManagerBeforeAdvice<MethodInvocation> getAuthorizationManagerBeforeAdvice() { |
| 91 | + if (this.authorizationManagerBeforeAdvice == null) { |
| 92 | + this.authorizationManagerBeforeAdvice = createDefaultAuthorizationManagerBeforeAdvice(); |
| 93 | + } |
| 94 | + return this.authorizationManagerBeforeAdvice; |
| 95 | + } |
| 96 | + |
| 97 | + private AuthorizationManagerBeforeAdvice<MethodInvocation> createDefaultAuthorizationManagerBeforeAdvice() { |
| 98 | + List<AuthorizationManagerBeforeAdvice<MethodAuthorizationContext>> beforeAdvices = new ArrayList<>(); |
| 99 | + beforeAdvices.add(getPreAnnotationAuthorizationManagerBeforeAdvice()); |
| 100 | + beforeAdvices.add(new SecuredAnnotationAuthorizationManagerBeforeAdvice()); |
| 101 | + return new DelegatingAuthorizationManagerBeforeAdvice(beforeAdvices); |
| 102 | + } |
| 103 | + |
| 104 | + private PreAnnotationAuthorizationManagerBeforeAdvice getPreAnnotationAuthorizationManagerBeforeAdvice() { |
| 105 | + PreAnnotationAuthorizationManagerBeforeAdvice preAnnotationBeforeAdvice = new PreAnnotationAuthorizationManagerBeforeAdvice(); |
| 106 | + preAnnotationBeforeAdvice.setExpressionHandler(getMethodSecurityExpressionHandler()); |
| 107 | + return preAnnotationBeforeAdvice; |
| 108 | + } |
| 109 | + |
| 110 | + @Autowired(required = false) |
| 111 | + void setAuthorizationManagerBeforeAdvice( |
| 112 | + AuthorizationManagerBeforeAdvice<MethodInvocation> authorizationManagerBeforeAdvice) { |
| 113 | + this.authorizationManagerBeforeAdvice = authorizationManagerBeforeAdvice; |
| 114 | + } |
| 115 | + |
| 116 | + private AuthorizationManagerAfterAdvice<MethodInvocation> getAuthorizationManagerAfterAdvice() { |
| 117 | + if (this.authorizationManagerAfterAdvice == null) { |
| 118 | + this.authorizationManagerAfterAdvice = createDefaultAuthorizationManagerAfterAdvice(); |
| 119 | + } |
| 120 | + return this.authorizationManagerAfterAdvice; |
| 121 | + } |
| 122 | + |
| 123 | + private AuthorizationManagerAfterAdvice<MethodInvocation> createDefaultAuthorizationManagerAfterAdvice() { |
| 124 | + List<AuthorizationManagerAfterAdvice<MethodAuthorizationContext>> afterAdvices = new ArrayList<>(); |
| 125 | + afterAdvices.add(getPostAnnotationAuthorizationManagerAfterAdvice()); |
| 126 | + return new DelegatingAuthorizationManagerAfterAdvice(afterAdvices); |
| 127 | + } |
| 128 | + |
| 129 | + private PostAnnotationAuthorizationManagerAfterAdvice getPostAnnotationAuthorizationManagerAfterAdvice() { |
| 130 | + PostAnnotationAuthorizationManagerAfterAdvice postAnnotationAfterAdvice = new PostAnnotationAuthorizationManagerAfterAdvice(); |
| 131 | + postAnnotationAfterAdvice.setExpressionHandler(getMethodSecurityExpressionHandler()); |
| 132 | + return postAnnotationAfterAdvice; |
| 133 | + } |
| 134 | + |
| 135 | + @Autowired(required = false) |
| 136 | + void setAuthorizationManagerAfterAdvice( |
| 137 | + AuthorizationManagerAfterAdvice<MethodInvocation> authorizationManagerAfterAdvice) { |
| 138 | + this.authorizationManagerAfterAdvice = authorizationManagerAfterAdvice; |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public void setImportMetadata(AnnotationMetadata importMetadata) { |
| 143 | + this.advisorOrder = (int) importMetadata.getAnnotationAttributes(EnableMethodSecurity.class.getName()) |
| 144 | + .get("order"); |
| 145 | + } |
| 146 | + |
| 147 | +} |
0 commit comments