Skip to content

Commit

Permalink
RoleVoter Configuration Defaults Prefix Using GrantedAuthorityDefauts
Browse files Browse the repository at this point in the history
Fixes: gh-4876
  • Loading branch information
dongmyo authored and rwinch committed Dec 7, 2018
1 parent 12ab2cc commit 56eb658
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,13 @@ protected AccessDecisionManager accessDecisionManager() {
if (jsr250Enabled()) {
decisionVoters.add(new Jsr250Voter());
}
decisionVoters.add(new RoleVoter());
RoleVoter roleVoter = new RoleVoter();
GrantedAuthorityDefaults grantedAuthorityDefaults =
getSingleBeanOrNull(GrantedAuthorityDefaults.class);
if (grantedAuthorityDefaults != null) {
roleVoter.setRolePrefix(grantedAuthorityDefaults.getRolePrefix());
}
decisionVoters.add(roleVoter);
decisionVoters.add(new AuthenticatedVoter());
return new AffirmativeBased(decisionVoters);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.PermissionEvaluator;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.hierarchicalroles.RoleHierarchy;
import org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl;
import org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor;
Expand Down Expand Up @@ -514,4 +515,42 @@ static class CustomAuthorityService {
public void customPrefixRoleUser() {}
}
}

@Test
@WithMockUser(authorities = "USER")
public void grantedAuthorityDefaultsWithEmptyRolePrefix() {
this.spring.register(EmptyRolePrefixGrantedAuthorityConfig.class).autowire();

EmptyRolePrefixGrantedAuthorityConfig.CustomAuthorityService customService = this.spring.getContext()
.getBean(EmptyRolePrefixGrantedAuthorityConfig.CustomAuthorityService.class);

assertThatThrownBy(() -> this.service.securedUser())
.isInstanceOf(AccessDeniedException.class);

customService.emptyPrefixRoleUser();
// no exception
}

@EnableGlobalMethodSecurity(securedEnabled = true)
static class EmptyRolePrefixGrantedAuthorityConfig {
@Bean
public GrantedAuthorityDefaults ga() {
return new GrantedAuthorityDefaults("");
}

@Bean
public CustomAuthorityService service() {
return new CustomAuthorityService();
}

@Bean
public MethodSecurityServiceImpl methodSecurityService() {
return new MethodSecurityServiceImpl();
}

static class CustomAuthorityService {
@Secured("USER")
public void emptyPrefixRoleUser() {}
}
}
}

0 comments on commit 56eb658

Please sign in to comment.