2016-09-11 3 views
0

私は、認証にスプリングセキュリティを使用する既存のWebアプリケーションを持っています。また、XSS攻撃を防止するためにセッション管理を使用して、ユーザーが事前定義された時間、XSRFトークンにログインできるようにします。パスによるSpringセキュリティセッション/ xsrf設定

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    // @formatter:off 
    http 
    .exceptionHandling() 
     .authenticationEntryPoint(restEntryPoint()) 
    .and() 
    .headers().addHeaderWriter(new StaticHeadersWriter("Server","")) 
    .and() 
    .httpBasic() 
     .authenticationEntryPoint(restEntryPoint()) 
    .and() 
    .logout().addLogoutHandler(myLogoutHandler()) 
    .logoutSuccessHandler(logoutSuccessHandler()) 
    .and() 
    .authorizeRequests() 
     .antMatchers("/index.html", "/login", "/").permitAll() 
     .antMatchers(HttpMethod.OPTIONS).denyAll() 
     .antMatchers(HttpMethod.HEAD).denyAll() 
     .anyRequest().authenticated() 
    .and() 
    .authenticationProvider(myAuthenticationProvider) 
     .csrf() 
      .csrfTokenRepository(csrfTokenRepository()) 
    .and() 
    .addFilterAfter(csrfHeaderFilter(), SessionManagementFilter.class); 
    // @formatter:on 
} 

これは、Webアプリケーションに最適です。しかし、今や、サードパーティのクライアントアプリケーションが純粋なREST呼び出しを介して私のサービスを呼び出せるような設定を追加するように要求されました。つまり、完全にステートレスで、http基本認証を使用する必要があります。セッションを作成せずにxsrfを無効にする必要があります思う...)。

これらのクライアントAPI呼び出しの共有URLパスを定義できます。しかし、既存のセキュリティ構成とサーバーを両方の要件をサポートするためにどのように活用できますか?自分の質問に答える

答えて

0

...

春のセキュリティを使用すると、順序に基づいて複数の設定を使用できます。ドキュメントには、以下の例を与える:他のパスは、既定のFormLoginWebSecurityConfigurerAdapterで構成されながら、上記の例では

@EnableWebSecurity 
public class MultiHttpSecurityConfig { 
    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) { 1 
     auth 
      .inMemoryAuthentication() 
       .withUser("user").password("password").roles("USER").and() 
       .withUser("admin").password("password").roles("USER", "ADMIN"); 
    } 

    @Configuration 
    @Order(1)              2 
    public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter { 
     protected void configure(HttpSecurity http) throws Exception { 
      http 
       .antMatcher("/api/**")        3 
       .authorizeRequests() 
        .anyRequest().hasRole("ADMIN") 
        .and() 
       .httpBasic(); 
     } 
    } 

    @Configuration             4 
    public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter { 

     @Override 
     protected void configure(HttpSecurity http) throws Exception { 
      http 
       .authorizeRequests() 
        .anyRequest().authenticated() 
        .and() 
       .formLogin(); 
     } 
    } 
} 

、/ APIは、唯一の管理者ロールに許可されるであろう。

続きを見るthis URL

関連する問題