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 ;
42
+ import org .springframework .security .authorization .method .HandleAuthorizationDenied ;
43
43
import org .springframework .security .authorization .method .AuthorizeReturnObject ;
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,21 @@ 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
+
233
+ @ Override
234
+ public Object handleDeniedInvocationResult (MethodInvocationResult methodInvocationResult ,
235
+ AuthorizationResult authorizationResult ) {
236
+ return handle (methodInvocationResult .getMethodInvocation (), authorizationResult );
237
+ }
238
+
230
239
}
231
240
232
- class MaskAnnotationPostProcessor implements MethodAuthorizationDeniedPostProcessor {
241
+ class MaskAnnotationPostProcessor implements MethodAuthorizationDeniedHandler {
233
242
234
243
MaskValueResolver maskValueResolver ;
235
244
@@ -238,7 +247,16 @@ class MaskAnnotationPostProcessor implements MethodAuthorizationDeniedPostProces
238
247
}
239
248
240
249
@ Override
241
- public Object postProcessResult (MethodInvocationResult methodInvocationResult ,
250
+ public Object handleDeniedInvocation (MethodInvocation mi , AuthorizationResult authorizationResult ) {
251
+ Mask mask = AnnotationUtils .getAnnotation (mi .getMethod (), Mask .class );
252
+ if (mask == null ) {
253
+ mask = AnnotationUtils .getAnnotation (mi .getMethod ().getDeclaringClass (), Mask .class );
254
+ }
255
+ return this .maskValueResolver .resolveValue (mask , mi , null );
256
+ }
257
+
258
+ @ Override
259
+ public Object handleDeniedInvocationResult (MethodInvocationResult methodInvocationResult ,
242
260
AuthorizationResult authorizationResult ) {
243
261
MethodInvocation mi = methodInvocationResult .getMethodInvocation ();
244
262
Mask mask = AnnotationUtils .getAnnotation (mi .getMethod (), Mask .class );
@@ -274,31 +292,49 @@ String resolveValue(Mask mask, MethodInvocation mi, Object returnObject) {
274
292
275
293
}
276
294
277
- class PostMaskingPostProcessor implements MethodAuthorizationDeniedPostProcessor {
295
+ class PostMaskingPostProcessor implements MethodAuthorizationDeniedHandler {
278
296
279
297
@ Override
280
- public Object postProcessResult (MethodInvocationResult contextObject , AuthorizationResult result ) {
298
+ public Object handleDeniedInvocation (MethodInvocation methodInvocation ,
299
+ AuthorizationResult authorizationResult ) {
300
+ return "***" ;
301
+ }
302
+
303
+ @ Override
304
+ public Object handleDeniedInvocationResult (MethodInvocationResult contextObject , AuthorizationResult result ) {
281
305
return "***" ;
282
306
}
283
307
284
308
}
285
309
286
- class CardNumberMaskingPostProcessor implements MethodAuthorizationDeniedPostProcessor {
310
+ class CardNumberMaskingPostProcessor implements MethodAuthorizationDeniedHandler {
287
311
288
312
static String MASK = "****-****-****-" ;
289
313
290
314
@ Override
291
- public Object postProcessResult (MethodInvocationResult contextObject , AuthorizationResult result ) {
315
+ public Object handleDeniedInvocation (MethodInvocation methodInvocation ,
316
+ AuthorizationResult authorizationResult ) {
317
+ return "***" ;
318
+ }
319
+
320
+ @ Override
321
+ public Object handleDeniedInvocationResult (MethodInvocationResult contextObject , AuthorizationResult result ) {
292
322
String cardNumber = (String ) contextObject .getResult ();
293
323
return MASK + cardNumber .substring (cardNumber .length () - 4 );
294
324
}
295
325
296
326
}
297
327
298
- class NullPostProcessor implements MethodAuthorizationDeniedPostProcessor {
328
+ class NullPostProcessor implements MethodAuthorizationDeniedHandler {
329
+
330
+ @ Override
331
+ public Object handleDeniedInvocation (MethodInvocation methodInvocation ,
332
+ AuthorizationResult authorizationResult ) {
333
+ return null ;
334
+ }
299
335
300
336
@ Override
301
- public Object postProcessResult (MethodInvocationResult methodInvocationResult ,
337
+ public Object handleDeniedInvocationResult (MethodInvocationResult methodInvocationResult ,
302
338
AuthorizationResult authorizationResult ) {
303
339
return null ;
304
340
}
@@ -320,7 +356,7 @@ public Object postProcessResult(MethodInvocationResult methodInvocationResult,
320
356
@ Retention (RetentionPolicy .RUNTIME )
321
357
@ Inherited
322
358
@ PostAuthorize ("hasRole('{role}')" )
323
- @ AuthorizationDeniedHandler ( postProcessorClass = NullPostProcessor .class )
359
+ @ HandleAuthorizationDenied ( handlerClass = NullPostProcessor .class )
324
360
@interface NullDenied {
325
361
326
362
String role ();
@@ -333,7 +369,8 @@ class UserFallbackDeniedHandler implements MethodAuthorizationDeniedHandler {
333
369
"Protected" );
334
370
335
371
@ Override
336
- public Object handle (MethodInvocation methodInvocation , AuthorizationResult authorizationResult ) {
372
+ public Object handleDeniedInvocation (MethodInvocation methodInvocation ,
373
+ AuthorizationResult authorizationResult ) {
337
374
return FALLBACK ;
338
375
}
339
376
0 commit comments