2013-03-21 84 views
5

我有彈簧安全到位,並通過login.jsp登錄工作完全正常。Spring Security - 調度到/ j_spring_security_check

現在,我必須自動讓用戶根據URL登錄(類似於Single Sign On)。我基本上有一個路徑參數在URL基本上是一個加密的代碼,我處理此代碼做一個自動登錄。

我修改我的LoginController檢查,如果我有使用,我讓我的用戶名密碼&一個有效的路徑PARAM,使用該用戶名密碼&我做"forward:/j_spring_security_check?j_username="+username+"&j_password="+password

這會指引我下面的錯誤轉到login.jsp Your login attempt was not successful, try again. Caused : Authentication method not supported: GET

我也試過"redirect:/j_spring_security_check?j_username="+username+"&j_password="+password,但沒有任何幫助。

電話/j_spring_security_checkPOSTforward: & redirect:正在做GET,所以我怎麼能分派到/j_spring_security_checkPOST從我的LoginController?

感謝

+0

檢查這可以幫助你:http://stackoverflow.com/a/8229195/322166 – Dani 2013-03-21 13:32:43

回答

10

/j_spring_security_check將URL映射到UsernamePasswordAuthenticationFilter以提供請求。

UsernamePasswordAuthenticationFilter中,默認情況下,postOnly設置爲true

以下更改spring-security.xml其中postOnly設置爲false工作。

<bean id="authenticationFilter" 
     class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter" 
     p:postOnly="false" /> 

此外,在web.xml,需要以下配置:

<filter-mapping> <filter-name>springSecurityFilterChain</filter-name> 
    <url-pattern>/*</url-pattern> 
    <dispatcher>REQUEST</dispatcher> 
    <dispatcher>FORWARD</dispatcher> 
</filter-mapping> 
3

你可以使用它返回「POST」,而不是「GET」爲getMethod請求包裝繞過檢查。

但是,檢查是有原因的。將憑據作爲URL參數發送通常被認爲是不好的做法。即使您使用的是加密參數,它仍然在技術上等同於發送未加密的身份驗證憑據,因爲竊取它的任何人都可以使用它來進行身份驗證。

+0

謝謝您寶貴的意見。現在我可以想到除了直接使用URL參數調用'/ j_spring_security_check'外,沒有別的選擇,如果你有一個請分享。 – 2013-03-22 10:25:22

+0

/j_spring_security_check不是安全問題所在。你可以直接自己直接調用'AuthenticationManager',而不是嘗試創建一個表單登錄過濾器的請求(這是一個壞主意,尤其是重定向)。但是,首先在URL中發送路徑參數可能不是一個好主意。 – 2013-03-22 13:33:07