【问题标题】:How to tune spring boot security configuration?如何调整 Spring Boot 安全配置?
【发布时间】:2019-03-11 11:02:07
【问题描述】:

我启用了 Spring Boot 安全性,并在排除列表中添加了一些 url (porterty security.ignored) 在 application.yaml 中。

现在我想在我的配置类中以编程方式将一些新的 url 添加到排除列表中。

我怎样才能做到这一点?

PS 我无法编辑 yaml,我只能编辑配置类。

【问题讨论】:

    标签: spring spring-boot spring-security


    【解决方案1】:

    如果您希望在 java 中排除一些 url 模式而不是 yaml 或属性。

    例子:

    如果需要排除,

    @Override
    protected void configure(HttpSecurity http) throws Exception {
    
        http.authorizeRequests()
            .antMatchers("/login**").permitAll()
            .antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
            .antMatchers("/**").access("hasRole('ROLE_USER')")
            .and()
                .formLogin().loginPage("/login").failureUrl("/login?error")
                    .usernameParameter("username").passwordParameter("password")
            .and()
                .logout().logoutSuccessUrl("/login?logout")
            .and()
                .exceptionHandling().accessDeniedPage("/403")
            .and()
                .csrf();
    
    }
    

    如果需要忽略,

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/authFailure");
    }
    

    希望这是有用的。

    【讨论】:

    • 您似乎不明白我写的内容:我想编辑在 yaml 中定义的属性 security.config。再次。我不想覆盖它,只需添加到现有的 yaml
    • 你说不能编辑 yaml。您需要在 application.yaml 中完成这项工作吗?
    • 属性已在 yaml 中定义。我只需要更改代码。不要覆盖,只是通过添加值来改变
    • 具体是哪个值?理想情况下,Spring Security 将自动配置。如果您需要排除或忽略任何 URL 模式,则需要覆盖 HttpSecurity 或 WebSecurity。
    • @Mebin Joe 我明白了。我会将此问题标记为令人困惑,因为他说他无法编辑 yaml 但希望在 yaml 中对其进行配置。实际上,您可以通过使用 web.ignoring.antmatchers 覆盖方法配置以编程方式忽略 url ......
    猜你喜欢
    • 2014-02-05
    • 2015-02-22
    • 2021-02-21
    • 1970-01-01
    • 2014-10-27
    • 2019-08-25
    • 2016-02-12
    • 2018-12-05
    相关资源
    最近更新 更多