Skip to content

Invalid OAuth2 login attempts don't emit a corresponding ApplicationEvent #7793

Closed
@sdavids13

Description

@sdavids13

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 and AbstractAuthorizationEvent 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:

  "classname": "org.springframework.security.oauth2.server.resource.web.BearerTokenAuthenticationFilter",
  "method": "doFilterInternal",
  "file": "BearerTokenAuthenticationFilter.java",
  "line": 135,
  "logger_name": "org.springframework.security.oauth2.server.resource.web.BearerTokenAuthenticationFilter",
  "level": "DEBUG",
  "message": "Authentication request for failed!",
  "stack_trace": "org.springframework.security.oauth2.core.OAuth2AuthenticationException: An error occurred while attempting to decode the Jwt: Signed JWT rejected: Invalid signature
.......

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:

implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("org.springframework.boot:spring-boot-starter-oauth2-resource-server")
...

WebSecurityConfigurerAdapter:

http
    .cors()
    .and()
    .authorizeRequests()
    .antMatchers(AUTH_WHITELIST)
    .permitAll()
    .anyRequest()
    .hasAnyAuthority("ROLE_REDACTED", .....)
    .and()
    .oauth2ResourceServer()
    .jwt()
    .jwtAuthenticationConverter(authenticationConverter);

Version

Spring Boot 2.2.2.RELEASE -> (Spring Security 5.2.1.RELEASE)

Metadata

Metadata

Assignees

Labels

in: oauth2An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions