java - 如何在 spring-security-javaconfig 中添加拒绝访问处理程序

标签 java spring-security spring-java-config

我正在使用 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/

相关文章:

java - Spring是否用java配置中的现有bean替换方法调用?

java - 如何在 Java 中运行轮询阻塞消费者?

java - GSON GraphAdapterBuilder 接口(interface)失败

spring-boot - Spring Security : what function does AuthenticationManager. authenticate() 执行

Spring测试@async方法

java - 从 @ComponentScan 中排除测试类中的配置

java - Spring Boot 为自定义错误页面返回 200 状态代码

java - JFreeChart 插值值

mysql - spring security 3.0 无法获取/处理用户凭据

grails - 为Grails功能测试禁用 Spring 安全性吗?