java - Spring 运行我的类,仅用于测试并覆盖服务器上的 '/' 目录

标签 java spring spring-mvc

我正在尝试为基于 Spring 的应用程序编写集成测试,但我需要提供第二个不需要凭据的应用程序。但是当我复制我的主类并更改所需的授权时。尽管我在主类中添加了过滤器,但 Spring 仍在启动它们。

Bootstrap - 主类

@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 - 用于测试

@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}) })

有帮助吗?

关于java - Spring 运行我的类,仅用于测试并覆盖服务器上的 '/' 目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27102751/

相关文章:

java - 无法从终端编译访问 jar 文件

java - 为什么我的 Spring Security 不工作?

java - 在代码中使用 set 或 List

angularjs - 如何通过单击按钮从表中删除整行?

jquery - Liferay 6.1 中未使用 SpringMVC portlet 调用 Ajax 方法

java - 如何使用 zipfileset 正确创建 ZIP 文件?

Java没有从文件中读取所有对象

Java:使用新的模式字母扩展 SimpleDateFormat

java - 使用 JpaRepository 从数据库更新和删除

spring - 使用 Angular2 将文件上传到 REST API