1
1
/*
2
- * Copyright 2002-2023 the original author or authors.
2
+ * Copyright 2002-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
17
17
package org .springframework .security .access .expression ;
18
18
19
19
import java .io .Serializable ;
20
- import java .util .Collection ;
21
- import java .util .Set ;
22
20
import java .util .function .Supplier ;
23
21
24
22
import org .jspecify .annotations .Nullable ;
25
23
26
24
import org .springframework .security .access .PermissionEvaluator ;
27
25
import org .springframework .security .access .hierarchicalroles .RoleHierarchy ;
28
26
import org .springframework .security .authentication .AuthenticationTrustResolver ;
29
- import org .springframework .security .authentication .AuthenticationTrustResolverImpl ;
27
+ import org .springframework .security .authorization .AuthorizationManager ;
28
+ import org .springframework .security .authorization .AuthorizationManagerFactory ;
29
+ import org .springframework .security .authorization .AuthorizationResult ;
30
+ import org .springframework .security .authorization .DefaultAuthorizationManagerFactory ;
30
31
import org .springframework .security .core .Authentication ;
31
- import org .springframework .security .core .GrantedAuthority ;
32
- import org .springframework .security .core .authority .AuthorityUtils ;
33
32
import org .springframework .util .Assert ;
34
33
import org .springframework .util .function .SingletonSupplier ;
35
34
42
41
*/
43
42
public abstract class SecurityExpressionRoot implements SecurityExpressionOperations {
44
43
45
- private final Supplier <Authentication > authentication ;
44
+ private static final AuthorizationManagerFactory <Object > DEFAULT_AUTHORIZATION_MANAGER_FACTORY = new DefaultAuthorizationManagerFactory <>();
45
+
46
+ private static final Object DEFAULT_OBJECT = new Object ();
46
47
47
- private AuthenticationTrustResolver trustResolver = new AuthenticationTrustResolverImpl () ;
48
+ private final Supplier < Authentication > authentication ;
48
49
49
- private @ Nullable RoleHierarchy roleHierarchy ;
50
+ private final Object object ;
50
51
51
- private @ Nullable Set < String > roles ;
52
+ private @ Nullable DefaultAuthorizationManagerFactory < Object > defaultAuthorizationManagerFactory ;
52
53
53
- private String defaultRolePrefix = "ROLE_" ;
54
+ private AuthorizationManagerFactory < Object > authorizationManagerFactory = DEFAULT_AUTHORIZATION_MANAGER_FACTORY ;
54
55
55
56
/**
56
57
* Allows "permitAll" expression
@@ -77,9 +78,11 @@ public abstract class SecurityExpressionRoot implements SecurityExpressionOperat
77
78
/**
78
79
* Creates a new instance
79
80
* @param authentication the {@link Authentication} to use. Cannot be null.
81
+ * @deprecated use {@link #SecurityExpressionRoot(Supplier, Object)} instead
80
82
*/
83
+ @ Deprecated (since = "7.0" )
81
84
public SecurityExpressionRoot (Authentication authentication ) {
82
- this (() -> authentication );
85
+ this (() -> authentication , DEFAULT_OBJECT );
83
86
}
84
87
85
88
/**
@@ -88,44 +91,48 @@ public SecurityExpressionRoot(Authentication authentication) {
88
91
* @param authentication the {@link Supplier} of the {@link Authentication} to use.
89
92
* Cannot be null.
90
93
* @since 5.8
94
+ * @deprecated use {@link #SecurityExpressionRoot(Supplier, Object)} instead
91
95
*/
96
+ @ Deprecated (since = "7.0" )
92
97
public SecurityExpressionRoot (Supplier <Authentication > authentication ) {
98
+ this (authentication , DEFAULT_OBJECT );
99
+ }
100
+
101
+ /**
102
+ * Creates a new instance that uses lazy initialization of the {@link Authentication}
103
+ * object.
104
+ * @param authentication the {@link Supplier} of the {@link Authentication} to use.
105
+ * Cannot be null.
106
+ * @param object the object being authorized
107
+ * @since 7.0
108
+ */
109
+ public SecurityExpressionRoot (Supplier <Authentication > authentication , Object object ) {
93
110
this .authentication = SingletonSupplier .of (() -> {
94
111
Authentication value = authentication .get ();
95
112
Assert .notNull (value , "Authentication object cannot be null" );
96
113
return value ;
97
114
});
115
+ this .object = object ;
98
116
}
99
117
100
118
@ Override
101
119
public final boolean hasAuthority (String authority ) {
102
- return hasAnyAuthority (authority );
120
+ return isGranted ( this . authorizationManagerFactory . hasAnyAuthority (authority ) );
103
121
}
104
122
105
123
@ Override
106
124
public final boolean hasAnyAuthority (String ... authorities ) {
107
- return hasAnyAuthorityName ( null , authorities );
125
+ return isGranted ( this . authorizationManagerFactory . hasAnyAuthority ( authorities ) );
108
126
}
109
127
110
128
@ Override
111
129
public final boolean hasRole (String role ) {
112
- return hasAnyRole ( role );
130
+ return isGranted ( this . authorizationManagerFactory . hasRole ( role ) );
113
131
}
114
132
115
133
@ Override
116
134
public final boolean hasAnyRole (String ... roles ) {
117
- return hasAnyAuthorityName (this .defaultRolePrefix , roles );
118
- }
119
-
120
- private boolean hasAnyAuthorityName (@ Nullable String prefix , String ... roles ) {
121
- Set <String > roleSet = getAuthoritySet ();
122
- for (String role : roles ) {
123
- String defaultedRole = getRoleWithDefaultPrefix (prefix , role );
124
- if (roleSet .contains (defaultedRole )) {
125
- return true ;
126
- }
127
- }
128
- return false ;
135
+ return isGranted (this .authorizationManagerFactory .hasAnyRole (roles ));
129
136
}
130
137
131
138
@ Override
@@ -135,33 +142,37 @@ public final Authentication getAuthentication() {
135
142
136
143
@ Override
137
144
public final boolean permitAll () {
138
- return true ;
145
+ return isGranted ( this . authorizationManagerFactory . permitAll ()) ;
139
146
}
140
147
141
148
@ Override
142
149
public final boolean denyAll () {
143
- return false ;
150
+ return isGranted ( this . authorizationManagerFactory . denyAll ()) ;
144
151
}
145
152
146
153
@ Override
147
154
public final boolean isAnonymous () {
148
- return this .trustResolver . isAnonymous ( getAuthentication ());
155
+ return isGranted ( this .authorizationManagerFactory . anonymous ());
149
156
}
150
157
151
158
@ Override
152
159
public final boolean isAuthenticated () {
153
- return this .trustResolver . isAuthenticated ( getAuthentication ());
160
+ return isGranted ( this .authorizationManagerFactory . authenticated ());
154
161
}
155
162
156
163
@ Override
157
164
public final boolean isRememberMe () {
158
- return this .trustResolver . isRememberMe ( getAuthentication ());
165
+ return isGranted ( this .authorizationManagerFactory . rememberMe ());
159
166
}
160
167
161
168
@ Override
162
169
public final boolean isFullyAuthenticated () {
163
- Authentication authentication = getAuthentication ();
164
- return this .trustResolver .isFullyAuthenticated (authentication );
170
+ return isGranted (this .authorizationManagerFactory .fullyAuthenticated ());
171
+ }
172
+
173
+ private boolean isGranted (AuthorizationManager <Object > authorizationManager ) {
174
+ AuthorizationResult authorizationResult = authorizationManager .authorize (this .authentication , this .object );
175
+ return (authorizationResult != null && authorizationResult .isGranted ());
165
176
}
166
177
167
178
/**
@@ -173,12 +184,22 @@ public final boolean isFullyAuthenticated() {
173
184
return getAuthentication ().getPrincipal ();
174
185
}
175
186
187
+ /**
188
+ * @deprecated Use
189
+ * {@link #setAuthorizationManagerFactory(AuthorizationManagerFactory)} instead
190
+ */
191
+ @ Deprecated (since = "7.0" )
176
192
public void setTrustResolver (AuthenticationTrustResolver trustResolver ) {
177
- this . trustResolver = trustResolver ;
193
+ getDefaultAuthorizationManagerFactory (). setTrustResolver ( trustResolver ) ;
178
194
}
179
195
196
+ /**
197
+ * @deprecated Use
198
+ * {@link #setAuthorizationManagerFactory(AuthorizationManagerFactory)} instead
199
+ */
200
+ @ Deprecated (since = "7.0" )
180
201
public void setRoleHierarchy (RoleHierarchy roleHierarchy ) {
181
- this . roleHierarchy = roleHierarchy ;
202
+ getDefaultAuthorizationManagerFactory (). setRoleHierarchy ( roleHierarchy ) ;
182
203
}
183
204
184
205
/**
@@ -193,20 +214,32 @@ public void setRoleHierarchy(RoleHierarchy roleHierarchy) {
193
214
* If null or empty, then no default role prefix is used.
194
215
* </p>
195
216
* @param defaultRolePrefix the default prefix to add to roles. Default "ROLE_".
217
+ * @deprecated Use
218
+ * {@link #setAuthorizationManagerFactory(AuthorizationManagerFactory)} instead
196
219
*/
220
+ @ Deprecated (since = "7.0" )
197
221
public void setDefaultRolePrefix (String defaultRolePrefix ) {
198
- this .defaultRolePrefix = defaultRolePrefix ;
222
+ getDefaultAuthorizationManagerFactory ().setRolePrefix (defaultRolePrefix );
223
+ }
224
+
225
+ /**
226
+ * Sets the {@link AuthorizationManagerFactory} to use for creating instances of
227
+ * {@link AuthorizationManager}.
228
+ * @param authorizationManagerFactory the {@link AuthorizationManagerFactory} to use
229
+ * @since 7.0
230
+ */
231
+ public void setAuthorizationManagerFactory (AuthorizationManagerFactory <Object > authorizationManagerFactory ) {
232
+ Assert .notNull (authorizationManagerFactory , "authorizationManagerFactory cannot be null" );
233
+ this .authorizationManagerFactory = authorizationManagerFactory ;
199
234
}
200
235
201
- private Set <String > getAuthoritySet () {
202
- if (this .roles == null ) {
203
- Collection <? extends GrantedAuthority > userAuthorities = getAuthentication ().getAuthorities ();
204
- if (this .roleHierarchy != null ) {
205
- userAuthorities = this .roleHierarchy .getReachableGrantedAuthorities (userAuthorities );
206
- }
207
- this .roles = AuthorityUtils .authorityListToSet (userAuthorities );
236
+ private DefaultAuthorizationManagerFactory <Object > getDefaultAuthorizationManagerFactory () {
237
+ if (this .defaultAuthorizationManagerFactory == null ) {
238
+ this .defaultAuthorizationManagerFactory = new DefaultAuthorizationManagerFactory <>();
239
+ this .authorizationManagerFactory = this .defaultAuthorizationManagerFactory ;
208
240
}
209
- return this .roles ;
241
+
242
+ return this .defaultAuthorizationManagerFactory ;
210
243
}
211
244
212
245
@ Override
@@ -225,24 +258,4 @@ public void setPermissionEvaluator(PermissionEvaluator permissionEvaluator) {
225
258
this .permissionEvaluator = permissionEvaluator ;
226
259
}
227
260
228
- /**
229
- * Prefixes role with defaultRolePrefix if defaultRolePrefix is non-null and if role
230
- * does not already start with defaultRolePrefix.
231
- * @param defaultRolePrefix
232
- * @param role
233
- * @return
234
- */
235
- private static String getRoleWithDefaultPrefix (@ Nullable String defaultRolePrefix , String role ) {
236
- if (role == null ) {
237
- return role ;
238
- }
239
- if (defaultRolePrefix == null || defaultRolePrefix .length () == 0 ) {
240
- return role ;
241
- }
242
- if (role .startsWith (defaultRolePrefix )) {
243
- return role ;
244
- }
245
- return defaultRolePrefix + role ;
246
- }
247
-
248
261
}
0 commit comments