首页 > 解决方案 > 无法测试活动的弹簧启动配置文件

问题描述

我有弹簧启动应用程序。我想为 SecurityConfig 创建两个配置文件:dev、prod。第一次尝试是从 WebSecurityConfigurerAdapter 扩展两个类,但我在 SecurityConfig 类中创建了两个 Bean。我的配置如下所示:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig {

    @Bean
    public JwtAuthorizeFilter jwtAuthenticationFilter() throws Exception {
        return new JwtAuthorizeFilter();
    }

    @Bean
    @Profile("prod")
    public WebSecurityConfigurerAdapter prod() {
        return new WebSecurityConfigurerAdapter() {
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                //some code...
            }
        };
    }

    @Bean
    @Profile("dev")
    public WebSecurityConfigurerAdapter dev() {
        return new WebSecurityConfigurerAdapter() {
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                        //some code...
            }
        };
    }

    @Bean
    public WebMvcConfigurer corsConfig() {
        return new WebMvcConfigurerAdapter() {
                       //some code...
        };
    }
}

也是运行应用程序的类:

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        System.setProperty("spring.profiles.active", "dev");
        SpringApplication.run(Application.class, args);
    }
}

当我开始应用程序时,如何测试正确的配置文件是否处于活动状态?当我添加空测试

@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("dev")
@ContextConfiguration(classes = SecurityConfig.class, loader=AnnotationConfigContextLoader.class)
public class SpringProfileConfigurationTest {


    @Autowired
    public WebSecurityConfigurerAdapter securityConfig;

    @Test
    public void testSecurityConfigDevActiveProfile() {

    }

}

它因错误而崩溃:

 .NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework
      .security.config.annotation.web.configuration.WebSecurityConfigurerAdapter'
      available: expected at least 1 bean which qualifies as autowire candidate.
      Dependency annotations:{@org.springframework.beans.factory.annotation
      .Autowired(required=true)}

UPD:将“类”添加到@ContextConfiguration,而不是另一个错误:

BeanCreationException:创建 org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration 中定义的名称为“springSecurityFilterChain”的 bean 时出错:通过工厂方法进行 Bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [javax.servlet.Filter]:工厂方法“springSecurityFilterChain”抛出异常;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“jwtAuthenticationFilter”的 bean 时出错:通过字段“userDetailsS​​ervice”表示的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“org.springframework.security.core.userdetails.UserDetailsS​​ervice”类型的合格 bean 可用:预计至少有 1 个 bean 有资格作为 autowire 候选者。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}

所以现在的问题出在 org.springframework.security.core.userdetails.UserDetailsS​​ervice - 它不能自动装配。它用于

@Component
public class JwtAuthorizeFilter extends OncePerRequestFilter {

    @Autowired
    private UserDetailsService userDetailsService;
    //some code;
}

标签: spring-bootintegration-testingprofiles

解决方案


为了将安全 Bean 添加到您的测试中,请添加SecurityConfig到您的ContextConfiguration定义

@ContextConfiguration(classes = {SecurityConfig.class})

推荐阅读