Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.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配置禁用Spring安全性?_Java_Spring_Spring Security - Fatal编程技术网

通过Java配置禁用Spring安全性?

通过Java配置禁用Spring安全性?,java,spring,spring-security,Java,Spring,Spring Security,我有一个通过Java配置使用Spring安全性的Java应用程序 在编译中打开/关闭整个Spring安全性的最简单方法是什么 类似于,但对于不使用XML的配置 编辑: 应用@Profile后,我的代码如下所示: @Configuration @Profile("SecurityOn") @EnableWebMvcSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 问题是,如果配置文件“Se

我有一个通过Java配置使用Spring安全性的Java应用程序

在编译中打开/关闭整个Spring安全性的最简单方法是什么

类似于,但对于不使用XML的配置

编辑:

应用@Profile后,我的代码如下所示:

@Configuration
@Profile("SecurityOn")
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
问题是,如果配置文件“SecurityOn”未激活,Spring Security将使用一些默认配置。相反,在这种情况下,如何完全关闭Spring安全性?

请考虑使用此功能

由于
@Profile
可以作为任何
@Bean
方法上的方法级注释应用,因此您可以使用单个概要文件(例如“安全”)并仅注释相关的
@Bean
方法

然后通过JVM系统属性指定活动概要文件,作为环境变量,或在
web.xml


请参阅示例用法。

要禁用该行为,可以添加另一个如下所示的类:

@Configuration
@EnableWebMvcSecurity
@Profile("!SecurityOn")
public class WebSecurityConfigDisable extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
            .antMatchers("/**").permitAll();
    }
}
然后,当您运行应用程序时,您需要登录的唯一时间是
SecurityOn
配置文件处于活动状态时。如果您使用的是Maven和Spring Boot,那么启用登录的命令如下所示

mvn spring-boot:run -Dspring.profiles.active=SecurityOn
在没有配置文件或其他配置文件的情况下运行它将禁用登录。这对当地的发展是有益的


我发现在使用
spring boot starter security
时,这是必要的,因为有默认配置需要登录。

谢谢!到目前为止,我还没有使用@Profiles,所以这是一个有价值的教训,但问题仍然存在。请参见问题中的编辑。