2017-03-05 6 views
1

私はSpring Initializrを使用して、埋め込まれたTomcat + Thymeleafテンプレートエンジンを使用してSpringブートWebアプリケーションを生成し、実行可能なJARファイルとしてパッケージ化しました。使用スプリングブート - 春のセキュリティ@ComponentScanまたは@Import

技術:私が持っている

春ブーツ1.4.2.RELEASE、春4.3.4.RELEASE、Thymeleaf 2.1.5.RELEASE、Tomcatの埋め込み8.5.6、Mavenの3、Javaの8

私はアクセスもどここの設定ファイルを使用し、このセキュリティクラスは

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内の

しかし、この設定すべてが私にとってまったく同じで

+0

'@EnableWebSecurity'を削除すると、スプリングブートの自動設定が無効になります。スプリングブートとして '@ComponentScan'は必要ありません。すべてのサブパッケージをスキャンします.'EnableAutoConfiguration 'は不要です – Shahbour

答えて

1

これは説明だと思うそれには2つの異なった方法であるので、私は、違いを知っていただきたいと思い

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

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

OKです@Import@ComponentScanの場合、これはあなたの質問に対する回答ではありません。なぜなら、なぜそれがComponentScanで動作しないのか分からないからです。

@Importは、他の設定をインポートするために使用されます。したがって、クラスに@Configurationという注釈が付けられており、いくつかのBeanがそこに定義されている場合、アプリケーションコンテキストにインポートされます。

@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でコードの実行をトリガーする方法だけです。誰かがそれを知っている場合は、コメントを与えてください。

関連する問題