-
Notifications
You must be signed in to change notification settings - Fork 6k
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
Use available RoleHierachy Bean for MethodSecurity Config #14057
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,7 +50,7 @@ public interface MethodSecurityService { | |
@PermitAll | ||
String jsr250PermitAll(); | ||
|
||
@RolesAllowed("ADMIN") | ||
@RolesAllowed({ "ADMIN", "USER" }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will you please separate this into a user method, like you did with the others? In this way, the test methods will read more consistently. |
||
String jsr250RolesAllowed(); | ||
|
||
@Secured({ "ROLE_USER", "RUN_AS_SUPER" }) | ||
|
@@ -68,6 +68,9 @@ public interface MethodSecurityService { | |
@PreAuthorize("hasRole('ADMIN')") | ||
void preAuthorizeAdmin(); | ||
|
||
@PreAuthorize("hasRole('USER')") | ||
void preAuthorizeUser(); | ||
|
||
@PreAuthorize("hasPermission(#object,'read')") | ||
String hasPermission(String object); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,8 @@ | |
import org.springframework.security.access.annotation.Jsr250BusinessServiceImpl; | ||
import org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler; | ||
import org.springframework.security.access.expression.method.MethodSecurityExpressionHandler; | ||
import org.springframework.security.access.hierarchicalroles.RoleHierarchy; | ||
import org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl; | ||
import org.springframework.security.authorization.AuthorizationDecision; | ||
import org.springframework.security.authorization.AuthorizationEventPublisher; | ||
import org.springframework.security.authorization.AuthorizationManager; | ||
|
@@ -447,6 +449,24 @@ public void configureWhenBeanOverridingDisallowedThenWorks() { | |
.autowire(); | ||
} | ||
|
||
@WithMockUser(roles = "ADMIN") | ||
@Test | ||
public void methodSecurityAdminWhenRoleHierarchyBeanAvailableThenUses() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think I'm understanding these tests yet. They seem to be testing that |
||
this.spring.register(RoleHierarchyConfig.class, MethodSecurityServiceConfig.class).autowire(); | ||
this.methodSecurityService.preAuthorizeAdmin(); | ||
this.methodSecurityService.secured(); | ||
this.methodSecurityService.jsr250RolesAllowed(); | ||
} | ||
|
||
@WithMockUser | ||
@Test | ||
public void methodSecurityUserWhenRoleHierarchyBeanAvailableThenUses() { | ||
this.spring.register(RoleHierarchyConfig.class, MethodSecurityServiceConfig.class).autowire(); | ||
this.methodSecurityService.preAuthorizeUser(); | ||
this.methodSecurityService.securedUser(); | ||
this.methodSecurityService.jsr250RolesAllowed(); | ||
} | ||
|
||
private static Consumer<ConfigurableWebApplicationContext> disallowBeanOverriding() { | ||
return (context) -> ((AnnotationConfigWebApplicationContext) context).setAllowBeanDefinitionOverriding(false); | ||
} | ||
|
@@ -627,4 +647,17 @@ Authz authz() { | |
|
||
} | ||
|
||
@Configuration | ||
@EnableMethodSecurity(jsr250Enabled = true, securedEnabled = true) | ||
static class RoleHierarchyConfig { | ||
|
||
@Bean | ||
RoleHierarchy roleHierarchy() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will you please make this method |
||
RoleHierarchyImpl roleHierarchyImpl = new RoleHierarchyImpl(); | ||
roleHierarchyImpl.setHierarchy("ADMIN > USER"); | ||
return roleHierarchyImpl; | ||
} | ||
|
||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason to use
ApplicationContext
instead ofObjectProvider
? I'd prefer to avoid future maintenance questions, if possible, by having this feature follow the same patterns other features.It also makes this beans dependencies clearer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It also may ultimately be less code to maintain: