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 org .springframework .context .ApplicationContext ;
24
22
import org .springframework .expression .Expression ;
27
25
import org .springframework .security .core .annotation .AnnotationTemplateExpressionDefaults ;
28
26
import org .springframework .security .core .annotation .SecurityAnnotationScanner ;
29
27
import org .springframework .security .core .annotation .SecurityAnnotationScanners ;
30
- import org .springframework .util .Assert ;
31
- import org .springframework .util .StringUtils ;
32
28
33
29
/**
34
30
* For internal use only, as this contract is likely to change.
39
35
*/
40
36
final class PreAuthorizeExpressionAttributeRegistry extends AbstractExpressionAttributeRegistry <ExpressionAttribute > {
41
37
42
- private final MethodAuthorizationDeniedHandler defaultHandler = new ThrowingMethodAuthorizationDeniedHandler ();
43
-
44
- private final SecurityAnnotationScanner <HandleAuthorizationDenied > handleAuthorizationDeniedScanner = SecurityAnnotationScanners
45
- .requireUnique (HandleAuthorizationDenied .class );
46
-
47
- private BiFunction <String , Class <? extends MethodAuthorizationDeniedHandler >, MethodAuthorizationDeniedHandler > handlerResolver ;
38
+ private final MethodAuthorizationDeniedHandlerResolver handlerResolver = new MethodAuthorizationDeniedHandlerResolver ();
48
39
49
40
private SecurityAnnotationScanner <PreAuthorize > preAuthorizeScanner = SecurityAnnotationScanners
50
41
.requireUnique (PreAuthorize .class );
51
42
52
- PreAuthorizeExpressionAttributeRegistry () {
53
- this .handlerResolver = (beanName , clazz ) -> new ReflectiveMethodAuthorizationDeniedHandler (clazz ,
54
- PreAuthorizeAuthorizationManager .class );
55
- }
56
-
57
43
@ NonNull
58
44
@ Override
59
45
ExpressionAttribute resolveAttribute (Method method , Class <?> targetClass ) {
@@ -62,19 +48,11 @@ ExpressionAttribute resolveAttribute(Method method, Class<?> targetClass) {
62
48
return ExpressionAttribute .NULL_ATTRIBUTE ;
63
49
}
64
50
Expression expression = getExpressionHandler ().getExpressionParser ().parseExpression (preAuthorize .value ());
65
- MethodAuthorizationDeniedHandler handler = resolveHandler (method , targetClass );
51
+ MethodAuthorizationDeniedHandler handler = this .handlerResolver .resolve (method ,
52
+ targetClass (method , targetClass ), PreAuthorizeAuthorizationManager .class );
66
53
return new PreAuthorizeExpressionAttribute (expression , handler );
67
54
}
68
55
69
- private MethodAuthorizationDeniedHandler resolveHandler (Method method , Class <?> targetClass ) {
70
- Class <?> targetClassToUse = targetClass (method , targetClass );
71
- HandleAuthorizationDenied deniedHandler = this .handleAuthorizationDeniedScanner .scan (method , targetClassToUse );
72
- if (deniedHandler != null ) {
73
- return this .handlerResolver .apply (deniedHandler .handler (), deniedHandler .handlerClass ());
74
- }
75
- return this .defaultHandler ;
76
- }
77
-
78
56
private PreAuthorize findPreAuthorizeAnnotation (Method method , Class <?> targetClass ) {
79
57
Class <?> targetClassToUse = targetClass (method , targetClass );
80
58
return this .preAuthorizeScanner .scan (method , targetClassToUse );
@@ -86,31 +64,11 @@ private PreAuthorize findPreAuthorizeAnnotation(Method method, Class<?> targetCl
86
64
* @param context the {@link ApplicationContext} to use
87
65
*/
88
66
void setApplicationContext (ApplicationContext context ) {
89
- Assert .notNull (context , "context cannot be null" );
90
- this .handlerResolver = (beanName , clazz ) -> resolveHandler (context , beanName , clazz );
67
+ this .handlerResolver .setContext (context );
91
68
}
92
69
93
70
void setTemplateDefaults (AnnotationTemplateExpressionDefaults defaults ) {
94
71
this .preAuthorizeScanner = SecurityAnnotationScanners .requireUnique (PreAuthorize .class , defaults );
95
72
}
96
73
97
- private MethodAuthorizationDeniedHandler resolveHandler (ApplicationContext context , String beanName ,
98
- Class <? extends MethodAuthorizationDeniedHandler > handlerClass ) {
99
- if (StringUtils .hasText (beanName )) {
100
- return context .getBean (beanName , MethodAuthorizationDeniedHandler .class );
101
- }
102
- if (handlerClass == this .defaultHandler .getClass ()) {
103
- return this .defaultHandler ;
104
- }
105
- String [] beanNames = context .getBeanNamesForType (handlerClass );
106
- if (beanNames .length == 0 ) {
107
- throw new IllegalStateException ("Could not find a bean of type " + handlerClass .getName ());
108
- }
109
- if (beanNames .length > 1 ) {
110
- throw new IllegalStateException ("Expected to find a single bean of type " + handlerClass .getName ()
111
- + " but found " + Arrays .toString (beanNames ));
112
- }
113
- return context .getBean (beanNames [0 ], handlerClass );
114
- }
115
-
116
74
}
0 commit comments