【问题标题】:log access denied events with Spring Security and J2EE container authentication使用 Spring Security 和 J2EE 容器身份验证记录访问被拒绝事件
【发布时间】:2023-03-31 13:10:02
【问题描述】:

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

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

我尝试的是:

  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
        }
    }
    

-不会被调用

  1. ApplicationListener - 我可以看到它在上下文关闭时被调用,所以它被正确注册但在授权错误时没有被调用。

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

【问题讨论】:

    标签: spring-security


    【解决方案1】:

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

    否则,第一个最简单的方法可以正常工作。希望我的错误对以后的人有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-31
      • 2018-01-06
      • 2016-03-15
      • 1970-01-01
      • 2017-11-28
      • 2012-02-15
      • 2019-12-12
      • 2012-07-12
      相关资源
      最近更新 更多