-
Notifications
You must be signed in to change notification settings - Fork 6k
RoleHierarchy
bean does not apply to AuthorityAuthorizationManager
#13911
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi, @bwgjoseph, thanks for the report. #13188 make it so when you use You can make your @Bean
public RoleHierarchy roleHierarchy() {
RoleHierarchyImpl roleHierarchy = new RoleHierarchyImpl();
String roleHierarchyFromMap = """
ROLE_ADMIN > ROLE_STAFF
ROLE_STAFF > ROLE_USER
ROLE_STAFF > ROLE_GUEST
""";
roleHierarchy.setHierarchy(roleHierarchyFromMap);
return roleHierarchy;
}
private AuthorizationManager<RequestAuthorizationContext> haveReadPermission(RoleHierarchy roleHierarchy) {
AuthorityAuthorizationManager<RequestAuthorizationContext> authority = AuthorityAuthorizationManager.hasAnyAuthority("ROLE_USER");
authority.setRoleHierarchy(roleHierarchy);
return authority;
} |
Hi @marcusdacoregio, Thanks for the clarification! In that case, then is it possible to consider having As I have a number Thanks! |
You can consider creating a factory to do that work for you, something like: @Component
class AuthorizationManagerFactory {
private final RoleHierarchy roleHierarchy;
// constructor
public AuthorizationManager<RequestAuthorizationContext> haveReadPermission() {
AuthorityAuthorizationManager<RequestAuthorizationContext> authority = AuthorityAuthorizationManager.hasAnyAuthority("ROLE_USER");
authority.setRoleHierarchy(this.roleHierarchy);
return authority;
}
// ... other methods
} Then, you can: @Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http, AuthorizationManagerFactory factory) throws Exception {
return http
// ...
.authorizeHttpRequests(httpReq -> httpReq.anyRequest().access(factory.haveReadPermission()))
.build();
} |
Sorry, I meant the tedious is having to keep calling the public AuthorizationManager<RequestAuthorizationContext> haveReadPermission() {
AuthorityAuthorizationManager<RequestAuthorizationContext> authority = AuthorityAuthorizationManager.hasAnyAuthority("ROLE_USER");
// not required to set, inferred from the bean hierarchy
// authority.setRoleHierarchy(this.roleHierarchy);
return authority;
} Although, it's not really a big issue to keep re-declaring per method. Just wanted to know if the experience can be better. So I can do something along this line @Bean
public RoleHierarchy roleHierarchy() {
RoleHierarchyImpl roleHierarchy = new RoleHierarchyImpl();
// omitted
return roleHierarchy;
}
public AuthorizationManager<RequestAuthorizationContext> haveReadPermission() {
return AuthorityAuthorizationManager.hasAnyAuthority("ROLE_USER");
}
public AuthorizationManager<RequestAuthorizationContext> haveWritePermission() {
return AuthorityAuthorizationManager.hasAnyAuthority("ROLE_MODERATOR");
} |
Describe the bug
Declaring
RoleHierarchy
in a@Bean
behave differently when declared manually toAuthorityAuthorizationManager
.I'm using
spring-boot-3.1.4
To Reproduce
Please refer to my repo for full example.
I have the following security configuration
And the following test
Pass Scenario
Given the following configuration; manually passing in role hierarchy, the test will pass
click to view test result
Fail Scenario
With the following configuration; configure
RoleHierarchy
as@Bean
and remove manually passing in role hierarchy. The test will fail.click to view test result
Expected behavior
I expect that
AuthorityAuthorizationManager
should have the same behavior whetherRoleHierarchy
is defined as a@Bean
or manually through thesetter
.This issue - #13188 - seem to suggest that this feature should be in working state after
spring-boot 3.1.0
Possible related issue
Sample
Click the link to a GitHub repository with a minimal, reproducible sample.
The text was updated successfully, but these errors were encountered: