|
34 | 34 | import org.springframework.context.annotation.Configuration;
|
35 | 35 | import org.springframework.security.access.AccessDeniedException;
|
36 | 36 | import org.springframework.security.access.PermissionEvaluator;
|
| 37 | +import org.springframework.security.access.annotation.Secured; |
37 | 38 | import org.springframework.security.access.hierarchicalroles.RoleHierarchy;
|
38 | 39 | import org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl;
|
39 | 40 | import org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor;
|
@@ -514,4 +515,42 @@ static class CustomAuthorityService {
|
514 | 515 | public void customPrefixRoleUser() {}
|
515 | 516 | }
|
516 | 517 | }
|
| 518 | + |
| 519 | + @Test |
| 520 | + @WithMockUser(authorities = "USER") |
| 521 | + public void grantedAuthorityDefaultsWithEmptyRolePrefix() { |
| 522 | + this.spring.register(EmptyRolePrefixGrantedAuthorityConfig.class).autowire(); |
| 523 | + |
| 524 | + EmptyRolePrefixGrantedAuthorityConfig.CustomAuthorityService customService = this.spring.getContext() |
| 525 | + .getBean(EmptyRolePrefixGrantedAuthorityConfig.CustomAuthorityService.class); |
| 526 | + |
| 527 | + assertThatThrownBy(() -> this.service.securedUser()) |
| 528 | + .isInstanceOf(AccessDeniedException.class); |
| 529 | + |
| 530 | + customService.emptyPrefixRoleUser(); |
| 531 | + // no exception |
| 532 | + } |
| 533 | + |
| 534 | + @EnableGlobalMethodSecurity(securedEnabled = true) |
| 535 | + static class EmptyRolePrefixGrantedAuthorityConfig { |
| 536 | + @Bean |
| 537 | + public GrantedAuthorityDefaults ga() { |
| 538 | + return new GrantedAuthorityDefaults(""); |
| 539 | + } |
| 540 | + |
| 541 | + @Bean |
| 542 | + public CustomAuthorityService service() { |
| 543 | + return new CustomAuthorityService(); |
| 544 | + } |
| 545 | + |
| 546 | + @Bean |
| 547 | + public MethodSecurityServiceImpl methodSecurityService() { |
| 548 | + return new MethodSecurityServiceImpl(); |
| 549 | + } |
| 550 | + |
| 551 | + static class CustomAuthorityService { |
| 552 | + @Secured("USER") |
| 553 | + public void emptyPrefixRoleUser() {} |
| 554 | + } |
| 555 | + } |
517 | 556 | }
|
0 commit comments