Spring security 使用Spring安全性和J2EE容器身份验证记录拒绝访问的事件

Spring security 使用Spring安全性和J2EE容器身份验证记录拒绝访问的事件,spring-security,Spring Security,我已将spring security配置为 @Configuration @EnableWebSecurity @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = false) public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSe

我已将spring security配置为

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = false)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
        .authorizeRequests()
        .anyRequest().authenticated()
        .and()
        .jee()
        .mappableRoles("ROLE1", "ROLE2");
    }
}
然后,
@Secured
在rest端点上添加角色的注释

不管我做什么,我似乎无法为授权创建自定义处理程序(即,用户成功登录但没有访问特定端点的正确角色)错误事件

我尝试的是:

  • 无法调用具有
    @ExceptionHandler(value=AccessDeniedException.class)
    的异常处理程序。我知道这是故意的,好吧

  • AuthenticationEntryPoint配置为
    http.exceptionHandling().authenticationEntryPoint(新的重新验证EntryPoint())

  • -没有人打电话

  • ApplicationListener-我可以看到它在上下文关闭时被调用,因此它已正确注册,但在授权错误时未被调用

  • 我只需要一个简单的处理程序来记录不成功的授权事件。

    我完全忘记了web.xml中列出了允许的角色,以便j2ee容器身份验证工作。因此,任何没有至少一个角色的用户都会被容器拒绝

    否则,第一个最简单的方法可以很好地工作。希望我的错误能对将来的人有所帮助

    @Component( "restAuthenticationEntryPoint" )
    public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {
        @Override
        public void commence( HttpServletRequest request, HttpServletResponse response,
                              AuthenticationException authException ) throws IOException {
                              // logging
        }
    }