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 如何通过配置文件激活/取消激活Spring Security_Java_Spring_Spring Security - Fatal编程技术网

Java 如何通过配置文件激活/取消激活Spring Security

Java 如何通过配置文件激活/取消激活Spring Security,java,spring,spring-security,Java,Spring,Spring Security,我已经在我的项目上设置了两个SpringWebSecurityConfigureAdapter实例 一个允许任何内容进入,另一个激活UserDigest 没有控件的配置文件 @Configuration @EnableWebSecurity @Profile("secoff") public class WebSecurityConfigSecOff extends WebSecurityConfigurerAdapter { @Override protected void c

我已经在我的项目上设置了两个SpringWebSecurityConfigureAdapter实例

一个允许任何内容进入,另一个激活UserDigest

没有控件的配置文件

@Configuration
@EnableWebSecurity
@Profile("secoff")
public class WebSecurityConfigSecOff extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.anonymous().and().csrf().disable();
    }
}
带有UserDigest的配置

@Configuration
@EnableWebSecurity
@Profile("secon")
public class WebSecurityConfigSecOn extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.addFilter(digestAuthenticationFilter())
            .exceptionHandling().authenticationEntryPoint(digestEntryPoint())
            .and()
            .httpBasic()
            .and()
            .authorizeRequests()
            .antMatchers( "/services/ESI/*").hasAnyRole("USER")
            .anyRequest().authenticated()
            .and()
            .csrf().disable();

}
这两个类也需要定义它

import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;

public class SpringSecurityInitializer extends AbstractSecurityWebApplicationInitializer {
}
我本以为我可以用…来运行这个系统

-Dspring.profiles.active=secon
并且安全性将被启用

但事实并非如此,它无法启动并给出错误

NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' available
它也不会触及两个“configure(HttpSecurity-http)”方法中的任何一个断点

两个配置文件似乎都未触发。 如果我删除@profile注释,那么在启用或禁用安全性的情况下就可以开始了

有人能提出解决办法吗


注意:JDK 1.8,Spring 4.3.7-RELEASE

此失败的原因是tomcat setEnv.bat具有此设置

spring.profiles.active=serverMode
这个值覆盖了我在Intellij中设置的a-D参数。 因此,我的个人资料不活跃

解决这一问题的另一种方法是从配置文件移动到@value注释

@Value("${securityDisabled:false}")
private boolean securityDisabled;

@Override
protected void configure(HttpSecurity http) throws Exception {
    if (securityDisabled)
    {
        http.anonymous().and().csrf().disable();
    }else {
        http.addFilter(digestAuthenticationFilter())
                .exceptionHandling().authenticationEntryPoint(digestEntryPoint())
                .and()
                .httpBasic()
                .and()
                .authorizeRequests()
                .antMatchers( "/services/ESI/*").hasAnyRole("USER")
                .anyRequest().authenticated()
                .and()
                .csrf().disable();
    }
}

此失败的原因是tomcat setEnv.bat具有以下设置

spring.profiles.active=serverMode
这个值覆盖了我在Intellij中设置的a-D参数。 因此,我的个人资料不活跃

解决这一问题的另一种方法是从配置文件移动到@value注释

@Value("${securityDisabled:false}")
private boolean securityDisabled;

@Override
protected void configure(HttpSecurity http) throws Exception {
    if (securityDisabled)
    {
        http.anonymous().and().csrf().disable();
    }else {
        http.addFilter(digestAuthenticationFilter())
                .exceptionHandling().authenticationEntryPoint(digestEntryPoint())
                .and()
                .httpBasic()
                .and()
                .authorizeRequests()
                .antMatchers( "/services/ESI/*").hasAnyRole("USER")
                .anyRequest().authenticated()
                .and()
                .csrf().disable();
    }
}

您能发布完整的堆栈跟踪和任何可能相关的附加配置(bean创建等)吗?我在一个测试项目中尝试了一个类似的配置,它对我来说就像预期的一样(使用spring boot)你能发布完整的堆栈跟踪和任何可能相关的附加配置(bean创建等)吗?我在一个测试项目中尝试了一个类似的配置,它按预期对我有效(使用spring boot)