Java Spring运行专门用于测试的类,并覆盖'/';服务器上的目录
我正在尝试为基于Spring的应用程序编写集成测试,但我需要提供第二个不需要凭据的应用程序。但当我复制我的主类并更改所需的授权时。尽管我在主类中添加了过滤器,Spring还是启动了这两个类 引导-主类Java Spring运行专门用于测试的类,并覆盖'/';服务器上的目录,java,spring,spring-mvc,Java,Spring,Spring Mvc,我正在尝试为基于Spring的应用程序编写集成测试,但我需要提供第二个不需要凭据的应用程序。但当我复制我的主类并更改所需的授权时。尽管我在主类中添加了过滤器,Spring还是启动了这两个类 引导-主类 @ComponentScan(excludeFilters = { @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = UnsecuredBootstarp.class) }) @EnableAutoConfigurat
@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 /
您忘记从引导中排除嵌套类应用程序安全性。只需替换这一行(在UnsecuredBootstrap.java中) 这一行:
@ComponentScan(excludeFilters = { @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {Bootstrap.class, Bootstrap.ApplicationSecurity.class}) })
它有用吗?此外,如果您希望UnsecuredBootstrap真正不安全,那么它不应该扩展WebMVCConfigureAdapter,而应该包含嵌套类ApplicationSecurity。IMHO main方法是这个类中唯一需要的方法。
@ComponentScan(excludeFilters = { @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {Bootstrap.class, Bootstrap.ApplicationSecurity.class}) })