2012-12-04 73 views
4

我正在Spring上開發Web應用程序,使用Spring Security 3和LDAP進行身份驗證。spring security 3 authentication-failure-url change

這是我的代碼的形式登錄片段:

<security:form-login 
    authentication-failure-url="/index.xhtml?error=true" 
    default-target-url="/SomeDefaultUrl.xhtml" 
    login-page="/index.xhtml" /> 

身份驗證失敗時我的應用程序將被重定向到「/index.xhtml?error=true」網址。問題是我不知道如何捕獲「error」變量並在index.xhtml文件中顯示一些身份驗證失敗消息。我沒有使用Spring mvc。

第二個問題是更改authentication-failure-url不起作用。

<security:form-login 
    authentication-failure-url="/error.xhtml" 
    default-target-url="/SomeDefaultUrl.xhtml" 
    login-page="/index.xhtml" /> 

我改變了認證失敗的URL,但儘管這樣的改變,它仍然重定向到的index.xhtml文件,而無需任何變量。

我怎樣才能解決這個問題?

回答

1

重要:/index.xhtml?error=true發送errorGET參數。

如果您index.xhtml是一個JSP文件,您可以使用隱式request參考訪問PARAM:

<%= request.getParameter("error") %> 

如果index.xhtml是你的網絡控制器方法/ servlet的網址,你需要從Request對象獲取error PARAM。

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException 
{  
    String error = req.getParameter("error"); 
} 

最後,如果你index.xhtml是一個普通的HTML文件,你可以使用Java腳本來獲取參數: How to retrieve GET parameters from javascript?

關於第二個問題:請確保您正確重建項目。它看起來沒有注意到你的改變。

+0

謝謝你的偉大的建議。 關於第二個問題:我重建項目,它注意到我的變化,因爲它被重定向到'index.xhtml'文件而不是'index.xhtml?error = true' 我認爲當authentication-failure-url參數的值不是_acceptable_它會自動重定向到index.xhtml。但我不知道如何解決它 – mariami

3

如果您index.xhtml是JSP,你可以做到這一點(直接從春季3食譜書):

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<c:if test="${not empty param.error}"> 
    Login error. 
    Reason ${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message} 
</c:if> 
0
<security:form-login 
    authentication-failure-url="/error.xhtml" 
    default-target-url="/SomeDefaultUrl.xhtml" 
    login-page="/index.xhtml" /> 

「我改變了認證失敗的URL,但儘管這樣的變化,它仍然沒有任何變量重定向到index.xhtml文件。「

答案:請在您的<http auto-config="true" use-expressions="true"></http>正文中定義下面的攔截url。

<intercept-url pattern="/error.xhtml" access="permitAll"/> 

您可能沒有定義訪問/error.xhtml,這就是爲什麼認證失敗-URL值是不能接受的,並且通過所定義即/索引的下一個邏輯URL落下。HTML

相關問題