首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SpringBoot2.0.x禁用某些配置文件的安全性

SpringBoot2.0.x禁用某些配置文件的安全性
EN

Stack Overflow用户
提问于 2018-03-13 14:28:05
回答 7查看 30.3K关注 0票数 16

在SpringBoot1.5.x中,我已经配置了安全性,并且在某些配置文件(例如本地配置文件)中,我将security.basic.enabled=false行添加到.properties文件中,以禁用该配置文件的所有安全性。我正在尝试迁移到新的Spring 2,其中删除了该配置属性。如何在SpringBoot2.0.x中实现相同的行为(不使用此属性)?

我已经读过Spring-Boot-Security-2.0安全性-弹簧中的变化-引导-2-0-m4了,没有关于这个属性的任何内容。

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2018-03-13 17:02:48

您必须添加自定义的Security配置,请参阅弹簧启动参考指南

28.1 MVC安全 默认的安全配置在SecurityAutoConfigurationUserDetailsServiceAutoConfiguration中实现。SecurityAutoConfiguration为web安全导入SpringBootWebSecurityConfigurationUserDetailsServiceAutoConfiguration配置身份验证,这也与非web应用程序相关。要完全关闭默认的web应用程序安全配置,可以添加WebSecurityConfigurerAdapter类型的bean (这样做不会禁用UserDetailsService配置或Actuator的安全性)。

例如:

代码语言:javascript
复制
@Configuration
public class ApplicationSecurity extends WebSecurityConfigurerAdapter {

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

若要仅对配置文件使用配置,请向类添加@Profile。如果要按属性启用它,请将ConditionalOnProperty添加到类中。

票数 17
EN

Stack Overflow用户

发布于 2018-03-13 17:01:49

这就是我最后是如何解决这个问题的。下面是我的安全配置在SpringBoot1.5.x中的一个示例。使用属性security.basic.enabled=false禁用了安全性

代码语言:javascript
复制
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

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

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().authorizeRequests()
                .anyRequest().authenticated()
                .and().httpBasic();
    }
}

由于security.basic.enabled在Spring 2中被删除(但仍然保留为属性名称),所以我最终使用security.enabled作为自定义属性。下面是我的配置在Spring 2中的一个示例:

代码语言:javascript
复制
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Value("${security.enabled:true}")
    private boolean securityEnabled;

    @Override
    public void configure(WebSecurity web) throws Exception {
        if (securityEnabled)
            web.ignoring().antMatchers("/upload/**");
        else
            web.ignoring().antMatchers("/**");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        if (securityEnabled)
            http.csrf().disable().authorizeRequests()
                    .anyRequest().authenticated()
                    .and().httpBasic();
    }
}
票数 10
EN

Stack Overflow用户

发布于 2018-12-05 10:35:48

在spring 2中还有另一个禁用安全性的选项。

代码语言:javascript
复制
@EnableAutoConfiguration(exclude = {SecurityAutoConfiguration.class})

将其添加到主类中

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49258766

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档