我正在尝试为基于Spring的应用程序编写集成测试,但我需要提供第二个不需要凭据的应用程序。但是当我复制我的主类并更改所需的授权时。春天开始了他们两个,尽管我在主课中添加了过滤器。
引导程序 - 主类
@ComponentScan(excludeFilters = { @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = UnsecuredBootstarp.class) })
@EnableAutoConfiguration(exclude = { UnsecuredBootstarp.class })
@Controller
public class Bootstrap extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(new Class[] { Bootstrap.class }, args);
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/login").setViewName("login");
registry.addViewController("/index").setViewName("index");
}
@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
protected static class ApplicationSecurity extends WebSecurityConfigurerAdapter {
@Autowired
private SecurityProperties security;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().hasRole("USER").and().formLogin()
.loginPage("/login").failureUrl("/login?error").permitAll()
.defaultSuccessUrl("/index.html").permitAll().and().logout()
.logoutUrl("/logout").logoutSuccessUrl("/login").deleteCookies("JSESSIONID")
.permitAll().and().csrf().disable();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(new AuthProvider());
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/**/*.js", "/**/*.json", "/**/*.css", "/**/*.png",
"/**/*.properties", "/**/*.ttf");
}
}
}
不安全引导 - 用于测试
@Controller
@ComponentScan(excludeFilters = { @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = Bootstrap.class) })
@EnableAutoConfiguration
//@EnableAutoConfiguration(exclude = { SecurityAutoConfiguration.class, SpringBootWebSecurityConfiguration.class })
public class UnsecuredBootstrap extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(new Class[] { UnsecuredBootstrap.class }, args);
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/login").setViewName("login");
registry.addViewController("/index").setViewName("index");
}
@Configuration
protected static class ApplicationSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest()
.permitAll().and().csrf().disable();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(new AuthProvider());
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/**/*.js", "/**/*.json", "/**/*.css", "/**/*.png",
"/**/*.properties", "/**/*.ttf");
}
}
}
当我启动应用程序时,在控制台中我得到
2014-11-24 11:22:02.440 INFO 5612 --- [ main] org.apache.cxf.endpoint.ServerImpl : Setting the server's publish address to be /
2014-11-24 11:22:02.575 INFO 5612 --- [ main] org.apache.cxf.endpoint.ServerImpl : Setting the server's publish address to be /
你忘了从 Bootstrap 中排除嵌套的类 ApplicationSecurity。只需替换此行(在 UnsecuredBootstrap.java 中)
@ComponentScan(excludeFilters = { @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = Bootstrap.class) })
用这一行:
@ComponentScan(excludeFilters = { @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {Bootstrap.class, Bootstrap.ApplicationSecurity.class}) })
它有帮助吗?