|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2018 the original author or authors. |
| 2 | + * Copyright 2002-2020 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.
|
|
32 | 32 | import org.springframework.security.access.PermissionEvaluator;
|
33 | 33 | import org.springframework.security.access.expression.AbstractSecurityExpressionHandler;
|
34 | 34 | import org.springframework.security.access.expression.SecurityExpressionHandler;
|
| 35 | +import org.springframework.security.access.hierarchicalroles.RoleHierarchy; |
| 36 | +import org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl; |
35 | 37 | import org.springframework.security.authentication.TestingAuthenticationToken;
|
36 | 38 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
37 | 39 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
|
68 | 70 | *
|
69 | 71 | * @author Rob Winch
|
70 | 72 | * @author Joe Grandja
|
| 73 | + * @author Evgeniy Cheban |
71 | 74 | */
|
72 | 75 | public class WebSecurityConfigurationTests {
|
73 | 76 | @Rule
|
@@ -270,6 +273,31 @@ protected void configure(HttpSecurity http) throws Exception {
|
270 | 273 | }
|
271 | 274 | }
|
272 | 275 |
|
| 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 | + |
273 | 301 | @Test
|
274 | 302 | public void securityExpressionHandlerWhenPermissionEvaluatorBeanThenPermissionEvaluatorUsed() throws Exception {
|
275 | 303 | this.spring.register(WebSecurityExpressionHandlerPermissionEvaluatorBeanConfig.class).autowire();
|
|
0 commit comments