2016-07-15 7 views
4

私は春の起動Webアプリケーションをいくつかの休憩のエンドポイントを公開している。私は選択された残りのエンドポイントに対してのみ基本認証を有効にする方法を知りたがっています。たとえば、/employee/{id}要求のみが認証され、他のすべての残りのエンドポイントは無視されるとします。私は次のコードを使用しています。私の質問は、antMatcherは指定された要求を認証するだけですか?現在、すべての残りのエンドポイントの認証を有効にしています。選択された残りのエンドポイントのみを認証する:春の起動

@Configuration 
@EnableWebSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 
    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     // How does it work will it only authenticate employee & 
     // ignore any other request?? Its authenticating all the requests currently. 
     http 
      .authorizeRequests() 
       .antMatchers("/employee/*").authenticated() 
      .and() 
      .httpBasic() 
      .and() 
      .csrf() 
       .disable();  
    } 

    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
     auth 
      .inMemoryAuthentication() 
       .withUser("admin").password("admin").roles("USER"); 
    } 
} 
+0

あなたの構成が私には罰金です。この設定が適用されていることを確認しますか?あなたはコンソール上のデフォルトの 'user'のためのランダムなパスワードを見ていますか?プロジェクトの構成を投稿してください。 –

答えて

4

デフォルトでは、Spring Securityはクラスパス上にすべてのエンドポイントを保護します。

他のすべてのエンドポイントが認証なしで許可されるようにするには、除外を明示的に追加する必要があります。

例:

@Configuration 
@EnableWebSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .authorizeRequests() 
       .antMatchers("/employee/*").authenticated() 
       .anyRequest().permitAll() 
      .and() 
      .httpBasic() 
      .and() 
      .csrf().disable(); 
    } 

    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
     auth 
      .inMemoryAuthentication() 
       .withUser("admin").password("admin").roles("USER"); 
    } 

} 
関連する問題