Skip to content

Commit 4e7be20

Browse files
evgeniychebaneleftherias
authored andcommitted
DefaultWebSecurityExpressionHandler uses RoleHierarchy bean
Fixes gh-7059
1 parent ccbad61 commit 4e7be20

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/builders/WebSecurity.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -31,6 +31,7 @@
3131
import org.springframework.http.HttpMethod;
3232
import org.springframework.security.access.PermissionEvaluator;
3333
import org.springframework.security.access.expression.SecurityExpressionHandler;
34+
import org.springframework.security.access.hierarchicalroles.RoleHierarchy;
3435
import org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder;
3536
import org.springframework.security.config.annotation.ObjectPostProcessor;
3637
import org.springframework.security.config.annotation.SecurityBuilder;
@@ -75,6 +76,7 @@
7576
* @see WebSecurityConfiguration
7677
*
7778
* @author Rob Winch
79+
* @author Evgeniy Cheban
7880
* @since 3.2
7981
*/
8082
public final class WebSecurity extends
@@ -389,6 +391,11 @@ public void setApplicationContext(ApplicationContext applicationContext)
389391
throws BeansException {
390392
this.defaultWebSecurityExpressionHandler
391393
.setApplicationContext(applicationContext);
394+
395+
try {
396+
this.defaultWebSecurityExpressionHandler.setRoleHierarchy(applicationContext.getBean(RoleHierarchy.class));
397+
} catch (NoSuchBeanDefinitionException e) {}
398+
392399
try {
393400
this.defaultWebSecurityExpressionHandler.setPermissionEvaluator(applicationContext.getBean(
394401
PermissionEvaluator.class));

config/src/test/java/org/springframework/security/config/annotation/web/configuration/WebSecurityConfigurationTests.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -32,6 +32,8 @@
3232
import org.springframework.security.access.PermissionEvaluator;
3333
import org.springframework.security.access.expression.AbstractSecurityExpressionHandler;
3434
import org.springframework.security.access.expression.SecurityExpressionHandler;
35+
import org.springframework.security.access.hierarchicalroles.RoleHierarchy;
36+
import org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl;
3537
import org.springframework.security.authentication.TestingAuthenticationToken;
3638
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
3739
import org.springframework.security.config.annotation.authentication.configuration.EnableGlobalAuthentication;
@@ -69,6 +71,7 @@
6971
*
7072
* @author Rob Winch
7173
* @author Joe Grandja
74+
* @author Evgeniy Cheban
7275
*/
7376
public class WebSecurityConfigurationTests {
7477
@Rule
@@ -290,6 +293,31 @@ protected void configure(HttpSecurity http) throws Exception {
290293
}
291294
}
292295

296+
@Test
297+
public void securityExpressionHandlerWhenRoleHierarchyBeanThenRoleHierarchyUsed() {
298+
this.spring.register(WebSecurityExpressionHandlerRoleHierarchyBeanConfig.class).autowire();
299+
TestingAuthenticationToken authentication = new TestingAuthenticationToken("user", "notused", "ROLE_ADMIN");
300+
FilterInvocation invocation = new FilterInvocation(new MockHttpServletRequest("GET", ""),
301+
new MockHttpServletResponse(), new MockFilterChain());
302+
303+
AbstractSecurityExpressionHandler handler = this.spring.getContext().getBean(AbstractSecurityExpressionHandler.class);
304+
EvaluationContext evaluationContext = handler.createEvaluationContext(authentication, invocation);
305+
Expression expression = handler.getExpressionParser()
306+
.parseExpression("hasRole('ROLE_USER')");
307+
boolean granted = expression.getValue(evaluationContext, Boolean.class);
308+
assertThat(granted).isTrue();
309+
}
310+
311+
@EnableWebSecurity
312+
static class WebSecurityExpressionHandlerRoleHierarchyBeanConfig extends WebSecurityConfigurerAdapter {
313+
@Bean
314+
RoleHierarchy roleHierarchy() {
315+
RoleHierarchyImpl roleHierarchy = new RoleHierarchyImpl();
316+
roleHierarchy.setHierarchy("ROLE_ADMIN > ROLE_USER");
317+
return roleHierarchy;
318+
}
319+
}
320+
293321
@Test
294322
public void securityExpressionHandlerWhenPermissionEvaluatorBeanThenPermissionEvaluatorUsed() {
295323
this.spring.register(WebSecurityExpressionHandlerPermissionEvaluatorBeanConfig.class).autowire();

0 commit comments

Comments
 (0)