Skip to content

AnonymousConfigurer not work using Custom DSL #14941

Closed
@shihyuho

Description

@shihyuho

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions