Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何通过application.properties禁用spring安全性?_Java_Spring_Spring Security - Fatal编程技术网

Java 如何通过application.properties禁用spring安全性?

Java 如何通过application.properties禁用spring安全性?,java,spring,spring-security,Java,Spring,Spring Security,我正在尝试使用密码哈希加密激活基本身份验证 @Configuration //gets picked up automatically by spring-boot public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception {

我正在尝试使用密码哈希加密激活
基本身份验证

@Configuration //gets picked up automatically by spring-boot
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
          auth.userDetailsService(details).passwordEncoder(new BCryptPasswordEncoder());
    }
}
我希望身份验证仅在生产中处于活动状态。因此,我首先尝试停用它,使用:

security.basic.enabled=false

结果:应用程序仍然安全。我将看到用户名/密码屏幕


但是我怎样才能禁用身份验证呢?

要禁用spring安全自动配置,请完全使用以下属性

spring.autoconfigure.exclude[0]=org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration

要在不同的环境中做不同的事情,您应该使用

假设您只在生产环境中需要安全配置bean,那么您应该在启用特定概要文件时将其标记为有条件地加载

@Configuration //gets picked up automatically by spring-boot
@Profile("Production")
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
          auth.userDetailsService(details).passwordEncoder(new BCryptPasswordEncoder());
    }
}
特定于配置文件的配置在名为
application-{Profile}.properties
bootstrap-{Profile}.properties
文件的属性文件中完成

现在,由于安全配置bean用profile
Production
标记,因此只有在启用了
Production
profile时才会加载它

@Configuration //gets picked up automatically by spring-boot
@Profile("Production")
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
          auth.userDetailsService(details).passwordEncoder(new BCryptPasswordEncoder());
    }
}
要启用配置文件,必须设置以下属性

#One or more profiles can be active simultaneously
spring.profiles.active=Production,Dev,Local
此属性可以在公共属性文件
application.properties
中编辑(与有条件加载的配置文件特定属性文件(如
application Production.properties
)不同,始终加载),也可以作为环境变量提供,如下所示

On Windows
set SPRING_PROFILES_ACTIVE=Production
On Unix
export SPRING_PROFILES_ACTIVE=Production
可以使用spring支持的一百万种其他方式来加载属性。要查看所有方法的列表,请通过此链接

最后,要禁用默认的Spring安全性(基本)自动配置,可以使用以下属性

security.basic.enabled=false
management.security.enabled=false #For actuator

以上内容应位于您希望禁用Spring Security自动配置的配置文件特定属性文件中。

使用
条件属性解决:

@Configuration
@ConditionalOnProperty("security.basic.enabled")
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

}

如前所述,我已经尝试使用
security.basic.enabled=false
,这应该正常工作,但在使用
websecurityConfigureAdapter
时不会工作。这是因为
websecurityConfigureAdapter
仍在创建一个bean,必须有条件地加载它,正如在答案的开头所提到的,我扩展了WebSecurity配置适配器两次,并按照上述方式工作。工作得很有魅力。