使用Spring Security和J2EE容器身份验证记录访问被拒绝的事件

时间:2015-06-09 06:43:12

标签: spring-security

我已将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注释在其余端点上具有角色。

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

我尝试的是:

  1. @ExceptionHandler(value = AccessDeniedException.class)的异常处理程序 - 不会被调用。我明白这是设计的,好的。

  2. AuthenticationEntryPoint配置为 http.exceptionHandling().authenticationEntryPoint(new RestAuthenticationEntryPoint())

    @Component( "restAuthenticationEntryPoint" )
    public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {
        @Override
        public void commence( HttpServletRequest request, HttpServletResponse response,
                              AuthenticationException authException ) throws IOException {
                              // logging
        }
    }
    
  3. - 不被称为

    1. ApplicationListener - 我可以看到它在上下文关闭时调用,因此它已正确注册但未在授权错误时调用。
    2. 我只需要一个简单的处理程序来记录不成功的授权事件。

1 个答案:

答案 0 :(得分:0)

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

否则,第一种最简单的方法可以正常工作。希望我的错误将来会帮助某人