[英]Spring runs my class dedicated for tests only and override '/' dir on server
I'm trying to write integration tests for application based on Spring but I need to provide second application that doesn't require credentials. 我正在尝试为基于Spring的应用程序编写集成测试,但是我需要提供第二个不需要凭据的应用程序。 But when I copied my main class and changed authorizations required. 但是,当我复制主类并更改所需的授权时。 Spring is starting both of them despite I added filter in my main class. 尽管我在主类中添加了过滤器,但Spring正在启动它们两者。
Bootstrap - main class 引导程序-主类
@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");
}
}
}
UnsecuredBootstrap - used for tests UnsecuredBootstrap-用于测试
@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");
}
}
}
In console when I start application I get 在控制台中,当我启动应用程序时,我得到了
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 /
You forgot to exlude nested class ApplicationSecurity from Bootstrap . 您忘记了从Bootstrap中排除嵌套类ApplicationSecurity 。 Simply replace this line (in UnsecuredBootstrap.java ) 只需替换此行(在UnsecuredBootstrap.java中 )
@ComponentScan(excludeFilters = { @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = Bootstrap.class) })
with this line: 用这一行:
@ComponentScan(excludeFilters = { @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {Bootstrap.class, Bootstrap.ApplicationSecurity.class}) })
Did it help? 有帮助吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.