我已将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>
我尝试的是:
@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
}
}
- 不被称为
我只需要一个简单的处理程序来记录不成功的授权事件。
答案 0 :(得分:0)
我完全忘记了在web.xml中列出了允许的角色以及j2ee容器身份验证的工作原理。因此,任何没有这些角色中的至少一个的用户都被容器拒绝。
否则,第一种最简单的方法可以正常工作。希望我的错误将来会帮助某人