4141import org .springframework .security .access .PermissionCacheOptimizer ;
4242import org .springframework .security .access .expression .AbstractSecurityExpressionHandler ;
4343import org .springframework .security .access .expression .ExpressionUtils ;
44+ import org .springframework .security .access .hierarchicalroles .RoleHierarchy ;
4445import org .springframework .security .authentication .AuthenticationTrustResolver ;
4546import org .springframework .security .authentication .AuthenticationTrustResolverImpl ;
47+ import org .springframework .security .authorization .AuthorizationManagerFactory ;
4648import org .springframework .security .core .Authentication ;
4749import org .springframework .security .core .parameters .DefaultSecurityParameterNameDiscoverer ;
4850import org .springframework .util .Assert ;
@@ -63,14 +65,14 @@ public class DefaultMethodSecurityExpressionHandler extends AbstractSecurityExpr
6365
6466 protected final Log logger = LogFactory .getLog (getClass ());
6567
66- private AuthenticationTrustResolver trustResolver = new AuthenticationTrustResolverImpl ();
68+ private final DefaultMethodSecurityAuthorizationManagerFactory defaultAuthorizationManagerFactory = new DefaultMethodSecurityAuthorizationManagerFactory ();
69+
70+ private AuthorizationManagerFactory <MethodInvocation > authorizationManagerFactory = defaultAuthorizationManagerFactory ;
6771
6872 private ParameterNameDiscoverer parameterNameDiscoverer = new DefaultSecurityParameterNameDiscoverer ();
6973
7074 private @ Nullable PermissionCacheOptimizer permissionCacheOptimizer = null ;
7175
72- private String defaultRolePrefix = "ROLE_" ;
73-
7476 public DefaultMethodSecurityExpressionHandler () {
7577 }
7678
@@ -103,12 +105,10 @@ protected MethodSecurityExpressionOperations createSecurityExpressionRoot(Authen
103105
104106 private MethodSecurityExpressionOperations createSecurityExpressionRoot (Supplier <Authentication > authentication ,
105107 MethodInvocation invocation ) {
106- MethodSecurityExpressionRoot root = new MethodSecurityExpressionRoot (authentication );
107- root .setThis ( invocation . getThis () );
108+ MethodSecurityExpressionRoot root = new MethodSecurityExpressionRoot (authentication , invocation );
109+ root .setAuthorizationManagerFactory ( this . authorizationManagerFactory );
108110 root .setPermissionEvaluator (getPermissionEvaluator ());
109- root .setTrustResolver (getTrustResolver ());
110- Optional .ofNullable (getRoleHierarchy ()).ifPresent (root ::setRoleHierarchy );
111- root .setDefaultRolePrefix (getDefaultRolePrefix ());
111+ root .setThis (invocation .getThis ());
112112 return root ;
113113 }
114114
@@ -224,6 +224,19 @@ private Object filterStream(final Stream<?> filterTarget, Expression filterExpre
224224 }).onClose (filterTarget ::close );
225225 }
226226
227+ /**
228+ * Sets the {@link AuthorizationManagerFactory} to be used. The default is
229+ * {@link DefaultMethodSecurityAuthorizationManagerFactory}.
230+ * @param authorizationManagerFactory the {@link AuthorizationManagerFactory} to use.
231+ * Cannot be null.
232+ * @since 7.0
233+ */
234+ public void setAuthorizationManagerFactory (
235+ AuthorizationManagerFactory <MethodInvocation > authorizationManagerFactory ) {
236+ Assert .notNull (authorizationManagerFactory , "authorizationManagerFactory cannot be null" );
237+ this .authorizationManagerFactory = authorizationManagerFactory ;
238+ }
239+
227240 /**
228241 * Sets the {@link AuthenticationTrustResolver} to be used. The default is
229242 * {@link AuthenticationTrustResolverImpl}.
@@ -232,14 +245,26 @@ private Object filterStream(final Stream<?> filterTarget, Expression filterExpre
232245 */
233246 public void setTrustResolver (AuthenticationTrustResolver trustResolver ) {
234247 Assert .notNull (trustResolver , "trustResolver cannot be null" );
235- this .trustResolver = trustResolver ;
248+ this .defaultAuthorizationManagerFactory . setTrustResolver ( trustResolver ) ;
236249 }
237250
238251 /**
239252 * @return The current {@link AuthenticationTrustResolver}
240253 */
241254 protected AuthenticationTrustResolver getTrustResolver () {
242- return this .trustResolver ;
255+ return this .defaultAuthorizationManagerFactory .getTrustResolver ();
256+ }
257+
258+ @ Override
259+ public void setRoleHierarchy (@ Nullable RoleHierarchy roleHierarchy ) {
260+ if (roleHierarchy != null ) {
261+ this .defaultAuthorizationManagerFactory .setRoleHierarchy (roleHierarchy );
262+ }
263+ }
264+
265+ @ Override
266+ protected @ Nullable RoleHierarchy getRoleHierarchy () {
267+ return this .defaultAuthorizationManagerFactory .getRoleHierarchy ();
243268 }
244269
245270 /**
@@ -288,14 +313,14 @@ public void setReturnObject(@Nullable Object returnObject, EvaluationContext ctx
288313 * @param defaultRolePrefix the default prefix to add to roles. Default "ROLE_".
289314 */
290315 public void setDefaultRolePrefix (String defaultRolePrefix ) {
291- this .defaultRolePrefix = defaultRolePrefix ;
316+ this .defaultAuthorizationManagerFactory . setRolePrefix ( defaultRolePrefix ) ;
292317 }
293318
294319 /**
295320 * @return The default role prefix
296321 */
297322 protected String getDefaultRolePrefix () {
298- return this .defaultRolePrefix ;
323+ return this .defaultAuthorizationManagerFactory . getRolePrefix () ;
299324 }
300325
301326}
0 commit comments