17
17
package org .springframework .security .config .annotation .method .configuration ;
18
18
19
19
import java .lang .annotation .Annotation ;
20
- import java .lang .reflect .AnnotatedElement ;
21
- import java .lang .reflect .Method ;
22
20
import java .util .ArrayList ;
23
- import java .util .Arrays ;
24
- import java .util .HashSet ;
25
21
import java .util .List ;
26
22
import java .util .Map ;
27
- import java .util .Set ;
28
23
29
24
import javax .annotation .security .DenyAll ;
30
25
import javax .annotation .security .PermitAll ;
31
26
import javax .annotation .security .RolesAllowed ;
32
27
33
- import org .springframework .aop .MethodMatcher ;
34
28
import org .springframework .aop .Pointcut ;
35
- import org .springframework .aop .support .AopUtils ;
29
+ import org .springframework .aop .support .ComposablePointcut ;
36
30
import org .springframework .aop .support .DefaultPointcutAdvisor ;
37
31
import org .springframework .aop .support .Pointcuts ;
38
- import org .springframework .aop .support .StaticMethodMatcher ;
32
+ import org .springframework .aop .support .annotation . AnnotationMatchingPointcut ;
39
33
import org .springframework .beans .factory .InitializingBean ;
40
34
import org .springframework .beans .factory .annotation .Autowired ;
41
35
import org .springframework .beans .factory .config .BeanDefinition ;
42
36
import org .springframework .context .annotation .Bean ;
43
37
import org .springframework .context .annotation .Configuration ;
44
38
import org .springframework .context .annotation .ImportAware ;
45
39
import org .springframework .context .annotation .Role ;
46
- import org .springframework .core .annotation .AnnotatedElementUtils ;
47
40
import org .springframework .core .annotation .AnnotationAttributes ;
48
41
import org .springframework .core .type .AnnotationMetadata ;
49
42
import org .springframework .security .access .annotation .Secured ;
50
43
import org .springframework .security .access .expression .method .DefaultMethodSecurityExpressionHandler ;
51
44
import org .springframework .security .access .expression .method .MethodSecurityExpressionHandler ;
52
45
import org .springframework .security .access .prepost .PostAuthorize ;
46
+ import org .springframework .security .access .prepost .PostFilter ;
53
47
import org .springframework .security .access .prepost .PreAuthorize ;
48
+ import org .springframework .security .access .prepost .PreFilter ;
54
49
import org .springframework .security .authorization .method .AuthorizationManagerMethodAfterAdvice ;
55
50
import org .springframework .security .authorization .method .AuthorizationManagerMethodBeforeAdvice ;
56
51
import org .springframework .security .authorization .method .AuthorizationMethodAfterAdvice ;
72
67
* Base {@link Configuration} for enabling Spring Security Method Security.
73
68
*
74
69
* @author Evgeniy Cheban
70
+ * @author Josh Cummings
75
71
* @see EnableMethodSecurity
76
72
* @since 5.5
77
73
*/
@@ -92,7 +88,9 @@ final class MethodSecurityConfiguration implements ImportAware, InitializingBean
92
88
@ Bean
93
89
@ Role (BeanDefinition .ROLE_INFRASTRUCTURE )
94
90
DefaultPointcutAdvisor methodSecurityAdvisor (AuthorizationMethodInterceptor interceptor ) {
95
- Pointcut pointcut = Pointcuts .union (getAuthorizationMethodBeforeAdvice (), getAuthorizationMethodAfterAdvice ());
91
+ AuthorizationMethodBeforeAdvice <?> beforeAdvice = getAuthorizationMethodBeforeAdvice ();
92
+ AuthorizationMethodAfterAdvice <?> afterAdvice = getAuthorizationMethodAfterAdvice ();
93
+ Pointcut pointcut = Pointcuts .union (beforeAdvice .getPointcut (), afterAdvice .getPointcut ());
96
94
DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor (pointcut , interceptor );
97
95
advisor .setOrder (order ());
98
96
return advisor ;
@@ -147,32 +145,34 @@ private AuthorizationMethodBeforeAdvice<MethodAuthorizationContext> createDefaul
147
145
}
148
146
149
147
private PreFilterAuthorizationMethodBeforeAdvice getPreFilterAuthorizationMethodBeforeAdvice () {
150
- PreFilterAuthorizationMethodBeforeAdvice preFilterBeforeAdvice = new PreFilterAuthorizationMethodBeforeAdvice ();
148
+ Pointcut pointcut = forAnnotation (PreFilter .class );
149
+ PreFilterAuthorizationMethodBeforeAdvice preFilterBeforeAdvice = new PreFilterAuthorizationMethodBeforeAdvice (
150
+ pointcut );
151
151
preFilterBeforeAdvice .setExpressionHandler (getMethodSecurityExpressionHandler ());
152
152
return preFilterBeforeAdvice ;
153
153
}
154
154
155
155
private AuthorizationMethodBeforeAdvice <MethodAuthorizationContext > getPreAuthorizeAuthorizationMethodBeforeAdvice () {
156
- MethodMatcher methodMatcher = new SecurityAnnotationsStaticMethodMatcher (PreAuthorize .class );
156
+ Pointcut pointcut = forAnnotation (PreAuthorize .class );
157
157
PreAuthorizeAuthorizationManager authorizationManager = new PreAuthorizeAuthorizationManager ();
158
158
authorizationManager .setExpressionHandler (getMethodSecurityExpressionHandler ());
159
- return new AuthorizationManagerMethodBeforeAdvice <>(methodMatcher , authorizationManager );
159
+ return new AuthorizationManagerMethodBeforeAdvice <>(pointcut , authorizationManager );
160
160
}
161
161
162
162
private AuthorizationManagerMethodBeforeAdvice <MethodAuthorizationContext > getSecuredAuthorizationMethodBeforeAdvice () {
163
- MethodMatcher methodMatcher = new SecurityAnnotationsStaticMethodMatcher (Secured .class );
163
+ Pointcut pointcut = forAnnotation (Secured .class );
164
164
SecuredAuthorizationManager authorizationManager = new SecuredAuthorizationManager ();
165
- return new AuthorizationManagerMethodBeforeAdvice <>(methodMatcher , authorizationManager );
165
+ return new AuthorizationManagerMethodBeforeAdvice <>(pointcut , authorizationManager );
166
166
}
167
167
168
168
private AuthorizationManagerMethodBeforeAdvice <MethodAuthorizationContext > getJsr250AuthorizationMethodBeforeAdvice () {
169
- MethodMatcher methodMatcher = new SecurityAnnotationsStaticMethodMatcher ( DenyAll .class , PermitAll .class ,
170
- RolesAllowed .class );
169
+ Pointcut pointcut = new ComposablePointcut ( forAnnotation ( DenyAll .class )). union ( forAnnotation ( PermitAll .class ))
170
+ . union ( forAnnotation ( RolesAllowed .class ) );
171
171
Jsr250AuthorizationManager authorizationManager = new Jsr250AuthorizationManager ();
172
172
if (this .grantedAuthorityDefaults != null ) {
173
173
authorizationManager .setRolePrefix (this .grantedAuthorityDefaults .getRolePrefix ());
174
174
}
175
- return new AuthorizationManagerMethodBeforeAdvice <>(methodMatcher , authorizationManager );
175
+ return new AuthorizationManagerMethodBeforeAdvice <>(pointcut , authorizationManager );
176
176
}
177
177
178
178
@ Autowired (required = false )
@@ -196,16 +196,18 @@ private AuthorizationMethodAfterAdvice<MethodAuthorizationContext> createDefault
196
196
}
197
197
198
198
private PostFilterAuthorizationMethodAfterAdvice getPostFilterAuthorizationMethodAfterAdvice () {
199
- PostFilterAuthorizationMethodAfterAdvice postFilterAfterAdvice = new PostFilterAuthorizationMethodAfterAdvice ();
199
+ Pointcut pointcut = forAnnotation (PostFilter .class );
200
+ PostFilterAuthorizationMethodAfterAdvice postFilterAfterAdvice = new PostFilterAuthorizationMethodAfterAdvice (
201
+ pointcut );
200
202
postFilterAfterAdvice .setExpressionHandler (getMethodSecurityExpressionHandler ());
201
203
return postFilterAfterAdvice ;
202
204
}
203
205
204
206
private AuthorizationManagerMethodAfterAdvice <MethodAuthorizationContext > getPostAuthorizeAuthorizationMethodAfterAdvice () {
205
- MethodMatcher methodMatcher = new SecurityAnnotationsStaticMethodMatcher (PostAuthorize .class );
207
+ Pointcut pointcut = forAnnotation (PostAuthorize .class );
206
208
PostAuthorizeAuthorizationManager authorizationManager = new PostAuthorizeAuthorizationManager ();
207
209
authorizationManager .setExpressionHandler (getMethodSecurityExpressionHandler ());
208
- return new AuthorizationManagerMethodAfterAdvice <>(methodMatcher , authorizationManager );
210
+ return new AuthorizationManagerMethodAfterAdvice <>(pointcut , authorizationManager );
209
211
}
210
212
211
213
@ Autowired (required = false )
@@ -241,27 +243,9 @@ private int order() {
241
243
return this .enableMethodSecurity .getNumber ("order" );
242
244
}
243
245
244
- private static final class SecurityAnnotationsStaticMethodMatcher extends StaticMethodMatcher {
245
-
246
- private final Set <Class <? extends Annotation >> annotationClasses ;
247
-
248
- @ SafeVarargs
249
- private SecurityAnnotationsStaticMethodMatcher (Class <? extends Annotation >... annotationClasses ) {
250
- this .annotationClasses = new HashSet <>(Arrays .asList (annotationClasses ));
251
- }
252
-
253
- @ Override
254
- public boolean matches (Method method , Class <?> targetClass ) {
255
- Method specificMethod = AopUtils .getMostSpecificMethod (method , targetClass );
256
- return hasAnnotations (specificMethod ) || hasAnnotations (specificMethod .getDeclaringClass ());
257
- }
258
-
259
- private boolean hasAnnotations (AnnotatedElement annotatedElement ) {
260
- Set <Annotation > annotations = AnnotatedElementUtils .findAllMergedAnnotations (annotatedElement ,
261
- this .annotationClasses );
262
- return !annotations .isEmpty ();
263
- }
264
-
246
+ private Pointcut forAnnotation (Class <? extends Annotation > annotationClass ) {
247
+ return Pointcuts .union (new AnnotationMatchingPointcut (annotationClass , true ),
248
+ new AnnotationMatchingPointcut (null , annotationClass , true ));
265
249
}
266
250
267
251
}
0 commit comments