39
39
import org .springframework .security .access .prepost .PreAuthorize ;
40
40
import org .springframework .security .access .prepost .PreFilter ;
41
41
import org .springframework .security .authorization .AuthorizationResult ;
42
- import org .springframework .security .authorization .method .AuthorizationDeniedHandler ;
43
42
import org .springframework .security .authorization .method .AuthorizeReturnObject ;
43
+ import org .springframework .security .authorization .method .HandleAuthorizationDenied ;
44
44
import org .springframework .security .authorization .method .MethodAuthorizationDeniedHandler ;
45
- import org .springframework .security .authorization .method .MethodAuthorizationDeniedPostProcessor ;
46
45
import org .springframework .security .authorization .method .MethodInvocationResult ;
47
46
import org .springframework .security .core .Authentication ;
48
47
import org .springframework .security .core .context .SecurityContextHolder ;
@@ -129,73 +128,72 @@ public interface MethodSecurityService {
129
128
void repeatedAnnotations ();
130
129
131
130
@ PreAuthorize ("hasRole('ADMIN')" )
132
- @ AuthorizationDeniedHandler (handlerClass = StarMaskingHandler .class )
131
+ @ HandleAuthorizationDenied (handlerClass = StarMaskingHandler .class )
133
132
String preAuthorizeGetCardNumberIfAdmin (String cardNumber );
134
133
135
134
@ PreAuthorize ("hasRole('ADMIN')" )
136
- @ AuthorizationDeniedHandler (handlerClass = StartMaskingHandlerChild .class )
135
+ @ HandleAuthorizationDenied (handlerClass = StartMaskingHandlerChild .class )
137
136
String preAuthorizeWithHandlerChildGetCardNumberIfAdmin (String cardNumber );
138
137
139
138
@ PreAuthorize ("hasRole('ADMIN')" )
140
- @ AuthorizationDeniedHandler (handlerClass = StarMaskingHandler .class )
139
+ @ HandleAuthorizationDenied (handlerClass = StarMaskingHandler .class )
141
140
String preAuthorizeThrowAccessDeniedManually ();
142
141
143
142
@ PostAuthorize ("hasRole('ADMIN')" )
144
- @ AuthorizationDeniedHandler ( postProcessorClass = CardNumberMaskingPostProcessor .class )
143
+ @ HandleAuthorizationDenied ( handlerClass = CardNumberMaskingPostProcessor .class )
145
144
String postAuthorizeGetCardNumberIfAdmin (String cardNumber );
146
145
147
146
@ PostAuthorize ("hasRole('ADMIN')" )
148
- @ AuthorizationDeniedHandler ( postProcessorClass = PostMaskingPostProcessor .class )
147
+ @ HandleAuthorizationDenied ( handlerClass = PostMaskingPostProcessor .class )
149
148
String postAuthorizeThrowAccessDeniedManually ();
150
149
151
150
@ PreAuthorize ("denyAll()" )
152
151
@ Mask ("methodmask" )
153
- @ AuthorizationDeniedHandler (handlerClass = MaskAnnotationHandler .class )
152
+ @ HandleAuthorizationDenied (handlerClass = MaskAnnotationHandler .class )
154
153
String preAuthorizeDeniedMethodWithMaskAnnotation ();
155
154
156
155
@ PreAuthorize ("denyAll()" )
157
- @ AuthorizationDeniedHandler (handlerClass = MaskAnnotationHandler .class )
156
+ @ HandleAuthorizationDenied (handlerClass = MaskAnnotationHandler .class )
158
157
String preAuthorizeDeniedMethodWithNoMaskAnnotation ();
159
158
160
159
@ NullDenied (role = "ADMIN" )
161
160
String postAuthorizeDeniedWithNullDenied ();
162
161
163
162
@ PostAuthorize ("denyAll()" )
164
163
@ Mask ("methodmask" )
165
- @ AuthorizationDeniedHandler ( postProcessorClass = MaskAnnotationPostProcessor .class )
164
+ @ HandleAuthorizationDenied ( handlerClass = MaskAnnotationPostProcessor .class )
166
165
String postAuthorizeDeniedMethodWithMaskAnnotation ();
167
166
168
167
@ PostAuthorize ("denyAll()" )
169
- @ AuthorizationDeniedHandler ( postProcessorClass = MaskAnnotationPostProcessor .class )
168
+ @ HandleAuthorizationDenied ( handlerClass = MaskAnnotationPostProcessor .class )
170
169
String postAuthorizeDeniedMethodWithNoMaskAnnotation ();
171
170
172
171
@ PreAuthorize ("hasRole('ADMIN')" )
173
172
@ Mask (expression = "@myMasker.getMask()" )
174
- @ AuthorizationDeniedHandler (handlerClass = MaskAnnotationHandler .class )
173
+ @ HandleAuthorizationDenied (handlerClass = MaskAnnotationHandler .class )
175
174
String preAuthorizeWithMaskAnnotationUsingBean ();
176
175
177
176
@ PostAuthorize ("hasRole('ADMIN')" )
178
177
@ Mask (expression = "@myMasker.getMask(returnObject)" )
179
- @ AuthorizationDeniedHandler ( postProcessorClass = MaskAnnotationPostProcessor .class )
178
+ @ HandleAuthorizationDenied ( handlerClass = MaskAnnotationPostProcessor .class )
180
179
String postAuthorizeWithMaskAnnotationUsingBean ();
181
180
182
181
@ AuthorizeReturnObject
183
182
UserRecordWithEmailProtected getUserRecordWithEmailProtected ();
184
183
185
184
@ PreAuthorize ("hasRole('ADMIN')" )
186
- @ AuthorizationDeniedHandler (handlerClass = UserFallbackDeniedHandler .class )
185
+ @ HandleAuthorizationDenied (handlerClass = UserFallbackDeniedHandler .class )
187
186
UserRecordWithEmailProtected getUserWithFallbackWhenUnauthorized ();
188
187
189
188
@ PreAuthorize ("@authz.checkResult(#result)" )
190
189
@ PostAuthorize ("@authz.checkResult(!#result)" )
191
- @ AuthorizationDeniedHandler (handlerClass = MethodAuthorizationDeniedHandler .class ,
192
- postProcessorClass = MethodAuthorizationDeniedPostProcessor .class )
190
+ @ HandleAuthorizationDenied (handlerClass = MethodAuthorizationDeniedHandler .class )
193
191
String checkCustomResult (boolean result );
194
192
195
193
class StarMaskingHandler implements MethodAuthorizationDeniedHandler {
196
194
197
195
@ Override
198
- public Object handle (MethodInvocation methodInvocation , AuthorizationResult result ) {
196
+ public Object handleDeniedInvocation (MethodInvocation methodInvocation , AuthorizationResult result ) {
199
197
return "***" ;
200
198
}
201
199
@@ -204,8 +202,8 @@ public Object handle(MethodInvocation methodInvocation, AuthorizationResult resu
204
202
class StartMaskingHandlerChild extends StarMaskingHandler {
205
203
206
204
@ Override
207
- public Object handle (MethodInvocation methodInvocation , AuthorizationResult result ) {
208
- return super .handle (methodInvocation , result ) + "-child" ;
205
+ public Object handleDeniedInvocation (MethodInvocation methodInvocation , AuthorizationResult result ) {
206
+ return super .handleDeniedInvocation (methodInvocation , result ) + "-child" ;
209
207
}
210
208
211
209
}
@@ -218,7 +216,6 @@ class MaskAnnotationHandler implements MethodAuthorizationDeniedHandler {
218
216
this .maskValueResolver = new MaskValueResolver (context );
219
217
}
220
218
221
- @ Override
222
219
public Object handle (MethodInvocation methodInvocation , AuthorizationResult result ) {
223
220
Mask mask = AnnotationUtils .getAnnotation (methodInvocation .getMethod (), Mask .class );
224
221
if (mask == null ) {
@@ -227,9 +224,15 @@ public Object handle(MethodInvocation methodInvocation, AuthorizationResult resu
227
224
return this .maskValueResolver .resolveValue (mask , methodInvocation , null );
228
225
}
229
226
227
+ @ Override
228
+ public Object handleDeniedInvocation (MethodInvocation methodInvocation ,
229
+ AuthorizationResult authorizationResult ) {
230
+ return handle (methodInvocation , authorizationResult );
231
+ }
232
+
230
233
}
231
234
232
- class MaskAnnotationPostProcessor implements MethodAuthorizationDeniedPostProcessor {
235
+ class MaskAnnotationPostProcessor implements MethodAuthorizationDeniedHandler {
233
236
234
237
MaskValueResolver maskValueResolver ;
235
238
@@ -238,7 +241,16 @@ class MaskAnnotationPostProcessor implements MethodAuthorizationDeniedPostProces
238
241
}
239
242
240
243
@ Override
241
- public Object postProcessResult (MethodInvocationResult methodInvocationResult ,
244
+ public Object handleDeniedInvocation (MethodInvocation mi , AuthorizationResult authorizationResult ) {
245
+ Mask mask = AnnotationUtils .getAnnotation (mi .getMethod (), Mask .class );
246
+ if (mask == null ) {
247
+ mask = AnnotationUtils .getAnnotation (mi .getMethod ().getDeclaringClass (), Mask .class );
248
+ }
249
+ return this .maskValueResolver .resolveValue (mask , mi , null );
250
+ }
251
+
252
+ @ Override
253
+ public Object handleDeniedInvocationResult (MethodInvocationResult methodInvocationResult ,
242
254
AuthorizationResult authorizationResult ) {
243
255
MethodInvocation mi = methodInvocationResult .getMethodInvocation ();
244
256
Mask mask = AnnotationUtils .getAnnotation (mi .getMethod (), Mask .class );
@@ -274,31 +286,38 @@ String resolveValue(Mask mask, MethodInvocation mi, Object returnObject) {
274
286
275
287
}
276
288
277
- class PostMaskingPostProcessor implements MethodAuthorizationDeniedPostProcessor {
289
+ class PostMaskingPostProcessor implements MethodAuthorizationDeniedHandler {
278
290
279
291
@ Override
280
- public Object postProcessResult (MethodInvocationResult contextObject , AuthorizationResult result ) {
292
+ public Object handleDeniedInvocation (MethodInvocation methodInvocation ,
293
+ AuthorizationResult authorizationResult ) {
281
294
return "***" ;
282
295
}
283
296
284
297
}
285
298
286
- class CardNumberMaskingPostProcessor implements MethodAuthorizationDeniedPostProcessor {
299
+ class CardNumberMaskingPostProcessor implements MethodAuthorizationDeniedHandler {
287
300
288
301
static String MASK = "****-****-****-" ;
289
302
290
303
@ Override
291
- public Object postProcessResult (MethodInvocationResult contextObject , AuthorizationResult result ) {
304
+ public Object handleDeniedInvocation (MethodInvocation methodInvocation ,
305
+ AuthorizationResult authorizationResult ) {
306
+ return "***" ;
307
+ }
308
+
309
+ @ Override
310
+ public Object handleDeniedInvocationResult (MethodInvocationResult contextObject , AuthorizationResult result ) {
292
311
String cardNumber = (String ) contextObject .getResult ();
293
312
return MASK + cardNumber .substring (cardNumber .length () - 4 );
294
313
}
295
314
296
315
}
297
316
298
- class NullPostProcessor implements MethodAuthorizationDeniedPostProcessor {
317
+ class NullPostProcessor implements MethodAuthorizationDeniedHandler {
299
318
300
319
@ Override
301
- public Object postProcessResult ( MethodInvocationResult methodInvocationResult ,
320
+ public Object handleDeniedInvocation ( MethodInvocation methodInvocation ,
302
321
AuthorizationResult authorizationResult ) {
303
322
return null ;
304
323
}
@@ -320,7 +339,7 @@ public Object postProcessResult(MethodInvocationResult methodInvocationResult,
320
339
@ Retention (RetentionPolicy .RUNTIME )
321
340
@ Inherited
322
341
@ PostAuthorize ("hasRole('{role}')" )
323
- @ AuthorizationDeniedHandler ( postProcessorClass = NullPostProcessor .class )
342
+ @ HandleAuthorizationDenied ( handlerClass = NullPostProcessor .class )
324
343
@interface NullDenied {
325
344
326
345
String role ();
@@ -333,7 +352,8 @@ class UserFallbackDeniedHandler implements MethodAuthorizationDeniedHandler {
333
352
"Protected" );
334
353
335
354
@ Override
336
- public Object handle (MethodInvocation methodInvocation , AuthorizationResult authorizationResult ) {
355
+ public Object handleDeniedInvocation (MethodInvocation methodInvocation ,
356
+ AuthorizationResult authorizationResult ) {
337
357
return FALLBACK ;
338
358
}
339
359
0 commit comments