如何在Spring Boot 2中禁用安全性?

5
在spring-boot-1.x中,我使用以下配置来在开发模式下禁用基本安全性:
application.properties:
security.basic.enabled=false

application-test.properties:
security.basic.enabled=true

application-prod.properties:
security.basic.enabled=true

从spring-boot-2.x开始,该属性不再得到支持。我该如何实现相同的配置(在默认配置文件中禁用任何安全相关的功能和配置)?


你需要编写配置类。 - GolamMazid Sajib
你能提供一个禁用全局安全性的配置类的示例吗?并且如何仅为默认配置文件加载它? - membersound
1个回答

7

这是配置类。这里允许所有的URL:

@Configuration
@ConditionalOnProperty(value = "app.security.basic.enabled", havingValue = "false")
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable()
            .authorizeRequests()
            .antMatchers("/**").permitAll()
            .anyRequest().authenticated();
}
}

那么,我该如何在默认配置文件中自动启用它,在测试/生产环境中禁用它呢? - membersound
我知道,在版本2中,Spring Security没有属性配置。你可以通过管理来读取属性文件。 - GolamMazid Sajib
我添加了@ConditionalOnProperty(value = "app.security.basic.enabled", havingValue = "false"),在这种情况下配置将会被加载。如果没有更好的方法,至少它能够工作。 - membersound

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接