Repository navigation
Description
With the following issues closed we've added the ability to configure the security of an application without needing the WebSecurityConfigurerAdapter.
Related issues:
- Configure HTTP Security without extending WebSecurityConfigurerAdapter #8804
- Configure WebSecurity without WebSecurityConfigurerAdapter #8978
- HttpSecurity DSL should accept an AuthenticationManager #10040
- Add LDAP AuthenticationManager factory #10138
Background
Below is additional information on why WebSecurityConfigurerAdapter is being deprecated.
WebSecurityConfigurerAdapter and HttpSecurity DSL were created to work around spring-projects/spring-framework#18353 Unfortunately, WebSecurityConfigurerAdapter design is flawed causing quite a few problems that cannot be properly fixed.
Does Not Expose Beans
The first is that rather than exposing Beans, it silently creates objects that cannot be used by the underlying application or by Spring Boot for auto configuration. This makes it difficult for Boot to know if an AuthenticationManager needs to be created or not. It also makes it difficult for users to leverage an AuthenticationManager in their own code as a Bean.
Instead, it is preferred that users expose an AuthenticationManager (or AuthenticationProvider or UserDetailsService) as a Bean so it can be used by the rest of the application.
Beans Cannot be Injected via Method Arguments
Another issue with WebSecurityConfigurerAdapter is that the static nature of the method signatures means that Security configuration does not follow best practices of passing in the Beans by method arguments. Instead, dependencies must be injected into member variables or resolved through method references.
While there is nothing inherently wrong with this, it doesn't follow best practices causing several limitations.
When using method arguments for Bean dependencies Spring knows exactly which Beans are dependencies before Object creation takes place. This means the minimal Object graph can be created. When using member variables, @Autowired methods on the configuration class, etc Spring needs to initialize the entire @Configuration to resolve the Bean. Similar problems happen when using method references to resolve Beans.
Not being able to determine the minimal dependencies of a Bean leads to BeanCurrentlyInCreationException (e.g. gh-4489 ) and AlreadyBuiltException being thrown (e.g. gh-3916). Again, this happens in large part due to not having as much information about the Object graph since parameters are not used to inject the Bean dependencies.
Activity
linghengqian commentedon Feb 11, 2022
Thanks for your efforts, I noticed that https://docs.spring.io/spring-security/reference/5.7.0-M1/servlet/configuration/java.html documentation has not been updated, so I am a little confused, something like this How should the scene migrate
WebSecurityConfigurerAdaptertoSecurityFilterChain. TheTokenAuthFilterhere extendsBasicAuthenticationFilter, so the construction requires anAuthenticationManagerparameter.eleftherias commentedon Feb 11, 2022
Thanks for bringing this up @linghengqian.
The reference documentation was updated as part of gh-10003 and will be available in 5.7.0-M2.
At the moment it's available in the latest snapshot documentation https://docs.spring.io/spring-security/reference/5.7/servlet/configuration/java.html.
We will also share a blog post on how to migrate common use-cases when we release 5.7.0-M2.
The configuration you share above can be rewritten as follows:
Deprecate WebSecurityConfigurerAdapter
eleftherias commentedon Feb 17, 2022
Closed via e97c643
Deprecate WebSecurityConfigurerAdapter
eleftherias commentedon Feb 21, 2022
The related blog post containing common use-cases https://spring.io/blog/2022/02/21/spring-security-without-the-websecurityconfigureradapter
NotFound403 commentedon Feb 22, 2022
huge change
tha2015 commentedon Feb 25, 2022
It is not a good practice to both modify the input parameter and return a value. I would not write some method like this if I have a choice.
linghengqian commentedon Feb 25, 2022
If you need to configure Multiple HttpSecurity, you'll find WebSecurityConfigurerAdapter not intuitive to the code and add a layer of burden. To be honest, WebFlux's thinking in this area is pretty good and worth changing.
tha2015 commentedon Feb 25, 2022
configure() method returns void, so I understand that it will create side effects. The method securityFilterChain() returns a value, so it should not create side effects (changing input). Is there any chance to improve this method?
17 remaining items
jackgon7 commentedon Jun 3, 2022
Have you managed to solve the error? Facing the same problem now.
alex-martel commentedon Jun 3, 2022
@jackgon7 Yes I was able to solve it thanks to @Sparklll which made me double check the code with his last comment.
My custom UserDetailsService was already annotated with
@Service. Defining the bean in the@Configurationwas not ok/redundant. I have no idea why/how it could work before but anyway. I have removed that bean creation from my security configuration then it worked.Please also be cautious where you autowire your AuthenticationManager. Due do somewhat bad design I had to annotate it with
@Lazyfor things to work. It was a smelly design which I have adapted to not have to have it lazy now, but you may have to double check that as well.jackgon7 commentedon Jun 3, 2022
Thanks for the reply. I found out the problem was due to I'm using AuthenticationManager in my @RestController as you mentioned. I managed to solve it by using returning a ProviderManager in the AuthenticationManager @bean in my @configuration as follow:
But I'm not quite sure if it is best practice though. I'm still quite new in the Spring environment.
linghengqian commentedon Jun 3, 2022
@jackgon7 Your configuration confuses me a bit, and while I'd suggest hanging up to Stackoverflow to continue the discussion, there's actually no need to bother if you just need to instantiate an
AuthenticationManagerbean forSecurityFilterChainto use, becauseAuthenticationManagerBuilderhas always been in the context ofSpring Security. Let an example of a simple modification.mohantangirala commentedon Jun 6, 2022
Hello, Do you have any documentation for SAML2 config?
hannah23280 commentedon Jul 13, 2022
Hi everyone,
Any documents that show sample codes of how to add our own custom AuthenticationProviders given that the WebSecurityConfigurerAdapter is now deprecated
Rutito2010 commentedon Aug 11, 2022
Thanks for this explanation, i been with this problem for 3 days and all i got to do was remove the @bean anotation from my inmemoryuserdetails i have in the websecurityconfig
hnpanther commentedon Aug 11, 2022
dmak commentedon Oct 20, 2022
Dear all,
I am facing the issue which is not obvious to resolve just by reading the documentation. I have defined the custom configuration in my Spring Boot application like below:
The above method is called, however has no effect as
SecurityFilterChaininstance created byOAuth2SecurityFilterChainConfigurationis used instead. Debug log:Is it expected that the bean
CustomSecurityConfig.filterChaindoes not participate inDefaultWebSecurityConditionevaluation? Or the issue withDefaultWebSecurityConditionis that the instance ofWebSecurityConfigurerAdapteris not in the context anymore (as to this issue it is deprecated)?The solution was to add
Order(1):and the application started to work as expected.
The documentation I read before posting:
UPD: Posted to stackoverflow.
ottlinger commentedon Nov 8, 2022
I'm running into issues as I have a locally defined AuthenticationFilter implementation.
The now deprecated/old way was:
But I'm unable to wire in my AuthenticationManager instance as it is null.
The definition of the filter happens in my
which is why I'm unable to wire in the authentication manager as suggested above.
@eleftherias unfortunately I am unable to find a comparable issue on StackOverflow. Hoping you can help somehow or point me to an example that works with SpringBoot3 and Spring6. Thanks.
kyle-copeland commentedon Nov 18, 2022
Thank you so much for posting this! It saved me big time.
halilkrkn commentedon Jan 19, 2023
Hi, If you are living to problem about using AuthenticationManagerBuilder without extends WebSecurityConfigurerAdapter and taken a problem like
Error creating bean with name 'webSecurityConfig': Requested bean is currently in creation: Is there an unresolvable circular reference?You can use add the
spring.main.allow-circular-references=trueintoapplication.properties.xml.OR, you can use this method
maksymgendin commentedon May 10, 2023
What about default configuration of
HttpSecurity? Currently theWebSecurityConfigurerAdaptersets it:How can I disable these defaults without extending
WebSecurityConfigurerAdapterand using it's parameterized constructor?DenkovychOleh commentedon Aug 11, 2023
Hello, I have problem
My code:
`
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;
@configuration
@EnableWebSecurity
public class SecurityConfig {
@bean
public PasswordEncoder encoder() {
return NoOpPasswordEncoder.getInstance();
}
@bean
public InMemoryUserDetailsManager userDetailsService() {
UserDetails user = User.builder()
.username("user1")
.password("user1")
.roles("ADMIN")
.build();
return new InMemoryUserDetailsManager(user);
}
}
`
When I send request in Postman I got 403 Error
ottlinger commentedon Aug 16, 2023
When I followed the workarounds proposed in this issue I ran into #13620 - does anyone else have similar issues?
SpringBoot2 worked fine but SpringBoot3 does not work (the issue links to a reproducer app on Github / sb3-filter-problem) with a combination of a 3-part-login and SB3.