Spring 使用 Spring 安全启动测试。如何启动另一种安全配置?

Spring Boot testing with Spring Security. How does one launch an alternative security config?

我的 spring 启动应用程序有一个应用程序 class。当我 运行 它(作为一个应用程序)时,它会在一个嵌入式 servlet 容器中自行启动(Tomcat,在我的例子中)。不知何故(我想是通过应用程序的@annotations),加载了同一个包中的WebSecurityConfig(扩展WebSecurityConfigurerAdapter)。

WebSecurityConfig 包含两个重要的配置信息块:

@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
@EnableGlobalMethodSecurity(prePostEnabled = true) // enables method-level role-checking
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
      auth
         .ldapAuthentication()
         .userSearchBase("CN=Users,DC=some,DC=domain,DC=com")   
         .userSearchFilter("(sAMAccountName={0})")
         .groupSearchBase("OU=Groups,DC=some,DC=domain,DC=com")                 
         .groupSearchFilter("(member={0})")
         .contextSource()
            .managerDn("cn=ad-bind,cn=users,dc=some,dc=domain,dc=com")
            .managerPassword("APASSWORD!") 
            .url("ldaps://some.domain.com:636");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
         System.out.println("***************** WebSecurityConfig.configure *************************");
         http.csrf().disable();

         http
            .headers()
                .frameOptions()
                    .disable();

         http
                .authorizeRequests()
                    .antMatchers("/resources/images/*", "/me", "/products", "/product/**", "/offerings", "/offering/**", "/client/**")
                            .permitAll()
                    .anyRequest().authenticated()
                    .and()
                    .formLogin()
                    .loginPage("/login").defaultSuccessUrl("/me")
                    .permitAll()
                    .and()
                    .logout()
                    .permitAll();

         http.logout().logoutSuccessUrl("/me");
    }
}

configureGlobal() 包含我们内部 LDAP 系统的配置,它工作得很好。

configure() 指定哪些 URL 是 public,哪些仅向登录用户显示,哪些相对 URL 在用户登录时发送给他们。

现在我正在进行集成测试并编写了一些方法来测试不需要身份验证的控制器。这些测试按预期工作。应用程序 class 启动并针对它执行测试。

但现在我想测试确实需要身份验证的控制器方法。我认为实现这一点的方法是告诉测试 class 启动替代应用程序 class(在我的例子中是 TestApplication)和创建虚拟用户的 WebSecurityConfig:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestApplication.class) // fires up with TestApplication.class instead of Application.class
@WebAppConfiguration
public class ProductControllerTests {
    // test methods here, this time with username/password included
}

@Configuration
@EnableAutoConfiguration
public class TestApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(applicationClass, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(applicationClass);
    }

    private static Class<TestApplication> applicationClass = TestApplication.class;

}

@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
      auth.inMemoryAuthentication().withUser("testuser").password("userpass").roles("USER");
      auth.inMemoryAuthentication().withUser("testadmin").password("adminpass").roles("ADMIN");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
         System.out.println("***************** WebSecurityConfig.configure *************************");
         http.csrf().disable();

         http
            .headers()
                .frameOptions()
                    .disable();

         http
                .authorizeRequests()
                    .antMatchers("/resources/images/*", "/me", "/products", "/product/**", "/offerings", "/offering/**", "/client/**")
                            .permitAll()
                    .anyRequest().authenticated()
                    .and()
                    .formLogin()
                    .loginPage("/login").defaultSuccessUrl("/me")
                    .permitAll()
                    .and()
                    .logout()
                    .permitAll();

         http.logout().logoutSuccessUrl("/me");
    }
}

所以我的问题是:当我执行单元测试时 class,我相信 TestApplication 正在触发。但是,它没有选择替代 WebSecurityConfig class 及其 auth.inMemoryAuthentication() 测试用户。 如何强制我的应用程序在 运行 正常运行应用程序时使用一个 WebSecurityConfig,而在 运行 运行单元测试时使用不同的 WebSecurityConfig?

您可以配置 TestApplication 以仅包含您想要测试的 bean。换句话说,确保您的 WebSecurityConfig 不是测试配置的一部分。如果你阅读 javadoc of @SpringBootApplication you will notice that it is a composite annotation that consists of (among others) the @ComponentScan annotation. Consequently your Application and your TestApplication will perform a recursive scan from the package in which the class is located. The Spring reference docs has a specific chapter about Using filters to customize scanning.

或者,如果您使用的是 Spring 安全版本 4 或更高版本,您可能会发现 @WithMockUser and @WithUserDetails 的新增内容很有趣。

在您的安全配置 class 中,添加 @Profile 注释以在单元测试配置文件中禁用。喜欢:

@Configuration
@Profile("!" + Constants.SPRING_PROFILE_UNITTEST)
public class WebSecurityConfig { ....}

并在测试目录中让您的另一个测试安全配置。