问题描述
我有启用了基本身份验证的 Spring Boot
应用程序.从数据库消耗的 UserServiceDetails
.为了进行单元测试,我想对其进行模拟,以便将数据从其他地方使用.
I have Spring Boot
application with basic auth enabled. UserServiceDetails
consumed from DB. For the unit-testing purpose, I want to mock it, so the data will be consumed from elsewhere.
我该怎么办?
我的问题不是如何模拟 UserServiceDetails
本身,而是如何模拟使用它来通过基本身份验证测试Controller的方式.
My question is not how to mock UserServiceDetails
itself but how to mock the way that I can use it to test Controllers with basic auth.
以下是我的 SpringSecurity
配置:
@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private static final String[] AUTH_WHITELIST = {
// -- swagger ui
"/",
"/csrf",
"/swagger-resources",
"/swagger-resources/**",
"/configuration/ui",
"/configuration/security",
"/swagger-ui.html",
"/webjars/**"
};
@Autowired
@Qualifier("customUserDetailsService")
private UserDetailsService userDetailsService;
private final static Integer bCryptEncryptionLevel = 8;
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder(bCryptEncryptionLevel);
}
public SecurityConfig() {
super();
}
@Autowired
public void configureGlobalSecurity(AuthenticationManagerBuilder authManagerBuilder) throws Exception {
authManagerBuilder.authenticationProvider(authenticationProvider());
authManagerBuilder.userDetailsService(userDetailsService)
.passwordEncoder(bCryptPasswordEncoder());
}
@Bean
public DaoAuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
authenticationProvider.setUserDetailsService(userDetailsService);
authenticationProvider.setPasswordEncoder(bCryptPasswordEncoder());
return authenticationProvider;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.antMatchers(AUTH_WHITELIST).permitAll()
// allow default swagger docket
.regexMatchers("\\A/v2/api-docs\\Z").permitAll()
// require auth for any other swagger docket
.regexMatchers("\\A/v2/api-docs?.*\\Z").authenticated()
.antMatchers("/**").authenticated()
.and()
.httpBasic()
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
}
总结一下,如何将 UserServiceDetails
模拟到 SpringSecurity
配置中,以便可以对 Spring Boot
应用程序中的控制器进行单元测试?
To summarize, how can I mock UserServiceDetails
into SpringSecurity
config, so I can unit-test controllers into my Spring Boot
app?
推荐答案
我认为您应该使用 @Profile, UserDetailesService
是一个接口,用于创建 UserDetailesService
的两个实现,一个用于测试,另一个用于其余的情况
I think you should use @Profile, UserDetailesService
is an interface creates two implementation of UserDetailesService
, one for test and another for the rest of the case
@Component
@Profile("test")
public class UserDetailesServiceTestImpl implements UserDetailesService{
}
@Component
@Profile("!test")
public class UserDetailesServiceImpl implements UserDetailesService{
}
这篇关于如何在SpringSecurity Bean中模拟自定义UserServiceDetails进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!