Skip to content

Commit 380927f

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

File tree

3 files changed

+92
-93
lines changed

3 files changed

+92
-93
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
import java.util.function.BiFunction;
22+
23+
import org.springframework.context.ApplicationContext;
24+
import org.springframework.security.core.annotation.SecurityAnnotationScanner;
25+
import org.springframework.security.core.annotation.SecurityAnnotationScanners;
26+
import org.springframework.util.Assert;
27+
import org.springframework.util.StringUtils;
28+
29+
/**
30+
* For internal use only, as this contract is likely to change.
31+
*
32+
* @author Evgeniy Cheban
33+
*/
34+
final class MethodAuthorizationDeniedHandlerResolver {
35+
36+
private final MethodAuthorizationDeniedHandler defaultHandler = new ThrowingMethodAuthorizationDeniedHandler();
37+
38+
private final SecurityAnnotationScanner<HandleAuthorizationDenied> handleAuthorizationDeniedScanner = SecurityAnnotationScanners
39+
.requireUnique(HandleAuthorizationDenied.class);
40+
41+
private BiFunction<String, Class<? extends MethodAuthorizationDeniedHandler>, MethodAuthorizationDeniedHandler> resolver;
42+
43+
MethodAuthorizationDeniedHandlerResolver(Class<?> managerClass) {
44+
this.resolver = (beanName, handlerClass) -> new ReflectiveMethodAuthorizationDeniedHandler(handlerClass,
45+
managerClass);
46+
}
47+
48+
void setContext(ApplicationContext context) {
49+
Assert.notNull(context, "context cannot be null");
50+
this.resolver = (beanName, handlerClass) -> doResolve(context, beanName, handlerClass);
51+
}
52+
53+
MethodAuthorizationDeniedHandler resolve(Method method, Class<?> targetClass) {
54+
HandleAuthorizationDenied deniedHandler = this.handleAuthorizationDeniedScanner.scan(method, targetClass);
55+
if (deniedHandler != null) {
56+
return this.resolver.apply(deniedHandler.handler(), deniedHandler.handlerClass());
57+
}
58+
return this.defaultHandler;
59+
}
60+
61+
private MethodAuthorizationDeniedHandler doResolve(ApplicationContext context, String beanName,
62+
Class<? extends MethodAuthorizationDeniedHandler> handlerClass) {
63+
if (StringUtils.hasText(beanName)) {
64+
return context.getBean(beanName, MethodAuthorizationDeniedHandler.class);
65+
}
66+
if (handlerClass == this.defaultHandler.getClass()) {
67+
return this.defaultHandler;
68+
}
69+
String[] beanNames = context.getBeanNamesForType(handlerClass);
70+
if (beanNames.length == 0) {
71+
throw new IllegalStateException("Could not find a bean of type " + handlerClass.getName());
72+
}
73+
if (beanNames.length > 1) {
74+
throw new IllegalStateException("Expected to find a single bean of type " + handlerClass.getName()
75+
+ " but found " + Arrays.toString(beanNames)
76+
+ " consider using 'handler' attribute to refer to specific bean");
77+
}
78+
return context.getBean(beanNames[0], handlerClass);
79+
}
80+
81+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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,12 @@
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(
40+
PostAuthorizeAuthorizationManager.class);
4941

5042
private SecurityAnnotationScanner<PostAuthorize> postAuthorizeScanner = SecurityAnnotationScanners
5143
.requireUnique(PostAuthorize.class);
5244

53-
PostAuthorizeExpressionAttributeRegistry() {
54-
this.handlerResolver = (beanName, clazz) -> new ReflectiveMethodAuthorizationDeniedHandler(clazz,
55-
PostAuthorizeAuthorizationManager.class);
56-
}
57-
5845
@NonNull
5946
@Override
6047
ExpressionAttribute resolveAttribute(Method method, Class<?> targetClass) {
@@ -63,19 +50,11 @@ ExpressionAttribute resolveAttribute(Method method, Class<?> targetClass) {
6350
return ExpressionAttribute.NULL_ATTRIBUTE;
6451
}
6552
Expression expression = getExpressionHandler().getExpressionParser().parseExpression(postAuthorize.value());
66-
MethodAuthorizationDeniedHandler deniedHandler = resolveHandler(method, targetClass);
53+
MethodAuthorizationDeniedHandler deniedHandler = this.handlerResolver.resolve(method,
54+
targetClass(method, targetClass));
6755
return new PostAuthorizeExpressionAttribute(expression, deniedHandler);
6856
}
6957

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-
7958
private PostAuthorize findPostAuthorizeAnnotation(Method method, Class<?> targetClass) {
8059
Class<?> targetClassToUse = targetClass(method, targetClass);
8160
return this.postAuthorizeScanner.scan(method, targetClassToUse);
@@ -87,31 +66,11 @@ private PostAuthorize findPostAuthorizeAnnotation(Method method, Class<?> target
8766
* @param context the {@link ApplicationContext} to use
8867
*/
8968
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);
9270
}
9371

9472
void setTemplateDefaults(AnnotationTemplateExpressionDefaults templateDefaults) {
9573
this.postAuthorizeScanner = SecurityAnnotationScanners.requireUnique(PostAuthorize.class, templateDefaults);
9674
}
9775

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

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

+5-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,12 @@
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(
39+
PreAuthorizeAuthorizationManager.class);
4840

4941
private SecurityAnnotationScanner<PreAuthorize> preAuthorizeScanner = SecurityAnnotationScanners
5042
.requireUnique(PreAuthorize.class);
5143

52-
PreAuthorizeExpressionAttributeRegistry() {
53-
this.handlerResolver = (beanName, clazz) -> new ReflectiveMethodAuthorizationDeniedHandler(clazz,
54-
PreAuthorizeAuthorizationManager.class);
55-
}
56-
5744
@NonNull
5845
@Override
5946
ExpressionAttribute resolveAttribute(Method method, Class<?> targetClass) {
@@ -62,19 +49,11 @@ ExpressionAttribute resolveAttribute(Method method, Class<?> targetClass) {
6249
return ExpressionAttribute.NULL_ATTRIBUTE;
6350
}
6451
Expression expression = getExpressionHandler().getExpressionParser().parseExpression(preAuthorize.value());
65-
MethodAuthorizationDeniedHandler handler = resolveHandler(method, targetClass);
52+
MethodAuthorizationDeniedHandler handler = this.handlerResolver.resolve(method,
53+
targetClass(method, targetClass));
6654
return new PreAuthorizeExpressionAttribute(expression, handler);
6755
}
6856

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-
7857
private PreAuthorize findPreAuthorizeAnnotation(Method method, Class<?> targetClass) {
7958
Class<?> targetClassToUse = targetClass(method, targetClass);
8059
return this.preAuthorizeScanner.scan(method, targetClassToUse);
@@ -86,31 +65,11 @@ private PreAuthorize findPreAuthorizeAnnotation(Method method, Class<?> targetCl
8665
* @param context the {@link ApplicationContext} to use
8766
*/
8867
void setApplicationContext(ApplicationContext context) {
89-
Assert.notNull(context, "context cannot be null");
90-
this.handlerResolver = (beanName, clazz) -> resolveHandler(context, beanName, clazz);
68+
this.handlerResolver.setContext(context);
9169
}
9270

9371
void setTemplateDefaults(AnnotationTemplateExpressionDefaults defaults) {
9472
this.preAuthorizeScanner = SecurityAnnotationScanners.requireUnique(PreAuthorize.class, defaults);
9573
}
9674

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

0 commit comments

Comments
 (0)