Skip to content

Commit 8425f56

Browse files
committed
Move and share DefaultAuthorizationManagerFactory
Signed-off-by: Steve Riesenberg <[email protected]>
1 parent 79715c8 commit 8425f56

File tree

6 files changed

+153
-125
lines changed

6 files changed

+153
-125
lines changed

core/src/main/java/org/springframework/security/access/expression/method/DefaultMethodSecurityAuthorizationManagerFactory.java

Lines changed: 0 additions & 120 deletions
This file was deleted.

core/src/main/java/org/springframework/security/access/expression/method/DefaultMethodSecurityExpressionHandler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.springframework.security.authentication.AuthenticationTrustResolver;
4646
import org.springframework.security.authentication.AuthenticationTrustResolverImpl;
4747
import org.springframework.security.authorization.AuthorizationManagerFactory;
48+
import org.springframework.security.authorization.DefaultAuthorizationManagerFactory;
4849
import org.springframework.security.core.Authentication;
4950
import org.springframework.security.core.parameters.DefaultSecurityParameterNameDiscoverer;
5051
import org.springframework.util.Assert;
@@ -65,7 +66,7 @@ public class DefaultMethodSecurityExpressionHandler extends AbstractSecurityExpr
6566

6667
protected final Log logger = LogFactory.getLog(getClass());
6768

68-
private final DefaultMethodSecurityAuthorizationManagerFactory defaultAuthorizationManagerFactory = new DefaultMethodSecurityAuthorizationManagerFactory();
69+
private final DefaultAuthorizationManagerFactory<MethodInvocation> defaultAuthorizationManagerFactory = new DefaultAuthorizationManagerFactory<>();
6970

7071
private AuthorizationManagerFactory<MethodInvocation> authorizationManagerFactory = defaultAuthorizationManagerFactory;
7172

