2016-09-15 4 views
0

Spring Saml2に関連するSpring Securityの設定に問題があり、が正常に応答した場合でもの認証が常にnullになる。Spring Saml FilterChainProxyコンテキストをクリアする - ヌル認証

私はspring4.3.0.RELEASEspring-security4.1.0.RELEASEと一緒にspring-security-saml2-coreライブラリ1.0.2.RELEASEを使用しています。

私はSSOCircleを介してSSOを通じて認証する簡単なSPを持っています。認証の動作とデバッグSAMLAuthenticationProviderは、私が期待した権限を持つ認証済みのユーザーを返すことがわかります。ここまでは順調ですね。

認証が完了すると、私はそれが私がその後ヌル認証を持っている理由である疑いがある次の行

SecurityContextHolder.clearContext(); 

を呼び出すorg.springframework.security.web.FilterChainProxy:180に足を踏み入れたが。続き

は春のセキュリティの設定です:

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

... 

@Bean 
    public SAMLAuthenticationProvider samlAuthenticationProvider() { 
     SAMLAuthenticationProvider samlAuthenticationProvider = new SAMLAuthenticationProvider(); 
     samlAuthenticationProvider.setUserDetails(samlUserMappingService); 
     samlAuthenticationProvider.setForcePrincipalAsString(false); 
     return samlAuthenticationProvider; 
    } 

@Bean 
    public FilterChainProxy samlFilter() throws Exception { 
     List<SecurityFilterChain> chains = new ArrayList<SecurityFilterChain>(); 

     chains.add(new DefaultSecurityFilterChain(new AntPathRequestMatcher("/saml/login/**"), samlEntryPoint())); 
     chains.add(new DefaultSecurityFilterChain(new AntPathRequestMatcher("/saml/logout/**"), samlLogoutFilter())); 
     chains.add(new DefaultSecurityFilterChain(new AntPathRequestMatcher("/saml/metadata/**"), 
       metadataDisplayFilter())); 
     chains.add(new DefaultSecurityFilterChain(new AntPathRequestMatcher("/saml/SSO/**"), 
       samlWebSSOProcessingFilter())); 
     chains.add(new DefaultSecurityFilterChain(new AntPathRequestMatcher("/saml/SSOHoK/**"), 
       samlWebSSOHoKProcessingFilter())); 
     chains.add(new DefaultSecurityFilterChain(new AntPathRequestMatcher("/saml/SingleLogout/**"), 
       samlLogoutProcessingFilter())); 
     chains.add(new DefaultSecurityFilterChain(new AntPathRequestMatcher("/saml/discovery/**"), samlIDPDiscovery())); 
     return new FilterChainProxy(chains); 
    } 



@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http.httpBasic().authenticationEntryPoint(samlEntryPoint()); 
    http.csrf().disable(); 
    //http.addFilterBefore(metadataGeneratorFilter(), ChannelProcessingFilter.class) 
    http.addFilterAfter(samlFilter(), BasicAuthenticationFilter.class); 
    http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/error").permitAll().antMatchers("/saml/**") 
      .permitAll().anyRequest().authenticated(); 
    http.logout().logoutSuccessUrl("/"); 
} 


@Override 
protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
    auth.authenticationProvider(samlAuthenticationProvider()).eraseCredentials(false); 

... 
    } 

そしてここでは、Web初期化子です:

public class WebInitialiser extends AbstractAnnotationConfigDispatcherServletInitializer { 

    @Override 
    protected Class<?>[] getServletConfigClasses() { 
     return new Class[] {}; 
    } 

    @Override 
    protected String[] getServletMappings() { 
     return new String[] { "/" }; 
    } 

    @Override 
    protected Class<?>[] getRootConfigClasses() { 
     return new Class[] { WebMvcConfig.class}; 
    } 

    @Override 
    public void onStartup(ServletContext servletContext) throws ServletException { 
     servletContext.addFilter("samlFilter", new DelegatingFilterProxy("samlFilter")) 
       .addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*"); 

     super.onStartup(servletContext); 
    } 

} 

P.S.私は主にgithubの次のリポジトリのコードを参照しました:https://github.com/vdenotaris/spring-boot-security-saml-sample/tree/master/src/main/java/com/vdenotaris/spring/boot/security/saml/webしかし、私はSpringブートを使用していません。

このアプリケーションは、apache-tomcat-8.0.30にデプロイされており、apache-tomcat-7.0.37も試しました。

答えて

2

私は答えを見つけました。コンテキストを初期化するとき、フィルタは正しく登録されていないので、上記のwierdの動作が行われます。

溶液を

public class SecurityInitialiser extends SecurityWebApplicationInitializer(){} 

を次のようにクラスを作成しWebInitialiserクラスからonStartupメソッドを除去することでした。

これは、Springブートで動作するのは、フィルタが自動的にスキャンされて登録されるためです。

関連する問題