How do you get the Request URL in spring Boot from an AuthorizationFailureEvent

Viewed 40

We are using Spring Boot 2.5.2 with Keycloak 14.0. I am trying to log the authorization events in addition to the URL the user called.

I am trying to follow Spring Boot Authentication Auditing Support. The code to retrieve the Request URL is:

private void onAuthorizationFailureEvent(
  AuthorizationFailureEvent event) {
    Map<String, Object> data = new HashMap<>();
    data.put(
      "type", event.getAccessDeniedException().getClass().getName());
    data.put("message", event.getAccessDeniedException().getMessage());
    data.put(
      "requestUrl", ((FilterInvocation)event.getSource()).getRequestUrl() );
    
    if (event.getAuthentication().getDetails() != null) {
        data.put("details", 
          event.getAuthentication().getDetails());
    }
    publish(new AuditEvent(event.getAuthentication().getName(), 
      AUTHORIZATION_FAILURE, data));
}

When I attempt this I am getting a ClassCastException when calling event.getSource(). The source seems to be a ReflectiveMethodInvocation (to my Controller) and not a FilterInvocation. Can anybody explain this? How do I get the request url?

0 Answers
Related