2017-08-24 67 views
2

tldr;註冊一個自定義的AuthenticationSuccessHandler或配置SAMLRelayStateSuccessHandler以在成功驗證後設置重定向URL Spring Security的SAML擴展的適當方式是什麼?如何在Spring Security的SAML擴展中註冊AuthenticationSuccessHandler或SAMLRelayStateSuccessHandler?

我是Spring和Spring Security的新手,但是我已經獲得了一些成功,讓SAML擴展可以用於單點登錄應用程序。但是,我遇到了一個問題,Okta(IDP)和我的應用程序將在成功驗證後不斷循環,而不是轉到原始URL。我的懷疑是,這是由於我使用Vaadin以及Spring並且一旦Vaadin servlet加載,它完全是做了一些有趣的事情,或者完全忽略了Spring過濾器。在建議的解決方案是註冊AuthenticationSuccessHandler的博客文章combining filter-based Spring Security with Vaadin中提出了一個可能的問題和解決方案。

然而,Spring Security SAML擴展與簡單的表單登錄或OpenID相比並不那麼簡單。例如,使用表單登錄註冊成功處理程序非常簡單

http.formLogin().successHandler(handler); 

沒有這樣的選項可用於SAML擴展。然而,在另一篇文章VladimírSchäfer中(參見「Spring saml-如何在SP上啓動登錄時記住請求參數,並在IdP響應後處理它們)建議將AuthenticationSuccessHandler更改爲SAMLRelayStateSuccessHandler。文檔表明這將是完美的方式它可是,我寧願不繼承SAMLEntryPoint

我現在SecurityConfiguration是完全基於Java的,並遵循使用Spring和1563一起馬特Raible的文章SecurityConfiguration如下:。

@Override 
protected void configure(final HttpSecurity http) throws Exception { 

    http 
     .csrf().disable() // Disable Spring's CSRF protection and use Vaadin's. 
     .authorizeRequests() // Everything else: Do SAML SSO 
      // Allow everyone to attempt to login 
      .antMatchers("/root/saml*").permitAll() 
      // But make sure all other requests are authenticated 
      .anyRequest().authenticated() 
      .and() 
      // Create a completely new session 
      .sessionManagement().sessionFixation().newSession() 
      .and() 
      // Configure the saml properties 
     .apply(saml()) 
      .serviceProvider() 
       .keyStore() 
        .storeFilePath("saml/keystore.jks") 
        .password(this.password) 
        .keyname(this.keyAlias) 
        .keyPassword(this.password) 
        .and() 
       .protocol("https") 
       // Do I need to adjust this localhost piece? 
       .hostname(String.format("%s:%s", "localhost", this.port)) 
       .basePath("/") 
       .and()  
       // Load the metadata from the stored properties 
      .identityProvider().metadataFilePath(this.metadataUrl); 
} 

` `

註冊AuthenticationSuccessHandler或配置SAMLRelayStateSuccessHandler的最佳方法是什麼?任何其他想法也將不勝感激!

回答

0

註冊自定義處理程序的解決方案是使用Sample程序中提供的示例securityContext.xml文件來配置Spring SAML擴展。仔細閱讀Spring SAML Extension guide即可獲取詳細信息。如果您使用Spring Boot,則需要通過添加@ImportResource註釋來告訴它導入securityContext.xml文件。就我而言,我刪除了所有的代碼從我的安全配置類,但離開了註釋和補充@ImportResource如下:

/** 
* This class is the main security configuration for working with the 
* Single-Sign On SAML backend. All it does it import the security context file 
* and configure other annotation-based options. All of the real configuration 
* options are in WEB-INF/securityContext.xml. 
* 
* @author Jay Jay Billings 
* 
*/ 
@EnableWebSecurity 
@Configuration 
@EnableGlobalMethodSecurity(securedEnabled = true) 
@ImportResource("WEB-INF/securityContext.xml") 
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {} 

一旦securityContext.xml文件已經到位,重新配置你的應用程序,你可以在successRedirectHandler豆添加自定義處理程序:

<!-- Handler deciding where to redirect user after successful login --> 
<bean id="successRedirectHandler" 
    class="com.example.app.customSuccessHandler"> 
</bean> 

這是回答我的問題,但它並沒有解決自己我重定向循環的問題。重定向循環的解決方案還需要1)修復MetadataGenerator bean中的實體基URL,並且2)修復security:intercept-url模式值以說明我的修改過的(非根)基URL。有關後者的信息,請參閱本網站上的「該網頁在spring-security應用程序中具有重定向循環」。

1

我使用:

@Bean 
public SavedRequestAwareAuthenticationSuccessHandler successRedirectHandler() { 
    SavedRequestAwareAuthenticationSuccessHandler successRedirectHandler = 
     new SavedRequestAwareAuthenticationSuccessHandler() { 
      @Override 
      public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException { 
       super.onAuthenticationSuccess(request, response, authentication); 
      }   
     }; 
    successRedirectHandler.setDefaultTargetUrl("/"); 
    return successRedirectHandler; 
} 

看來,以避免需要一個單獨的securityContext.xml文件。

+0

我希望我能接受兩個答案。我接受了我自己的,因爲它包含了更多關於如何做的細節以及進一步的反思,我認爲通過Java代碼使用XML文件是非常有價值的。基本上,我認爲HTTPSecurity上的Java API有點不足。 –

相關問題