-
Notifications
You must be signed in to change notification settings - Fork 6k
Invalid OAuth2 login attempts don't emit a corresponding ApplicationEvent #7793
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
Comments
I attempted to get the event publisher to recognize the OAuth2AuthenticationException by adding the following bean to the Application Context:
I continued to get log messages saying:
I then wired in the event publisher into the WebSecurityConfigurerAdapter via:
Still that didn't work... I then tried to set the properties again in the constructor which still doesn't work either:
All produced the same log message saying Long story short.. there seems to be 2 problems:
|
@sdavids13, thanks for the report, and sorry to hear about all the debugging woes! I did a bit of digging and found that the DefaultAuthenticationEventPublisher eventPublisher = objectPostProcessor
.postProcess(new DefaultAuthenticationEventPublisher()); It would be better if this first looked for an In the meantime, the following workaround should do the trick: private ProviderManager providerManager() {
JwtDecoder jwtDecoder = JwtDecoders.fromIssuerLocation(...);
JwtAuthenticationProvider authenticationProvider =
new JwtAuthenticationProvider(jwtDecoder);
authenticationProvider.setJwtAuthenticationConverter(jwtAuthenticationConverter);
ProviderManager providerManager = new ProviderManager
(Arrays.asList(authenticationProvider));
providerManager.setAuthenticationEventPublisher(fooPublisher);
return providerManager;
} And then: protected void configure(HttpSecurity http) throws Exception {
http
.cors(withDefaults())
.authorizeRequests(authz -> authz
.antMatchers(AUTH_WHITELIST).permitAll()
.anyRequest().hasAnyAuthority("ROLE_REDACTED", .....)
)
.oauth2ResourceServer(oauth2 -> oauth2
.jwt(j -> j
.authenticationManager(providerManager())
)
);
} |
Looks like the work-around you provided works - thanks! Might I also suggest you add the following code as well:
|
@sdavids13 I'm glad the workaround worked! As for adding the exception by default, my concern is that not all One way might be to simplify how custom mappings are added. It seems reasonable to overload public void setAdditionalExceptionMappings(Map<
Class<? extends AuthenticationException>,
Class<? extends AbstractAuthenticationFailureEvent>> additionalExceptionMappings) And then you could instead do: @Bean
AuthenticationEventPublisher eventPublisher(ApplicationEventPublisher application) {
AuthenticationEventPublisher authentication =
new DefaultAuthenticationEventPublisher(application);
authentication.setAdditionalExceptionMappings(
Collections.singletonMap(OAuth2AuthenticationException.class, FooEvent.class));
return authentication;
} Would you be interested in submitting a PR for that feature? |
I should have mentioned that I do also agree that we should probably have some kind of OAuth mapping there - we probably just need a more specific exception type. |
@sdavids13 Feel free to take the PR for a spin to see if it suits your needs. It introduces |
Will this issue will be a solution for Spring Security Oauth2's deprecated OAuth2AuthenticationFailureEvent class? Right now the deprecated class refers to the Migration Guide but there doesn't seem to have a replacement solution. Thank you. |
@lsko, this ticket will address bearer token errors only, mapping them to There is no plan at this time to port over |
Summary
I would like to log requests for invalid attempts for authenticating via OAuth 2.0 bearer tokens (via JWT) for which I have ApplicationListeners for both
AbstractAuthenticationEvent
andAbstractAuthorizationEvent
events which log failures as they are seen. Currently when presented with invalid JWT bearer tokens a 401 HTTP response is provided but no Application Listener events are fired that corresponds to the error response.Actual Behavior
When submitting a request with a bad JWT bearer token the application responds with a 401 response code but no ApplicationListener event is fired that correlates to the bad response. Currently I see an error that says:
Upon inspection of the BearerTokenAuthenticationFilter it seems to merely manipulate the HTTPServletResponse in the default implementation instead of producing any application events.
Expected Behavior
On malformed JWT bearer token error responses a corresponding application event should be sent that implements an
AbstractAuthenticationFailureEvent
.Configuration
Dependencies:
WebSecurityConfigurerAdapter
:Version
Spring Boot 2.2.2.RELEASE -> (Spring Security 5.2.1.RELEASE)
The text was updated successfully, but these errors were encountered: