|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2019 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.authentication.configuration.EnableGlobalAuthentication;
|
|
69 | 71 | *
|
70 | 72 | * @author Rob Winch
|
71 | 73 | * @author Joe Grandja
|
| 74 | + * @author Evgeniy Cheban |
72 | 75 | */
|
73 | 76 | public class WebSecurityConfigurationTests {
|
74 | 77 | @Rule
|
@@ -290,6 +293,31 @@ protected void configure(HttpSecurity http) throws Exception {
|
290 | 293 | }
|
291 | 294 | }
|
292 | 295 |
|
| 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 | + |
293 | 321 | @Test
|
294 | 322 | public void securityExpressionHandlerWhenPermissionEvaluatorBeanThenPermissionEvaluatorUsed() {
|
295 | 323 | this.spring.register(WebSecurityExpressionHandlerPermissionEvaluatorBeanConfig.class).autowire();
|
|
0 commit comments