@@ -226,7 +227,7 @@ private Object filterStream(final Stream<?> filterTarget, Expression filterExpre
226227

227228
/**
228229
* Sets the {@link AuthorizationManagerFactory} to be used. The default is
229-
* {@link DefaultMethodSecurityAuthorizationManagerFactory}.
230+
* {@link DefaultAuthorizationManagerFactory}.
230231
* @param authorizationManagerFactory the {@link AuthorizationManagerFactory} to use.
231232
* Cannot be null.
232233
* @since 7.0

core/src/main/java/org/springframework/security/access/expression/method/MethodSecurityExpressionRoot.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.security.authorization.AuthorizationManager;
2828
import org.springframework.security.authorization.AuthorizationManagerFactory;
2929
import org.springframework.security.authorization.AuthorizationResult;
30+
import org.springframework.security.authorization.DefaultAuthorizationManagerFactory;
3031
import org.springframework.security.core.Authentication;
3132
import org.springframework.util.Assert;
3233
import org.springframework.util.function.SingletonSupplier;
@@ -41,7 +42,7 @@
4142
*/
4243
final class MethodSecurityExpressionRoot implements MethodSecurityExpressionOperations {
4344

44-
private static final DefaultMethodSecurityAuthorizationManagerFactory DEFAULT_AUTHORIZATION_MANAGER_FACTORY = new DefaultMethodSecurityAuthorizationManagerFactory();
45+
private static final DefaultAuthorizationManagerFactory<MethodInvocation> DEFAULT_AUTHORIZATION_MANAGER_FACTORY = new DefaultAuthorizationManagerFactory<>();
4546

4647
private static final PermissionEvaluator DEFAULT_PERMISSION_EVALUATOR = new DenyAllPermissionEvaluator();
4748

core/src/main/java/org/springframework/security/authorization/AuthorizationManagerFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
/**
2020
* A factory for creating different kinds of {@link AuthorizationManager} instances.
2121
*
22-
* @param <T> the type of object that the authorization check is being done on.
22+
* @param <T> the type of object that the authorization check is being done on
2323
* @author Steve Riesenberg
2424
* @since 7.0
2525
*/
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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;
18+
19+
import org.springframework.security.access.hierarchicalroles.NullRoleHierarchy;
20+
import org.springframework.security.access.hierarchicalroles.RoleHierarchy;
21+
import org.springframework.security.authentication.AuthenticationTrustResolver;
22+
import org.springframework.security.authentication.AuthenticationTrustResolverImpl;
23+
import org.springframework.util.Assert;
24+
25+
/**
26+
* A factory for creating different kinds of {@link AuthorizationManager} instances.
27+
*
28+
* @param <T> the type of object that the authorization check is being done on
29+
* @author Steve Riesenberg
30+
* @since 7.0
31+
*/
32+
public final class DefaultAuthorizationManagerFactory<T> implements AuthorizationManagerFactory<T> {
33+
34+
private AuthenticationTrustResolver trustResolver = new AuthenticationTrustResolverImpl();
35+
36+
private RoleHierarchy roleHierarchy = new NullRoleHierarchy();
37+
38+
private String rolePrefix = "ROLE_";
39+
40+
/**
41+
* Returns the {@link AuthenticationTrustResolver} used to check the user's
42+
* authentication.
43+
* @return the {@link AuthenticationTrustResolver}
44+
*/
45+
public AuthenticationTrustResolver getTrustResolver() {
46+
return this.trustResolver;
47+
}
48+
49+
/**
50+
* Sets the {@link AuthenticationTrustResolver} used to check the user's
51+
* authentication.
52+
* @param trustResolver the {@link AuthenticationTrustResolver} to use
53+
*/
54+
public void setTrustResolver(AuthenticationTrustResolver trustResolver) {
55+
Assert.notNull(trustResolver, "trustResolver cannot be null");
56+
this.trustResolver = trustResolver;
57+
}
58+
59+
/**
60+
* Returns the {@link RoleHierarchy} used to discover reachable authorities.
61+
* @return the {@link RoleHierarchy}
62+
*/
63+
public RoleHierarchy getRoleHierarchy() {
64+
return this.roleHierarchy;
65+
}
66+
67+
/**
68+
* Sets the {@link RoleHierarchy} used to discover reachable authorities.
69+
* @param roleHierarchy the {@link RoleHierarchy} to use
70+
*/
71+
public void setRoleHierarchy(RoleHierarchy roleHierarchy) {
72+
Assert.notNull(roleHierarchy, "roleHierarchy cannot be null");
73+
this.roleHierarchy = roleHierarchy;
74+
}
75+
76+
/**
77+
* Returns the prefix used to create an authority name from a role name.
78+
* @return the role prefix
79+
*/
80+
public String getRolePrefix() {
81+
return this.rolePrefix;
82+
}
83+
84+
/**
85+
* Sets the prefix used to create an authority name from a role name. Can be an empty
86+
* string.
87+
* @param rolePrefix the role prefix to use
88+
*/
89+
public void setRolePrefix(String rolePrefix) {
90+
Assert.notNull(rolePrefix, "rolePrefix cannot be null");
91+
this.rolePrefix = rolePrefix;
92+
}
93+
94+
@Override
95+
public AuthorizationManager<T> hasRole(String role) {
96+
return hasAnyRole(role);
97+
}
98+
99+
@Override
100+
public AuthorizationManager<T> hasAnyRole(String... roles) {
101+
return withRoleHierarchy(AuthorityAuthorizationManager.hasAnyRole(this.rolePrefix, roles));
102+
}
103+
104+
@Override
105+
public AuthorizationManager<T> hasAuthority(String authority) {
106+
return withRoleHierarchy(AuthorityAuthorizationManager.hasAuthority(authority));
107+
}
108+
109+
@Override
110+
public AuthorizationManager<T> hasAnyAuthority(String... authorities) {
111+
return withRoleHierarchy(AuthorityAuthorizationManager.hasAnyAuthority(authorities));
112+
}
113+
114+
@Override
115+
public AuthorizationManager<T> authenticated() {
116+
return withTrustResolver(AuthenticatedAuthorizationManager.authenticated());
117+
}
118+
119+
@Override
120+
public AuthorizationManager<T> fullyAuthenticated() {
121+
return withTrustResolver(AuthenticatedAuthorizationManager.fullyAuthenticated());
122+
}
123+
124+
@Override
125+
public AuthorizationManager<T> rememberMe() {
126+
return withTrustResolver(AuthenticatedAuthorizationManager.rememberMe());
127+
}
128+
129+
@Override
130+
public AuthorizationManager<T> anonymous() {
131+
return withTrustResolver(AuthenticatedAuthorizationManager.anonymous());
132+
}
133+
134+
private AuthorityAuthorizationManager<T> withRoleHierarchy(AuthorityAuthorizationManager<T> authorizationManager) {
135+
authorizationManager.setRoleHierarchy(this.roleHierarchy);
136+
return authorizationManager;
137+
}
138+
139+
private AuthenticatedAuthorizationManager<T> withTrustResolver(
140+
AuthenticatedAuthorizationManager<T> authorizationManager) {
141+
authorizationManager.setTrustResolver(this.trustResolver);
142+
return authorizationManager;
143+
}
144+
145+
}

core/src/test/java/org/springframework/security/access/expression/method/MethodSecurityExpressionRootTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.security.access.PermissionEvaluator;
2727
import org.springframework.security.access.expression.ExpressionUtils;
2828
import org.springframework.security.authentication.AuthenticationTrustResolver;
29+
import org.springframework.security.authorization.DefaultAuthorizationManagerFactory;
2930
import org.springframework.security.core.Authentication;
3031

3132
import static org.assertj.core.api.Assertions.assertThat;
@@ -58,7 +59,7 @@ public void createContext() {
5859
this.ctx = new StandardEvaluationContext();
5960
this.ctx.setRootObject(this.root);
6061
this.trustResolver = mock(AuthenticationTrustResolver.class);
61-
DefaultMethodSecurityAuthorizationManagerFactory authorizationManagerFactory = new DefaultMethodSecurityAuthorizationManagerFactory();
62+
DefaultAuthorizationManagerFactory authorizationManagerFactory = new DefaultAuthorizationManagerFactory();
6263
authorizationManagerFactory.setTrustResolver(this.trustResolver);
6364
this.root.setAuthorizationManagerFactory(authorizationManagerFactory);
6465
}

0 commit comments

Comments
 (0)