16
16
17
17
package org .springframework .security .config .annotation .method .configuration ;
18
18
19
- import java .lang .annotation .Annotation ;
20
19
import java .util .ArrayList ;
21
20
import java .util .List ;
22
21
import java .util .Map ;
23
22
24
- import javax .annotation .security .DenyAll ;
25
- import javax .annotation .security .PermitAll ;
26
- import javax .annotation .security .RolesAllowed ;
27
-
28
- import org .springframework .aop .Pointcut ;
29
- import org .springframework .aop .support .ComposablePointcut ;
30
23
import org .springframework .aop .support .DefaultPointcutAdvisor ;
31
- import org .springframework .aop .support .Pointcuts ;
32
- import org .springframework .aop .support .annotation .AnnotationMatchingPointcut ;
33
24
import org .springframework .beans .factory .InitializingBean ;
34
25
import org .springframework .beans .factory .annotation .Autowired ;
35
26
import org .springframework .beans .factory .config .BeanDefinition ;
39
30
import org .springframework .context .annotation .Role ;
40
31
import org .springframework .core .annotation .AnnotationAttributes ;
41
32
import org .springframework .core .type .AnnotationMetadata ;
42
- import org .springframework .security .access .annotation .Secured ;
43
33
import org .springframework .security .access .expression .method .DefaultMethodSecurityExpressionHandler ;
44
34
import org .springframework .security .access .expression .method .MethodSecurityExpressionHandler ;
45
- import org .springframework .security .access .prepost .PostAuthorize ;
46
- import org .springframework .security .access .prepost .PostFilter ;
47
- import org .springframework .security .access .prepost .PreAuthorize ;
48
- import org .springframework .security .access .prepost .PreFilter ;
49
- import org .springframework .security .authorization .method .AuthorizationManagerMethodAfterAdvice ;
50
- import org .springframework .security .authorization .method .AuthorizationManagerMethodBeforeAdvice ;
51
- import org .springframework .security .authorization .method .AuthorizationMethodAfterAdvice ;
52
- import org .springframework .security .authorization .method .AuthorizationMethodBeforeAdvice ;
53
35
import org .springframework .security .authorization .method .AuthorizationMethodInterceptor ;
54
- import org .springframework .security .authorization .method .DelegatingAuthorizationMethodAfterAdvice ;
55
- import org .springframework .security .authorization .method .DelegatingAuthorizationMethodBeforeAdvice ;
36
+ import org .springframework .security .authorization .method .AuthorizationMethodInterceptors ;
37
+ import org .springframework .security .authorization .method .DelegatingAuthorizationMethodInterceptor ;
56
38
import org .springframework .security .authorization .method .Jsr250AuthorizationManager ;
57
- import org .springframework .security .authorization .method .MethodAuthorizationContext ;
58
39
import org .springframework .security .authorization .method .PostAuthorizeAuthorizationManager ;
59
- import org .springframework .security .authorization .method .PostFilterAuthorizationMethodAfterAdvice ;
40
+ import org .springframework .security .authorization .method .PostFilterAuthorizationMethodInterceptor ;
60
41
import org .springframework .security .authorization .method .PreAuthorizeAuthorizationManager ;
61
- import org .springframework .security .authorization .method .PreFilterAuthorizationMethodBeforeAdvice ;
42
+ import org .springframework .security .authorization .method .PreFilterAuthorizationMethodInterceptor ;
62
43
import org .springframework .security .authorization .method .SecuredAuthorizationManager ;
63
44
import org .springframework .security .config .core .GrantedAuthorityDefaults ;
64
45
import org .springframework .util .Assert ;
@@ -79,30 +60,19 @@ final class MethodSecurityConfiguration implements ImportAware, InitializingBean
79
60
80
61
private GrantedAuthorityDefaults grantedAuthorityDefaults ;
81
62
82
- private AuthorizationMethodBeforeAdvice <MethodAuthorizationContext > authorizationMethodBeforeAdvice ;
83
-
84
- private AuthorizationMethodAfterAdvice <MethodAuthorizationContext > authorizationMethodAfterAdvice ;
63
+ private AuthorizationMethodInterceptor interceptor ;
85
64
86
65
private AnnotationAttributes enableMethodSecurity ;
87
66
88
67
@ Bean
89
68
@ Role (BeanDefinition .ROLE_INFRASTRUCTURE )
90
- DefaultPointcutAdvisor methodSecurityAdvisor (AuthorizationMethodInterceptor interceptor ) {
91
- AuthorizationMethodBeforeAdvice <?> beforeAdvice = getAuthorizationMethodBeforeAdvice ();
92
- AuthorizationMethodAfterAdvice <?> afterAdvice = getAuthorizationMethodAfterAdvice ();
93
- Pointcut pointcut = Pointcuts .union (beforeAdvice .getPointcut (), afterAdvice .getPointcut ());
94
- DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor (pointcut , interceptor );
69
+ DefaultPointcutAdvisor methodSecurityAdvisor () {
70
+ AuthorizationMethodInterceptor interceptor = getInterceptor ();
71
+ DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor (interceptor .getPointcut (), interceptor );
95
72
advisor .setOrder (order ());
96
73
return advisor ;
97
74
}
98
75
99
- @ Bean
100
- @ Role (BeanDefinition .ROLE_INFRASTRUCTURE )
101
- AuthorizationMethodInterceptor authorizationMethodInterceptor () {
102
- return new AuthorizationMethodInterceptor (getAuthorizationMethodBeforeAdvice (),
103
- getAuthorizationMethodAfterAdvice ());
104
- }
105
-
106
76
private MethodSecurityExpressionHandler getMethodSecurityExpressionHandler () {
107
77
if (this .methodSecurityExpressionHandler == null ) {
108
78
DefaultMethodSecurityExpressionHandler methodSecurityExpressionHandler = new DefaultMethodSecurityExpressionHandler ();
@@ -124,15 +94,18 @@ void setGrantedAuthorityDefaults(GrantedAuthorityDefaults grantedAuthorityDefaul
124
94
this .grantedAuthorityDefaults = grantedAuthorityDefaults ;
125
95
}
126
96
127
- private AuthorizationMethodBeforeAdvice < MethodAuthorizationContext > getAuthorizationMethodBeforeAdvice () {
128
- if (this .authorizationMethodBeforeAdvice = = null ) {
129
- this .authorizationMethodBeforeAdvice = createDefaultAuthorizationMethodBeforeAdvice () ;
97
+ private AuthorizationMethodInterceptor getInterceptor () {
98
+ if (this .interceptor ! = null ) {
99
+ return this .interceptor ;
130
100
}
131
- return this .authorizationMethodBeforeAdvice ;
101
+ List <AuthorizationMethodInterceptor > interceptors = new ArrayList <>();
102
+ interceptors .addAll (createDefaultAuthorizationMethodBeforeAdvice ());
103
+ interceptors .addAll (createDefaultAuthorizationMethodAfterAdvice ());
104
+ return new DelegatingAuthorizationMethodInterceptor (interceptors );
132
105
}
133
106
134
- private AuthorizationMethodBeforeAdvice < MethodAuthorizationContext > createDefaultAuthorizationMethodBeforeAdvice () {
135
- List <AuthorizationMethodBeforeAdvice < MethodAuthorizationContext > > beforeAdvices = new ArrayList <>();
107
+ private List < AuthorizationMethodInterceptor > createDefaultAuthorizationMethodBeforeAdvice () {
108
+ List <AuthorizationMethodInterceptor > beforeAdvices = new ArrayList <>();
136
109
beforeAdvices .add (getPreFilterAuthorizationMethodBeforeAdvice ());
137
110
beforeAdvices .add (getPreAuthorizeAuthorizationMethodBeforeAdvice ());
138
111
if (securedEnabled ()) {
@@ -141,79 +114,55 @@ private AuthorizationMethodBeforeAdvice<MethodAuthorizationContext> createDefaul
141
114
if (jsr250Enabled ()) {
142
115
beforeAdvices .add (getJsr250AuthorizationMethodBeforeAdvice ());
143
116
}
144
- return new DelegatingAuthorizationMethodBeforeAdvice <>( beforeAdvices ) ;
117
+ return beforeAdvices ;
145
118
}
146
119
147
- private PreFilterAuthorizationMethodBeforeAdvice getPreFilterAuthorizationMethodBeforeAdvice () {
148
- Pointcut pointcut = forAnnotation (PreFilter .class );
149
- PreFilterAuthorizationMethodBeforeAdvice preFilterBeforeAdvice = new PreFilterAuthorizationMethodBeforeAdvice (
150
- pointcut );
151
- preFilterBeforeAdvice .setExpressionHandler (getMethodSecurityExpressionHandler ());
152
- return preFilterBeforeAdvice ;
120
+ private PreFilterAuthorizationMethodInterceptor getPreFilterAuthorizationMethodBeforeAdvice () {
121
+ PreFilterAuthorizationMethodInterceptor interceptor = new PreFilterAuthorizationMethodInterceptor ();
122
+ interceptor .setExpressionHandler (getMethodSecurityExpressionHandler ());
123
+ return interceptor ;
153
124
}
154
125
155
- private AuthorizationMethodBeforeAdvice <MethodAuthorizationContext > getPreAuthorizeAuthorizationMethodBeforeAdvice () {
156
- Pointcut pointcut = forAnnotation (PreAuthorize .class );
126
+ private AuthorizationMethodInterceptor getPreAuthorizeAuthorizationMethodBeforeAdvice () {
157
127
PreAuthorizeAuthorizationManager authorizationManager = new PreAuthorizeAuthorizationManager ();
158
128
authorizationManager .setExpressionHandler (getMethodSecurityExpressionHandler ());
159
- return new AuthorizationManagerMethodBeforeAdvice <>( pointcut , authorizationManager );
129
+ return AuthorizationMethodInterceptors . preAuthorize ( authorizationManager );
160
130
}
161
131
162
- private AuthorizationManagerMethodBeforeAdvice <MethodAuthorizationContext > getSecuredAuthorizationMethodBeforeAdvice () {
163
- Pointcut pointcut = forAnnotation (Secured .class );
164
- SecuredAuthorizationManager authorizationManager = new SecuredAuthorizationManager ();
165
- return new AuthorizationManagerMethodBeforeAdvice <>(pointcut , authorizationManager );
132
+ private AuthorizationMethodInterceptor getSecuredAuthorizationMethodBeforeAdvice () {
133
+ return AuthorizationMethodInterceptors .secured (new SecuredAuthorizationManager ());
166
134
}
167
135
168
- private AuthorizationManagerMethodBeforeAdvice <MethodAuthorizationContext > getJsr250AuthorizationMethodBeforeAdvice () {
169
- Pointcut pointcut = new ComposablePointcut (forAnnotation (DenyAll .class )).union (forAnnotation (PermitAll .class ))
170
- .union (forAnnotation (RolesAllowed .class ));
136
+ private AuthorizationMethodInterceptor getJsr250AuthorizationMethodBeforeAdvice () {
171
137
Jsr250AuthorizationManager authorizationManager = new Jsr250AuthorizationManager ();
172
138
if (this .grantedAuthorityDefaults != null ) {
173
139
authorizationManager .setRolePrefix (this .grantedAuthorityDefaults .getRolePrefix ());
174
140
}
175
- return new AuthorizationManagerMethodBeforeAdvice <>( pointcut , authorizationManager );
141
+ return AuthorizationMethodInterceptors . jsr250 ( authorizationManager );
176
142
}
177
143
178
144
@ Autowired (required = false )
179
- void setAuthorizationMethodBeforeAdvice (
180
- AuthorizationMethodBeforeAdvice <MethodAuthorizationContext > authorizationMethodBeforeAdvice ) {
181
- this .authorizationMethodBeforeAdvice = authorizationMethodBeforeAdvice ;
182
- }
183
-
184
- private AuthorizationMethodAfterAdvice <MethodAuthorizationContext > getAuthorizationMethodAfterAdvice () {
185
- if (this .authorizationMethodAfterAdvice == null ) {
186
- this .authorizationMethodAfterAdvice = createDefaultAuthorizationMethodAfterAdvice ();
187
- }
188
- return this .authorizationMethodAfterAdvice ;
145
+ void setAuthorizationMethodInterceptor (AuthorizationMethodInterceptor interceptor ) {
146
+ this .interceptor = interceptor ;
189
147
}
190
148
191
- private AuthorizationMethodAfterAdvice < MethodAuthorizationContext > createDefaultAuthorizationMethodAfterAdvice () {
192
- List <AuthorizationMethodAfterAdvice < MethodAuthorizationContext > > afterAdvices = new ArrayList <>();
149
+ private List < AuthorizationMethodInterceptor > createDefaultAuthorizationMethodAfterAdvice () {
150
+ List <AuthorizationMethodInterceptor > afterAdvices = new ArrayList <>();
193
151
afterAdvices .add (getPostFilterAuthorizationMethodAfterAdvice ());
194
152
afterAdvices .add (getPostAuthorizeAuthorizationMethodAfterAdvice ());
195
- return new DelegatingAuthorizationMethodAfterAdvice <>( afterAdvices ) ;
153
+ return afterAdvices ;
196
154
}
197
155
198
- private PostFilterAuthorizationMethodAfterAdvice getPostFilterAuthorizationMethodAfterAdvice () {
199
- Pointcut pointcut = forAnnotation (PostFilter .class );
200
- PostFilterAuthorizationMethodAfterAdvice postFilterAfterAdvice = new PostFilterAuthorizationMethodAfterAdvice (
201
- pointcut );
202
- postFilterAfterAdvice .setExpressionHandler (getMethodSecurityExpressionHandler ());
203
- return postFilterAfterAdvice ;
156
+ private AuthorizationMethodInterceptor getPostFilterAuthorizationMethodAfterAdvice () {
157
+ PostFilterAuthorizationMethodInterceptor interceptor = new PostFilterAuthorizationMethodInterceptor ();
158
+ interceptor .setExpressionHandler (getMethodSecurityExpressionHandler ());
159
+ return interceptor ;
204
160
}
205
161
206
- private AuthorizationManagerMethodAfterAdvice <MethodAuthorizationContext > getPostAuthorizeAuthorizationMethodAfterAdvice () {
207
- Pointcut pointcut = forAnnotation (PostAuthorize .class );
162
+ private AuthorizationMethodInterceptor getPostAuthorizeAuthorizationMethodAfterAdvice () {
208
163
PostAuthorizeAuthorizationManager authorizationManager = new PostAuthorizeAuthorizationManager ();
209
164
authorizationManager .setExpressionHandler (getMethodSecurityExpressionHandler ());
210
- return new AuthorizationManagerMethodAfterAdvice <>(pointcut , authorizationManager );
211
- }
212
-
213
- @ Autowired (required = false )
214
- void setAuthorizationMethodAfterAdvice (
215
- AuthorizationMethodAfterAdvice <MethodAuthorizationContext > authorizationMethodAfterAdvice ) {
216
- this .authorizationMethodAfterAdvice = authorizationMethodAfterAdvice ;
165
+ return AuthorizationMethodInterceptors .postAuthorize (authorizationManager );
217
166
}
218
167
219
168
@ Override
@@ -227,7 +176,7 @@ public void afterPropertiesSet() throws Exception {
227
176
if (!securedEnabled () && !jsr250Enabled ()) {
228
177
return ;
229
178
}
230
- Assert .isNull (this .authorizationMethodBeforeAdvice ,
179
+ Assert .isNull (this .interceptor ,
231
180
"You have specified your own advice, meaning that the annotation attributes securedEnabled and jsr250Enabled will be ignored. Please choose one or the other." );
232
181
}
233
182
@@ -243,9 +192,4 @@ private int order() {
243
192
return this .enableMethodSecurity .getNumber ("order" );
244
193
}
245
194
246
- private Pointcut forAnnotation (Class <? extends Annotation > annotationClass ) {
247
- return Pointcuts .union (new AnnotationMatchingPointcut (annotationClass , true ),
248
- new AnnotationMatchingPointcut (null , annotationClass , true ));
249
- }
250
-
251
195
}
0 commit comments