在 Spring Boot RESTful API 上记录授权失败



在 Spring Boot 中,在 RESTful API 上记录失败的授权尝试的最佳方法是什么,即在下面,如果用户没有管理员角色,默认情况下 Spring 不会记录访问此方法的失败尝试:

    @PreAuthorize("hasAuthority('admin')")
    @RequestMapping(value = "/getVersion", method = RequestMethod.GET)
    public ResponseEntity<?> printVersion() {
        logger.info("Received get request for version");
        return new ResponseEntity<String>(applicationDetails.getAppVersion(), HttpStatus.OK);
    }

我尝试使用ApplicationListener<AuthorizedEvent>,虽然类似的方法适用于ApplicationListener<AbstractAuthenticationEvent>,但它似乎不适用于AuthorizedEvents

您使用了错误的事件,请参阅AuthorizationFailureEvent

指示安全对象调用失败,因为无法授权主体处理请求。

此事件可能由于AccessDecisionManagerAfterInvocationManager而引发。