当前位置: 首页 > 知识库问答 >
问题:

无法测试活动的Spring Boot配置文件

曾景龙
2023-03-14

我有Spring启动应用程序。我想为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)}

BeanCreationException:在org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration中html" target="_blank">定义的创建名为'Spring SecurityFilterChain'的bean时出错:通过工厂方法进行的bean实例化失败;嵌套异常是org.springframework.beans.beanInstantiationException:未能实例化[javax.servlet.filter]:工厂方法'Spring SecurityFilterChain'引发异常;嵌套异常是org.springframework.beans.factory.unsatisfiedDependencyException:创建名为“JWT AuthenticationFilter”的bean时出错:通过字段“User DetailsService”表示未满足的依赖关系;嵌套异常是org.springframework.beans.factory.nosuchbeanDefinitionException:没有类型为'org.springframework.security.core.userdetails.userdetailsService'的合格bean可用:至少需要1个符合autowire候选的bean。依赖注释:{@org.springframework.beans.factory.annotation.autowire(required=true)}

所以现在的问题出在org.springframework.security.core.userdetails.userdetailsService上--它是不可能的。它用于

@Component
public class JwtAuthorizeFilter extends OncePerRequestFilter {

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

共有1个答案

澹台星剑
2023-03-14

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

@ContextConfiguration(classes = {SecurityConfig.class})
 类似资料:
  • 问题内容: 我正在尝试使用Maven 3 在Spring Boot应用程序中设置活动配置文件。在我的pom.xml中,将默认的活动配置文件和属性spring.profiles.active设置 为development: 但是每次我运行应用程序时,都会在日志中收到以下消息: 并且将SpringBoot配置文件设置为默认值(读取application.properties而不是application

  • 问题内容: 尝试在android上运行仪器测试时出现错误。我已经编写了一个名为 AudioPlayerActivity 的活动,该活动位于 com.mycompany.mobile.android.gui 包中,现在我正在尝试测试该项目的GUI,并且在以下错误中运行: java.lang.RuntimeException:无法解析以下活动:Intent {act = android.intent.

  • 我无法激活IntelliJ idea中的Spring下载配置文件。 爪哇17 IntelliJ IDEA 2021.3测试版(终极版) 这是文件pom.xml,我正在尝试运行的微服务。 文件application.yaml

  • 我有几个用于Eclipse的文件(我使用的是Eclipse Oxyox),下面是一个示例 请注意,我正在使用变量,以便使其可用于任何项目。 我试图将我为项目启用的概要文件“注入”到通用的启动概要文件配置中,类似于变量,但与当前的Maven概要文件相关。 如果不能这样做,我需要为每个环境创建一个启动配置文件,这意味着每个目标要启动3个文件。 是否有一种方法可以使用/创建一个Eclipse变量,该变量

  • 本文向大家介绍Springboot Cucumber测试配置介绍详解,包括了Springboot Cucumber测试配置介绍详解的使用技巧和注意事项,需要的朋友参考一下 目前Spring-boot成为了java开发的主流框架,Cucumber作为一款支持dsl的自动化测试工具,很适合用户编写DSL优化过的单元测试等测试用例。本文将讲解如何在SpringBoot中配置Cucumber进行自动化测试

  • 41.3.5 自动配置的测试 Spring Boot的自动配置系统适用于应用程序,但对测试来说有时可能有点过了,最好是仅加载程序要测试的“切片”所需的部分配置。例如,您可能想测试Spring MVC的控制器是否正确地映射了URL,而且您不想在这些测试中涉及数据库调用;或者您可能想要测试JPA实体,而当这些测试运行时,您对Web层并不感兴趣。 spring-boot-test-autoconfigu