spring-boot - 无法测试活动的弹簧启动配置文件
问题描述
我有弹簧启动应用程序。我想为 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 时出错:通过字段“userDetailsService”表示的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“org.springframework.security.core.userdetails.UserDetailsService”类型的合格 bean 可用:预计至少有 1 个 bean 有资格作为 autowire 候选者。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
所以现在的问题出在 org.springframework.security.core.userdetails.UserDetailsService - 它不能自动装配。它用于
@Component
public class JwtAuthorizeFilter extends OncePerRequestFilter {
@Autowired
private UserDetailsService userDetailsService;
//some code;
}
解决方案
为了将安全 Bean 添加到您的测试中,请添加SecurityConfig
到您的ContextConfiguration定义
@ContextConfiguration(classes = {SecurityConfig.class})
推荐阅读
- polygon - 在folium中为多边形着色
- javascript - 在 Kotlin JS 中等待 fetch()?
- reactjs - 在带有 typescript 的 redux-thunk 中,调用 getState 永远不会返回
- ios - 使用标准外接显示器时如何修复苹果 Catalina 上的模糊字体
- scala - scala ZIO foreachPar
- java - 使用字符串的 ArrayLists 进行 Java 插入排序
- sql - 选择两列 sql, h2 的组合
- angular - 角嵌套承诺没有完成
- node.js - 随机 npm 模块是否总是创建加密安全字符串?
- python - 如何优化工作(但缓慢)的楼梯置换功能?