20
20
import java .lang .reflect .Method ;
21
21
import java .util .Collection ;
22
22
import java .util .HashSet ;
23
+ import java .util .List ;
23
24
import java .util .Set ;
24
25
import java .util .function .Supplier ;
25
26
29
30
import org .aopalliance .intercept .MethodInvocation ;
30
31
31
32
import org .springframework .aop .support .AopUtils ;
32
- import org .springframework .core .annotation .AnnotationConfigurationException ;
33
33
import org .springframework .lang .NonNull ;
34
34
import org .springframework .security .authorization .AuthoritiesAuthorizationManager ;
35
35
import org .springframework .security .authorization .AuthorizationDecision ;
36
36
import org .springframework .security .authorization .AuthorizationManager ;
37
37
import org .springframework .security .core .Authentication ;
38
+ import org .springframework .security .core .annotation .AnnotationSynthesizer ;
39
+ import org .springframework .security .core .annotation .AnnotationSynthesizers ;
38
40
import org .springframework .util .Assert ;
39
41
40
42
/**
49
51
*/
50
52
public final class Jsr250AuthorizationManager implements AuthorizationManager <MethodInvocation > {
51
53
52
- private static final Set <Class <? extends Annotation >> JSR250_ANNOTATIONS = new HashSet <>();
53
-
54
- static {
55
- JSR250_ANNOTATIONS .add (DenyAll .class );
56
- JSR250_ANNOTATIONS .add (PermitAll .class );
57
- JSR250_ANNOTATIONS .add (RolesAllowed .class );
58
- }
59
-
60
54
private final Jsr250AuthorizationManagerRegistry registry = new Jsr250AuthorizationManagerRegistry ();
61
55
62
56
private AuthorizationManager <Collection <String >> authoritiesAuthorizationManager = new AuthoritiesAuthorizationManager ();
@@ -102,6 +96,9 @@ public AuthorizationDecision check(Supplier<Authentication> authentication, Meth
102
96
103
97
private final class Jsr250AuthorizationManagerRegistry extends AbstractAuthorizationManagerRegistry {
104
98
99
+ private final AnnotationSynthesizer <?> synthesizer = AnnotationSynthesizers
100
+ .requireUnique (List .of (DenyAll .class , PermitAll .class , RolesAllowed .class ));
101
+
105
102
@ NonNull
106
103
@ Override
107
104
AuthorizationManager <MethodInvocation > resolveManager (Method method , Class <?> targetClass ) {
@@ -121,45 +118,8 @@ AuthorizationManager<MethodInvocation> resolveManager(Method method, Class<?> ta
121
118
122
119
private Annotation findJsr250Annotation (Method method , Class <?> targetClass ) {
123
120
Method specificMethod = AopUtils .getMostSpecificMethod (method , targetClass );
124
- Annotation annotation = findAnnotation (specificMethod );
125
- return (annotation != null ) ? annotation
126
- : findAnnotation ((targetClass != null ) ? targetClass : specificMethod .getDeclaringClass ());
127
- }
128
-
129
- private Annotation findAnnotation (Method method ) {
130
- Set <Annotation > annotations = new HashSet <>();
131
- for (Class <? extends Annotation > annotationClass : JSR250_ANNOTATIONS ) {
132
- Annotation annotation = AuthorizationAnnotationUtils .findUniqueAnnotation (method , annotationClass );
133
- if (annotation != null ) {
134
- annotations .add (annotation );
135
- }
136
- }
137
- if (annotations .isEmpty ()) {
138
- return null ;
139
- }
140
- if (annotations .size () > 1 ) {
141
- throw new AnnotationConfigurationException (
142
- "The JSR-250 specification disallows DenyAll, PermitAll, and RolesAllowed from appearing on the same method." );
143
- }
144
- return annotations .iterator ().next ();
145
- }
146
-
147
- private Annotation findAnnotation (Class <?> clazz ) {
148
- Set <Annotation > annotations = new HashSet <>();
149
- for (Class <? extends Annotation > annotationClass : JSR250_ANNOTATIONS ) {
150
- Annotation annotation = AuthorizationAnnotationUtils .findUniqueAnnotation (clazz , annotationClass );
151
- if (annotation != null ) {
152
- annotations .add (annotation );
153
- }
154
- }
155
- if (annotations .isEmpty ()) {
156
- return null ;
157
- }
158
- if (annotations .size () > 1 ) {
159
- throw new AnnotationConfigurationException (
160
- "The JSR-250 specification disallows DenyAll, PermitAll, and RolesAllowed from appearing on the same class definition." );
161
- }
162
- return annotations .iterator ().next ();
121
+ Class <?> targetClassToUse = (targetClass != null ) ? targetClass : specificMethod .getDeclaringClass ();
122
+ return this .synthesizer .synthesize (specificMethod , targetClassToUse );
163
123
}
164
124
165
125
private Set <String > getAllowedRolesWithPrefix (RolesAllowed rolesAllowed ) {
0 commit comments