簡體   English   中英

Spring運行我的僅用於測試的類,並在服務器上覆蓋'/'目錄

[英]Spring runs my class dedicated for tests only and override '/' dir on server

我正在嘗試為基於Spring的應用程序編寫集成測試,但是我需要提供第二個不需要憑據的應用程序。 但是,當我復制主類並更改所需的授權時。 盡管我在主類中添加了過濾器,但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");
        }

    }
}

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

有幫助嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM