【发布时间】: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 注释在其余端点上具有角色。
不管我做什么,我似乎无法为授权创建自定义处理程序(即用户成功登录但没有访问特定端点的正确角色)错误事件。
我尝试的是:
带有
@ExceptionHandler(value = AccessDeniedException.class)的异常处理程序 - 不会被调用。我明白这是设计使然,好的。-
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 } }
-不会被调用
- ApplicationListener - 我可以看到它在上下文关闭时被调用,所以它被正确注册但在授权错误时没有被调用。
我只需要一个简单的处理程序来记录不成功的授权事件。
【问题讨论】:
标签: spring-security