简体   繁体   中英

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. 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.

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

@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 . Simply replace this line (in 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM