2016-12-25 25 views
0

私はSpringの上に構築したWebサービスを持っています。Springセキュリティによる認可と認証

@Configuration 
@EnableGlobalMethodSecurity(securedEnabled=true) 
@EnableWebSecurity 

public class ServerSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private Properties properties; 

    private static final String ALL_URI = "/v1/**"; 
    private static final String HEALTH_URI = "/v1/healthCheck"; 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.addFilterBefore(getFilter(), BasicAuthenticationFilter.class); 
     http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); 
     http.authorizeRequests() 
       .antMatchers(HEALTH_URI).permitAll() 
       .anyRequest().authenticated(); 
     http.csrf().disable(); 
    } 

    private AuthenticationFilter getFilter() { 
     return new AuthenticationFilter(properties.getKey()); 
    } 
} 

AuthenticationFilterクラスはAbstractAuthenticationProcessingFilterを拡張し、実際の認証を行い、次のように私は現在、春のセキュリティを使用して認証しています。私のセキュリティに認証を追加したいのであれば、認証フィルターのattemptAuthenticationメソッドでこれらのチェックを行うだけですか?それとももっと良い方法がありますか?私が理解しているのは、認可と認証を別々に行うべきだということです。最初に認証し、アクセス許可を確認します。だから、私は、Spring Security内で許可を行うより良いアプローチが、attemptAuthenticationメソッドに追加するよりもむしろ良いと思うでしょう。

答えて

0

あなたはAuthenticationProviderは、認証AuthenticationProviderを実装し、authenticationsupportsメソッドをオーバーライドして、AuthenticationManagerに注入する必要がありません。フィルタで

attemptAuthentication方法はauthentication(例えばUsernamePasswordFilterが要求からusernamepasswordを取得し、その後、AuthenticationManagerUsernamePasswordAuthenticationTokenを構築します)を取得するには通常、

supports方法は、認証を行うために使用することができるかどうかAuthenticationProviderをテストします。(例えばDaoAuthenticationProviderUsernamePasswordAuthenticationToken

authenticate方法は、(例えばDaoAuthenticationProviderは、ユーザ名によって実際のパスワードを取得し、利用者と比較認証を行うために使用されるサポートしていますこのメソッドは、すでに認証されているAuthentication(たとえばUsernamePasswordAuthenticationToken)を返し、この認証にはユーザー権限(これはhasRole('xxx')に使用できます)を使用するか、詳細などを使用する必要があります。

attemptAuthenticationが成功した後、AuthenticationSecurityContextHolderに設定されます。 hasRole('xx')などを使用できます。

関連する問題