【问题标题】:questions for spring security config春季安全配置问题
【发布时间】:2016-03-10 03:15:13
【问题描述】:

我已经尝试配置 Spring Security。

这是我的安全配置。

@Override
protected void configure(HttpSecurity http) throws Exception {
    // @formatter:off
        http.authorizeRequests()
            .antMatchers("/api/**", "/denied**").permitAll()                
            .anyRequest().denyAll()
            .and()
            .exceptionHandling().accessDeniedPage("/denied")
            .and()
            .csrf().disable();
    // @formatter:on
}

当我访问 url "localhost:8080/admin" 时,我期待 "/denied" 页面。 但它显示默认的 403 页面提供了 spring 安全性。 我的配置有问题吗?

【问题讨论】:

    标签: spring spring-security


    【解决方案1】:

    更新: 将.anyRequest().denyAll() 更改为.anyRequest().authenticated()

    @Override
    protected void configure(HttpSecurity http) throws Exception {
    // @formatter:off
        http.authorizeRequests()
            .antMatchers("/api/**", "/denied**").permitAll()                
            .anyRequest().authenticated()
            .and()
            .exceptionHandling().accessDeniedPage("/denied")
            .and()
            .csrf().disable();
    // @formatter:on
    }
    

    如果您需要使用requiresSecure(),只需在.and()之后添加.requiresChannel().anyRequest().requiresSecure().and()

    【讨论】:

    • 谢谢。如果用户请求未定义的端点,我只想响应 403 状态文本(例如 {"error" : "blahblah"})。所以我设置了 accessDeniedPage("/denied")。但它显示默认的 403 页面。
    • 当你设置.anyRequest().denyAll() 时,accessDeniedPage("/denied") 也被拒绝了。所以如果你只是想响应403,只需将.antMatchers("/**").fullyAuthenticated()更改为.anyRequest().authenticated()或者你可以使用AuthenticationEntryPoint
    猜你喜欢
    • 1970-01-01
    • 2020-09-27
    • 1970-01-01
    • 2020-09-25
    • 2023-03-06
    • 2013-12-22
    • 2018-08-01
    相关资源
    最近更新 更多