Skip to content

Commit b4e51e7

Browse files
evgeniychebaneleftherias
authored andcommitted
DefaultWebSecurityExpressionHandler uses RoleHierarchy bean
Fixes gh-7059
1 parent e0169ea commit b4e51e7

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-2013 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;
@@ -74,6 +75,7 @@
7475
* @see WebSecurityConfiguration
7576
*
7677
* @author Rob Winch
78+
* @author Evgeniy Cheban
7779
* @since 3.2
7880
*/
7981
public final class WebSecurity extends
@@ -383,6 +385,11 @@ public void setApplicationContext(ApplicationContext applicationContext)
383385
throws BeansException {
384386
this.defaultWebSecurityExpressionHandler
385387
.setApplicationContext(applicationContext);
388+
389+
try {
390+
this.defaultWebSecurityExpressionHandler.setRoleHierarchy(applicationContext.getBean(RoleHierarchy.class));
391+
} catch (NoSuchBeanDefinitionException e) {}
392+
386393
try {
387394
this.defaultWebSecurityExpressionHandler.setPermissionEvaluator(applicationContext.getBean(
388395
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-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.
@@ -45,6 +45,8 @@
4545
import org.springframework.security.access.PermissionEvaluator;
4646
import org.springframework.security.access.expression.AbstractSecurityExpressionHandler;
4747
import org.springframework.security.access.expression.SecurityExpressionHandler;
48+
import org.springframework.security.access.hierarchicalroles.RoleHierarchy;
49+
import org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl;
4850
import org.springframework.security.authentication.TestingAuthenticationToken;
4951
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
5052
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
@@ -68,6 +70,7 @@
6870
*
6971
* @author Rob Winch
7072
* @author Joe Grandja
73+
* @author Evgeniy Cheban
7174
*/
7275
public class WebSecurityConfigurationTests {
7376
@Rule
@@ -270,6 +273,31 @@ protected void configure(HttpSecurity http) throws Exception {
270273
}
271274
}
272275

276+
@Test
277+
public void securityExpressionHandlerWhenRoleHierarchyBeanThenRoleHierarchyUsed() {
278+
this.spring.register(WebSecurityExpressionHandlerRoleHierarchyBeanConfig.class).autowire();
279+
TestingAuthenticationToken authentication = new TestingAuthenticationToken("user", "notused", "ROLE_ADMIN");
280+
FilterInvocation invocation = new FilterInvocation(new MockHttpServletRequest("GET", ""),
281+
new MockHttpServletResponse(), new MockFilterChain());
282+
283+
AbstractSecurityExpressionHandler handler = this.spring.getContext().getBean(AbstractSecurityExpressionHandler.class);
284+
EvaluationContext evaluationContext = handler.createEvaluationContext(authentication, invocation);
285+
Expression expression = handler.getExpressionParser()
286+
.parseExpression("hasRole('ROLE_USER')");
287+
boolean granted = expression.getValue(evaluationContext, Boolean.class);
288+
assertThat(granted).isTrue();
289+
}
290+
291+
@EnableWebSecurity
292+
static class WebSecurityExpressionHandlerRoleHierarchyBeanConfig extends WebSecurityConfigurerAdapter {
293+
@Bean
294+
RoleHierarchy roleHierarchy() {
295+
RoleHierarchyImpl roleHierarchy = new RoleHierarchyImpl();
296+
roleHierarchy.setHierarchy("ROLE_ADMIN > ROLE_USER");
297+
return roleHierarchy;
298+
}
299+
}
300+
273301
@Test
274302
public void securityExpressionHandlerWhenPermissionEvaluatorBeanThenPermissionEvaluatorUsed() throws Exception {
275303
this.spring.register(WebSecurityExpressionHandlerPermissionEvaluatorBeanConfig.class).autowire();

0 commit comments

Comments
 (0)