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

AnonymousConfigurer not work using Custom DSL #14941

Closed
shihyuho opened this issue Apr 22, 2024 · 3 comments
Closed

AnonymousConfigurer not work using Custom DSL #14941

shihyuho opened this issue Apr 22, 2024 · 3 comments
Labels
status: waiting-for-triage An issue we've not yet triaged type: bug A general bug

Comments

@shihyuho
Copy link

shihyuho commented Apr 22, 2024

I'm using

  • Spring Boot 3.2.5
  • Spring Security 6.2.4

Describe the bug

While playing around with Custom DSL, I noticed adding an anonymous configurer does not work

To Reproduce

@Configuration
@EnableWebSecurity
public class Config {
  @Bean
  public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    return http
      .with(new MyCustomDsl(), withDefaults())
      .build();
  }
}

public class MyCustomDsl extends AbstractHttpConfigurer<MyCustomDsl, HttpSecurity> {

  @Override
  public void init(HttpSecurity http) throws Exception {
    http.anonymous(anonymous -> anonymous.principal("myAnonymousUser"));
  }
}

Expected behavior

I expected the anonymous principal to be myAnonymousUser, but the actual result was anonymousUser, which is the default name set by AnonymousConfigurer.

Sample

https://github.com/shihyuho/anonymous-configurer-issue

Additional Notes

Upon tracing the code, the reason appears to be:

In HttpSecurityConfiguration, .anonymous(withDefaults()) is already set once when creating HttpSecurity instance, and in the init method of AnonymousConfigurer, the authenticationFilter is initialized.

As a result, although the principal can still be changed later with custom DSL, the filter is not recreated, which prevents the changes from taking effect.

@shihyuho shihyuho added status: waiting-for-triage An issue we've not yet triaged type: bug A general bug labels Apr 22, 2024
@kse-music
Copy link
Contributor

I think to modify the added Configurer, you need to modify it before building like so

  @Bean
  public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    return http
      .anonymous(anonymous -> anonymous.principal("myAnonymousUser"))
      .with(new MyCustomDsl(), withDefaults())
      .build();
  }

@shihyuho
Copy link
Author

shihyuho commented Apr 24, 2024

I think to modify the added Configurer, you need to modify it before building like so

  @Bean
  public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    return http
      .anonymous(anonymous -> anonymous.principal("myAnonymousUser"))
      .with(new MyCustomDsl(), withDefaults())
      .build();
  }

Thank you for your suggestion @kse-music , but this is not what I am looking for. The document mentions that it is possible to add other configurers to a custom DSL:

image

Therefore, I'm planning to design some custom DSLs targeted at our common scenarios, providing a quick configuration to configure HttpSecurity for developers.

@kse-music
Copy link
Contributor

kse-music commented Apr 26, 2024

If the init method of the custom Configurer supports modifying the configuration of the Configurer that has been added to HttpSecurity, can I understand that because the custom Configurer is initialized last, it will cause inconsistency of the behavior in the init method and the configure method. For example like so:

  @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        return http
                .with(new MyCustomDsl(), withDefaults())
                .build();
    }

    static class MyCustomDsl extends AbstractHttpConfigurer<MyCustomDsl, HttpSecurity> {

        @Override
        public void init(HttpSecurity http) throws Exception {
            http.sessionManagement(c -> c.enableSessionUrlRewriting(true).sessionCreationPolicy(SessionCreationPolicy.STATELESS));
        }

    }

When the SessionManagementConFigurer initializes, the variable enableSessionUrlrewroting = false, sessionPolicy = if_required in init method, but the variable enableSessionUrlrewroting = true, sessionPolicy = STATELESS in configure method.

I think there are still some Configurer like this

@jzheaux I don’t know what I understand, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants