1
1
/*
2
- * Copyright 2002-2024 the original author or authors.
2
+ * Copyright 2002-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
17
17
package org .springframework .security .authorization .method ;
18
18
19
19
import java .lang .reflect .Method ;
20
- import java .util .Arrays ;
21
- import java .util .function .BiFunction ;
22
20
23
21
import reactor .util .annotation .NonNull ;
24
22
28
26
import org .springframework .security .core .annotation .AnnotationTemplateExpressionDefaults ;
29
27
import org .springframework .security .core .annotation .SecurityAnnotationScanner ;
30
28
import org .springframework .security .core .annotation .SecurityAnnotationScanners ;
31
- import org .springframework .util .Assert ;
32
- import org .springframework .util .StringUtils ;
33
29
34
30
/**
35
31
* For internal use only, as this contract is likely to change.
40
36
*/
41
37
final class PostAuthorizeExpressionAttributeRegistry extends AbstractExpressionAttributeRegistry <ExpressionAttribute > {
42
38
43
- private final MethodAuthorizationDeniedHandler defaultHandler = new ThrowingMethodAuthorizationDeniedHandler ();
44
-
45
- private final SecurityAnnotationScanner <HandleAuthorizationDenied > handleAuthorizationDeniedScanner = SecurityAnnotationScanners
46
- .requireUnique (HandleAuthorizationDenied .class );
47
-
48
- private BiFunction <String , Class <? extends MethodAuthorizationDeniedHandler >, MethodAuthorizationDeniedHandler > handlerResolver ;
39
+ private final MethodAuthorizationDeniedHandlerResolver handlerResolver = new MethodAuthorizationDeniedHandlerResolver (
40
+ PostAuthorizeAuthorizationManager .class );
49
41
50
42
private SecurityAnnotationScanner <PostAuthorize > postAuthorizeScanner = SecurityAnnotationScanners
51
43
.requireUnique (PostAuthorize .class );
52
44
53
- PostAuthorizeExpressionAttributeRegistry () {
54
- this .handlerResolver = (beanName , clazz ) -> new ReflectiveMethodAuthorizationDeniedHandler (clazz ,
55
- PostAuthorizeAuthorizationManager .class );
56
- }
57
-
58
45
@ NonNull
59
46
@ Override
60
47
ExpressionAttribute resolveAttribute (Method method , Class <?> targetClass ) {
@@ -63,19 +50,11 @@ ExpressionAttribute resolveAttribute(Method method, Class<?> targetClass) {
63
50
return ExpressionAttribute .NULL_ATTRIBUTE ;
64
51
}
65
52
Expression expression = getExpressionHandler ().getExpressionParser ().parseExpression (postAuthorize .value ());
66
- MethodAuthorizationDeniedHandler deniedHandler = resolveHandler (method , targetClass );
53
+ MethodAuthorizationDeniedHandler deniedHandler = this .handlerResolver .resolve (method ,
54
+ targetClass (method , targetClass ), PostAuthorizeAuthorizationManager .class );
67
55
return new PostAuthorizeExpressionAttribute (expression , deniedHandler );
68
56
}
69
57
70
- private MethodAuthorizationDeniedHandler resolveHandler (Method method , Class <?> targetClass ) {
71
- Class <?> targetClassToUse = targetClass (method , targetClass );
72
- HandleAuthorizationDenied deniedHandler = this .handleAuthorizationDeniedScanner .scan (method , targetClassToUse );
73
- if (deniedHandler != null ) {
74
- return this .handlerResolver .apply (deniedHandler .handler (), deniedHandler .handlerClass ());
75
- }
76
- return this .defaultHandler ;
77
- }
78
-
79
58
private PostAuthorize findPostAuthorizeAnnotation (Method method , Class <?> targetClass ) {
80
59
Class <?> targetClassToUse = targetClass (method , targetClass );
81
60
return this .postAuthorizeScanner .scan (method , targetClassToUse );
@@ -87,31 +66,11 @@ private PostAuthorize findPostAuthorizeAnnotation(Method method, Class<?> target
87
66
* @param context the {@link ApplicationContext} to use
88
67
*/
89
68
void setApplicationContext (ApplicationContext context ) {
90
- Assert .notNull (context , "context cannot be null" );
91
- this .handlerResolver = (beanName , clazz ) -> resolveHandler (context , beanName , clazz );
69
+ this .handlerResolver .setContext (context );
92
70
}
93
71
94
72
void setTemplateDefaults (AnnotationTemplateExpressionDefaults templateDefaults ) {
95
73
this .postAuthorizeScanner = SecurityAnnotationScanners .requireUnique (PostAuthorize .class , templateDefaults );
96
74
}
97
75
98
- private MethodAuthorizationDeniedHandler resolveHandler (ApplicationContext context , String beanName ,
99
- Class <? extends MethodAuthorizationDeniedHandler > handlerClass ) {
100
- if (StringUtils .hasText (beanName )) {
101
- return context .getBean (beanName , MethodAuthorizationDeniedHandler .class );
102
- }
103
- if (handlerClass == this .defaultHandler .getClass ()) {
104
- return this .defaultHandler ;
105
- }
106
- String [] beanNames = context .getBeanNamesForType (handlerClass );
107
- if (beanNames .length == 0 ) {
108
- throw new IllegalStateException ("Could not find a bean of type " + handlerClass .getName ());
109
- }
110
- if (beanNames .length > 1 ) {
111
- throw new IllegalStateException ("Expected to find a single bean of type " + handlerClass .getName ()
112
- + " but found " + Arrays .toString (beanNames ));
113
- }
114
- return context .getBean (beanNames [0 ], handlerClass );
115
- }
116
-
117
76
}
0 commit comments