Skip to content

Commit bff6d82

Browse files
evgeniychebaneleftherias
authored andcommitted
DefaultWebSecurityExpressionHandler uses RoleHierarchy bean
Fixes gh-7059
1 parent 674e2c0 commit bff6d82

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;
@@ -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-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)