2017-03-05 1 views
1

저는 Spring Initializr을 사용하고 임베디드 Tomcat + Thymeleaf 템플릿 엔진을 사용하며 실행 가능한 JAR 파일로 패키지를 사용하여 스프링 부트 웹 애플리케이션을 생성했습니다. 사용스프링 부트 - 스프링 보안 @ComponentScan 또는 @Import

기술 :

봄 부팅 1.4.2.RELEASE, 봄 4.3.4.RELEASE, Thymeleaf 2.1.5.RELEASE, 톰캣 삽입 8.5.6, 메이븐 3, 자바 (8) 내가 가진

내가 액세서 곳이 config 파일을 사용하여이 보안 클래스는

com.tdk.config 

/** 
* @author nunito 
* @version 1.0 
* @since 4 mar. 2017 
*/ 
@Configuration 
@EnableWebSecurity 
@PropertySource("classpath:/com/tdk/config/app-${APP-KEY}.properties") 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    protected String loginPage = "/tdk/login"; 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 

     http 
      .formLogin() 
       .loginPage(getLoginPage()) 
       .permitAll() 
       .and() 
      .authorizeRequests() 
       .antMatchers("/mockup/**").permitAll() 
       .antMatchers("/welcome/**").authenticated() 
       .and() 
      .logout() 
       .permitAll() 
       .logoutSuccessUrl("/index.html"); 


    } 

    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
     auth 
      .inMemoryAuthentication() 
       .passwordEncoder(new StandardPasswordEncoder()) 
       .withUser("nunito").password("08c461ad70fce6c74e12745931085508ccb2090f2eae3707f6b62089c634ddd2636f380f40109dfb").roles("ADMIN").and() 
       .withUser("nunito").password("4cfbf05e4493d17125c547fdba494033d7aceee9310f253f3e96c4f928333d2436d669d63a84fe4f").roles("ADMIN"); 
    } 

    public String getLoginPage() { 
     return loginPage; 
    } 

    public void setLoginPage(String loginPage) { 
     this.loginPage = loginPage; 
    } 

@SpringBootApplication 
@ComponentScan(basePackages = "com.tdk.config") 
@EnableAutoConfiguration 
public class TdkCloudApplication { 

    public static void main(String[] args) { 
     SpringApplication.run(TdkCloudApplication.class, args); 
    } 
} 

나는 404이 URL을

에서의 그러나이 설정의 모든 것이 OK입니다

@SpringBootApplication 
@EnableAutoConfiguration 
@Import({SecurityConfig.class}) 
public class TdkCloudApplication { 

    public static void main(String[] args) { 
     SpringApplication.run(TdkCloudApplication.class, args); 
    } 
} 

나를 위해 할 수있는 두 가지 방법이 있기 때문에 나는 정확히 같은이가 설명이

+0

'@EnableWebSecurity'를 제거해 봄으로써 스프링 부트 자동 구성을 비활성화 할 수 있습니까? 또한 스프링 부트로 '@ComponentScan'이 필요하지 않으므로 모든 하위 패키지를 검사합니다. '@EnableAutoConfiguration'은 필요하지 않습니다. – Shahbour

답변

1

생각의 차이를 알고 싶습니다 @Import@ComponentScan 인 경우이 질문에 대한 답변이 아닙니다 (이유는 모르겠지만 ComponentScan에서는 작동하지 않습니다). 힌트와 비슷합니다.

@Import은 다른 구성을 가져 오는 데 사용되므로 클래스에 @Configuration으로 주석을 추가하고 거기에 정의 된 일부 빈이있는 경우 응용 프로그램 컨텍스트로 가져옵니다.

@Configuration 
public class config{ 
    @Bean 
    public ClassA a(){ 
     return new ClassA(); 
    } 
} 

@Import({config.Class}) // import Bean for ClassA 

@ComponentScan 각 클래스에 매핑 일대일 빈을 @Component, @Service, @Repository 주석 모든 클래스를 검색하고 있습니다. 예 :

@Component 
public class ClassB {} 

@ComponentScan // import Bean ClassB 

스프링의 버전 4.2 후에 @ComponentScan 또한 성분으로서 @Configuration를 스캔 할 수있다. 따라서 귀하의 경우 SecurityConfig는 구성 요소가 아닌 컨텍스트로 가져와야합니다.

내가 이해하지 못하는 유일한 점은 @Import이 SecurityConfig에서 코드의 실행을 트리거하는 방법입니다. 아무도 모른다면 주석을주십시오.

관련 문제