Skip to content
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

Closed
wants to merge 1 commit into from

Conversation

kandaguru17
Copy link
Contributor

Closes gh-12783

PR includes

  • Automatic inclusion of any RoleHierarchy Bean into the Configurations related to Method Security

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Oct 28, 2023
@jzheaux
Copy link
Contributor

jzheaux commented Oct 30, 2023

Thanks for the PR, @kandaguru17! Will you please add some tests to confirm that the feature works?

@jzheaux jzheaux self-assigned this Oct 30, 2023
@jzheaux jzheaux added in: config An issue in spring-security-config type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Oct 30, 2023
@jzheaux jzheaux added this to the 6.3.0-M1 milestone Oct 30, 2023
@kandaguru17
Copy link
Contributor Author

Thanks for the PR, @kandaguru17! Will you please add some tests to confirm that the feature works?

done :)

Copy link
Contributor

@jzheaux jzheaux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @kandaguru17! This is shaping up nicely.

In addition to my inline comments, will you please update the copyright years for each file that you edit?

@@ -447,6 +449,24 @@ public void configureWhenBeanOverridingDisallowedThenWorks() {
.autowire();
}

@WithMockUser(roles = "ADMIN")
@Test
public void methodSecurityAdminWhenRoleHierarchyBeanAvailableThenUses() {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 ADMIN can reach ADMIN resources. But with a role hierarchy in place, wouldn't we want to test that ADMIN can reach USER resources as well?

@@ -48,8 +52,13 @@ final class SecuredMethodSecurityConfiguration {
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
static MethodInterceptor securedAuthorizationMethodInterceptor(
ObjectProvider<SecurityContextHolderStrategy> strategyProvider,
ObjectProvider<ObservationRegistry> registryProvider) {
ObjectProvider<ObservationRegistry> registryProvider, ApplicationContext context) {
Copy link
Contributor

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 of ObjectProvider? 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.

Copy link
Contributor

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:

RoleHierarchy roleHierarchy = roleHierarchyProvider.getIfAvailable(NullRoleHierarchy::new);

static class RoleHierarchyConfig {

@Bean
RoleHierarchy roleHierarchy() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will you please make this method static? Such ensures that it is available at the time the static method interceptor beans are published. It also ensures that the interceptor beans are available for bean post-processing.

@@ -50,7 +50,7 @@ public interface MethodSecurityService {
@PermitAll
String jsr250PermitAll();

@RolesAllowed("ADMIN")
@RolesAllowed({ "ADMIN", "USER" })
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

@DerChris173
Copy link
Contributor

Dear @kandaguru17 , thank you for raising this PR. Will you be able to integrate the requested changes? Otherwise I will attempt to pick it up.

@DerChris173
Copy link
Contributor

Dear @jzheaux , as I wasn't able to contribute to the original branch, I forked a new PR with the changes integrated:
#14260

@jzheaux
Copy link
Contributor

jzheaux commented Dec 7, 2023

Thanks again, @kandaguru17! This was merged via #14260.

@jzheaux jzheaux closed this Dec 7, 2023
@jzheaux jzheaux added the status: duplicate A duplicate of another issue label Dec 7, 2023
@kandaguru17
Copy link
Contributor Author

Thanks again, @kandaguru17! This was merged via #14260.

Apologies @jzheaux for the delay,
had other priority work over the last couple of weeks

Many thanks @DerChris173 , for amending the comments in the PR :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: config An issue in spring-security-config status: duplicate A duplicate of another issue type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use RoleHierarchy bean in Method Security Configuration
4 participants