Skip to content

Commit 9a45c43

Browse files
committed
Polish
Issue spring-projectsgh-16622 Signed-off-by: Evgeniy Cheban <[email protected]>
1 parent 6930987 commit 9a45c43

File tree

3 files changed

+86
-92
lines changed

3 files changed

+86
-92
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2002-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.authorization.method;
18+
19+
import java.lang.reflect.Method;
20+
import java.util.Arrays;
21+
22+
import org.springframework.context.ApplicationContext;
23+
import org.springframework.security.core.annotation.SecurityAnnotationScanner;
24+
import org.springframework.security.core.annotation.SecurityAnnotationScanners;
25+
import org.springframework.util.Assert;
26+
import org.springframework.util.StringUtils;
27+
28+
/**
29+
* For internal use only, as this contract is likely to change.
30+
*
31+
* @author Evgeniy Cheban
32+
*/
33+
final class MethodAuthorizationDeniedHandlerResolver {
34+
35+
private final MethodAuthorizationDeniedHandler defaultHandler = new ThrowingMethodAuthorizationDeniedHandler();
36+
37+
private final SecurityAnnotationScanner<HandleAuthorizationDenied> handleAuthorizationDeniedScanner = SecurityAnnotationScanners
38+
.requireUnique(HandleAuthorizationDenied.class);
39+
40+
private ApplicationContext context;
41+
42+
void setContext(ApplicationContext context) {
43+
Assert.notNull(context, "context cannot be null");
44+
this.context = context;
45+
}
46+
47+
MethodAuthorizationDeniedHandler resolve(Method method, Class<?> targetClass, Class<?> managerClass) {
48+
HandleAuthorizationDenied deniedHandler = this.handleAuthorizationDeniedScanner.scan(method, targetClass);
49+
if (deniedHandler != null) {
50+
return doResolve(deniedHandler.handler(), deniedHandler.handlerClass(), managerClass);
51+
}
52+
return this.defaultHandler;
53+
}
54+
55+
private MethodAuthorizationDeniedHandler doResolve(String beanName,
56+
Class<? extends MethodAuthorizationDeniedHandler> handlerClass, Class<?> managerClass) {
57+
if (this.context == null) {
58+
return new ReflectiveMethodAuthorizationDeniedHandler(handlerClass, managerClass);
59+
}
60+
if (StringUtils.hasText(beanName)) {
61+
return this.context.getBean(beanName, MethodAuthorizationDeniedHandler.class);
62+
}
63+
if (handlerClass == this.defaultHandler.getClass()) {
64+
return this.defaultHandler;
65+
}
66+
String[] beanNames = this.context.getBeanNamesForType(handlerClass);
67+
if (beanNames.length == 0) {
68+
throw new IllegalStateException("Could not find a bean of type " + handlerClass.getName());
69+
}
70+
if (beanNames.length > 1) {
71+
throw new IllegalStateException("Expected to find a single bean of type " + handlerClass.getName()
72+
+ " but found " + Arrays.toString(beanNames)
73+
+ " consider using 'handler' attribute to refer to specific bean");
74+
}
75+
return this.context.getBean(beanNames[0], handlerClass);
76+
}
77+
78+
}

Diff for: core/src/main/java/org/springframework/security/authorization/method/PostAuthorizeExpressionAttributeRegistry.java

+4-46
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
package org.springframework.security.authorization.method;
1818

1919
import java.lang.reflect.Method;
20-
import java.util.Arrays;
21-
import java.util.function.BiFunction;
2220

2321
import reactor.util.annotation.NonNull;
2422

@@ -28,8 +26,6 @@
2826
import org.springframework.security.core.annotation.AnnotationTemplateExpressionDefaults;
2927
import org.springframework.security.core.annotation.SecurityAnnotationScanner;
3028
import org.springframework.security.core.annotation.SecurityAnnotationScanners;
31-
import org.springframework.util.Assert;
32-
import org.springframework.util.StringUtils;
3329

3430
/**
3531
* For internal use only, as this contract is likely to change.
@@ -40,21 +36,11 @@
4036
*/
4137
final class PostAuthorizeExpressionAttributeRegistry extends AbstractExpressionAttributeRegistry<ExpressionAttribute> {
4238

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();
4940

5041
private SecurityAnnotationScanner<PostAuthorize> postAuthorizeScanner = SecurityAnnotationScanners
5142
.requireUnique(PostAuthorize.class);
5243

53-
PostAuthorizeExpressionAttributeRegistry() {
54-
this.handlerResolver = (beanName, clazz) -> new ReflectiveMethodAuthorizationDeniedHandler(clazz,
55-
PostAuthorizeAuthorizationManager.class);
56-
}
57-
5844
@NonNull
5945
@Override
6046
ExpressionAttribute resolveAttribute(Method method, Class<?> targetClass) {
@@ -63,19 +49,11 @@ ExpressionAttribute resolveAttribute(Method method, Class<?> targetClass) {
6349
return ExpressionAttribute.NULL_ATTRIBUTE;
6450
}
6551
Expression expression = getExpressionHandler().getExpressionParser().parseExpression(postAuthorize.value());
66-
MethodAuthorizationDeniedHandler deniedHandler = resolveHandler(method, targetClass);
52+
MethodAuthorizationDeniedHandler deniedHandler = this.handlerResolver.resolve(method,
53+
targetClass(method, targetClass), PostAuthorizeAuthorizationManager.class);
6754
return new PostAuthorizeExpressionAttribute(expression, deniedHandler);
6855
}
6956

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-
7957
private PostAuthorize findPostAuthorizeAnnotation(Method method, Class<?> targetClass) {
8058
Class<?> targetClassToUse = targetClass(method, targetClass);
8159
return this.postAuthorizeScanner.scan(method, targetClassToUse);
@@ -87,31 +65,11 @@ private PostAuthorize findPostAuthorizeAnnotation(Method method, Class<?> target
8765
* @param context the {@link ApplicationContext} to use
8866
*/
8967
void setApplicationContext(ApplicationContext context) {
90-
Assert.notNull(context, "context cannot be null");
91-
this.handlerResolver = (beanName, clazz) -> resolveHandler(context, beanName, clazz);
68+
this.handlerResolver.setContext(context);
9269
}
9370

9471
void setTemplateDefaults(AnnotationTemplateExpressionDefaults templateDefaults) {
9572
this.postAuthorizeScanner = SecurityAnnotationScanners.requireUnique(PostAuthorize.class, templateDefaults);
9673
}
9774

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-
11775
}

Diff for: core/src/main/java/org/springframework/security/authorization/method/PreAuthorizeExpressionAttributeRegistry.java

+4-46
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
package org.springframework.security.authorization.method;
1818

1919
import java.lang.reflect.Method;
20-
import java.util.Arrays;
21-
import java.util.function.BiFunction;
2220

2321
import org.springframework.context.ApplicationContext;
2422
import org.springframework.expression.Expression;
@@ -27,8 +25,6 @@
2725
import org.springframework.security.core.annotation.AnnotationTemplateExpressionDefaults;
2826
import org.springframework.security.core.annotation.SecurityAnnotationScanner;
2927
import org.springframework.security.core.annotation.SecurityAnnotationScanners;
30-
import org.springframework.util.Assert;
31-
import org.springframework.util.StringUtils;
3228

3329
/**
3430
* For internal use only, as this contract is likely to change.
@@ -39,21 +35,11 @@
3935
*/
4036
final class PreAuthorizeExpressionAttributeRegistry extends AbstractExpressionAttributeRegistry<ExpressionAttribute> {
4137

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();
4839

4940
private SecurityAnnotationScanner<PreAuthorize> preAuthorizeScanner = SecurityAnnotationScanners
5041
.requireUnique(PreAuthorize.class);
5142

52-
PreAuthorizeExpressionAttributeRegistry() {
53-
this.handlerResolver = (beanName, clazz) -> new ReflectiveMethodAuthorizationDeniedHandler(clazz,
54-
PreAuthorizeAuthorizationManager.class);
55-
}
56-
5743
@NonNull
5844
@Override
5945
ExpressionAttribute resolveAttribute(Method method, Class<?> targetClass) {
@@ -62,19 +48,11 @@ ExpressionAttribute resolveAttribute(Method method, Class<?> targetClass) {
6248
return ExpressionAttribute.NULL_ATTRIBUTE;
6349
}
6450
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);
6653
return new PreAuthorizeExpressionAttribute(expression, handler);
6754
}
6855

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-
7856
private PreAuthorize findPreAuthorizeAnnotation(Method method, Class<?> targetClass) {
7957
Class<?> targetClassToUse = targetClass(method, targetClass);
8058
return this.preAuthorizeScanner.scan(method, targetClassToUse);
@@ -86,31 +64,11 @@ private PreAuthorize findPreAuthorizeAnnotation(Method method, Class<?> targetCl
8664
* @param context the {@link ApplicationContext} to use
8765
*/
8866
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);
9168
}
9269

9370
void setTemplateDefaults(AnnotationTemplateExpressionDefaults defaults) {
9471
this.preAuthorizeScanner = SecurityAnnotationScanners.requireUnique(PreAuthorize.class, defaults);
9572
}
9673

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-
11674
}

0 commit comments

Comments
 (0)