AuthenticationSuccessEvent fires multiple times with OAuth2 Resource Server
See original GitHub issueAuthenticationSuccessEvent seems to be fired multiple times for single requests. Not sure if this is expected behavior or not, I will try to create a stripped down app to demonstrate. I just wanted to make sure that this wouldn’t be intended behavior. Code configuration is below in Kotlin:
@EnableWebSecurity
class WebSecurityConfiguration: WebSecurityConfigurerAdapter() {
/**
* Spring Authentication Manager
*/
@Bean
override fun authenticationManager(): AuthenticationManager {
return super.authenticationManager()
}
}
@Configuration
@EnableResourceServer
@EnableWebSecurity
@Order(-1)
class ResourceServerConfig(private val authenticationProvider: MongoDBAuthenticationProvider) : ResourceServerConfigurerAdapter() {
// Constants
private val antPatternsForAllUsers = arrayOf("/actuator/**")
@Autowired
fun configureGlobal(auth: AuthenticationManagerBuilder) {
auth.authenticationProvider(authenticationProvider)
}
override fun configure(http: HttpSecurity) {
http
.addFilterBefore(CorsFilter(), SessionManagementFilter::class.java)
.authorizeRequests().antMatchers(*antPatternsForAllUsers).permitAll().and()
.authorizeRequests().anyRequest().authenticated().and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
.csrf().disable()
}
}
@Component
class AuthListener {
@EventListener
fun authenticationFailed(event: OAuth2AuthenticationFailureEvent) {
System.out.println("OAuth2AuthenticationFailureEvent")
}
@EventListener
fun authenticationSucceeded(event: AuthenticationSuccessEvent) {
System.out.println("AuthenticationSuccessEvent " + event.toString())
}
@EventListener fun authenticationFailed(event: AuthenticationFailureBadCredentialsEvent) {
System.out.println("AuthenticationFailureBadCredentialsEvent " + event.toString())
}
}
Issue Analytics
- State:
- Created 5 years ago
- Comments:10 (4 by maintainers)
Top Results From Across the Web
Spring Security Oauth2 AuthenticationSuccessEvent ...
I want to process some operation after a user login success and failure. The problem is each time I send a request with...
Read more >Spring Security Reference
It is required by applications that use OAuth 2.0 or OpenID Connect Core 1.0, such as client, resource server, and authorization server. The...
Read more >spring-projects/spring-security-oauth - Gitter
hi together, i have an app/service with embedded resource server. and now i have the request also to support an non oauth2 token...
Read more >Spring Security OAuth2 success or failed event listener
Spring @EventListener example AuthenticationSuccessEvent and ... two components one is authentication server and another is resource server.
Read more >Implementing an OAuth 2 authorization server with Spring ...
Spring I/O 2022 - Barcelona, 26-27 MayAfter project Spring Security OAuth has been deprecated, there was a lot of confusion in the community ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Sorry for commenting on closed issue But I have a solution that worked for me. ( Using the debugging mode ):
For the
Client authentication
: authenticationSuccessEvent.getSource() is an instance ofOAuth2Authentication
.For the
User authentication
: authenticationSuccessEvent.getSource() is an instance ofUsernamePasswordAuthenticationToken
.So to execute logic only after user authentication :
I hope It helps.
I know that this issue is closed but it seems to be relevant for the latest version of spring security at the moment. I think that the events should be separated.