-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
StackOverflowException
when adapter's AuthenticationManager
gets published as a bean
#10419
Comments
AuthenticationManager
gets published as a beanStackOverflowException
when adapter's AuthenticationManager
gets published as a bean
Hi @goto1134, thanks for the bug report. I have a few questions that might help clarify the issue in your case:
|
|
Thanks for the feedback, @goto1134. That's very helpful context. I was able to put together a sample based on your provided snippet, and reproduced the problem you're having. Ultimately, we should be able to inject the However, you may consider a workaround in the meantime, which is to expose your own @Bean
fun jwtAuthenticationProvider(jwtDecoder: JwtDecoder, jwtAuthenticationConverter: JwtAuthenticationConverter): JwtAuthenticationProvider {
val jwtAuthenticationProvider = JwtAuthenticationProvider(jwtDecoder)
jwtAuthenticationProvider.setJwtAuthenticationConverter(jwtAuthenticationConverter)
return jwtAuthenticationProvider
} This bean gets used by the configurer from the DSL. It can also be injected wherever you would have used the @Bean
fun authenticationManager(jwtAuthenticationProvider: JwtAuthenticationProvider): AuthenticationManager {
val anonymousAuthenticationProvider = AnonymousAuthenticationProvider(UUID.randomUUID().toString())
return ProviderManager(anonymousAuthenticationProvider, jwtAuthenticationProvider)
} This is essentially what the framework will build and pass into the |
Upon further investigation, this issue appears to be a duplicate of #8369, though the stack trace is slightly different. In fact, it's possible the stack trace will be different most of the time due to the recursive @goto1134, let me know if you have any questions with the above workaround. I'm going to close this as a duplicate. |
Describe the bug
If you publish an
AuthenticationManager
withorg.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter#authenticationManagerBean
, you will get aStackOverflowException
.To Reproduce
Use
org.springframework.boot:spring-boot-starter-oauth2-resource-server:2.5.3
Publish the security config authentication manager bean as shown below:
Call any method with an invalid JWT token
Get StackOverflowException with the following calls:
Expected behaviour
Get an authentication error without the stack overflow.
Sample
None, sorry
Why does it happen
WebSecurityConfigurerAdapter
configures the publishedAuthenticationManager
bean as a parent for theAuthenticationManagerBuilder
. The builder then creates theProviderManager
, which will have ourAuthenticationManager
bean as a parent. This configuration creates a circular dependency.If all of the configured
AuthenticationProviders
fail to authenticate, theProviderManager
will call its parent'sauthenticate
method. The bean will call theProviderManager
again and so on. The following code is taken from theProviderManager
class to illustrate the algorithm:An ugly way to make it work 1
Add this line to your
configure
method:http.getSharedObject(AuthenticationManagerBuilder::class.java).parentAuthenticationManager(null)
.All together:
An ugly way to make it work 2*
Оverride this method in your
SecurityConfig
:The text was updated successfully, but these errors were encountered: