我正在使用 spring-security-javaconfig 库来实现 spring 安全。如果我使用的是 xml 配置文件,我会使用类似这样的东西来定义自定义访问被拒绝页面:
<http auto-config="true">
<intercept-url pattern="/admin*" access="ROLE_ADMIN" />
<access-denied-handler ref="accessDeniedHandler"/>
</http>
到目前为止,这是我的安全配置类:
@Configuration
@EnableWebSecurity
public class SecurityConfigurator extends WebSecurityConfigurerAdapter {
@Override
protected void registerAuthentication(AuthenticationManagerBuilder auth)
throws Exception {
auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
auth.inMemoryAuthentication().withUser("admin").password("password").roles("ADMIN");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeUrls().antMatchers( "/admin").hasRole("ADMIN");
}
}
最佳答案
我想这应该可以解决问题:
HttpSecurity http = ...
http.exceptionHandling().accessDeniedHandler(myAccessDeniedHandler);
关于java - 如何在 spring-security-javaconfig 中添加拒绝访问处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18154864/