2009-11-12 53 views
0

我目前正在開發一個項目,我們使用freemarker作爲模板語言。春季安全自定義免費標記表格

@Controller 
public class LoginController { 
private static finaal String LOGIN = "components/security/login"; 

@RequestMapping("/security/login") 
public String login(){ 

    return LOGIN; 
    } 
} 

我Freemarker模板:

HTML代碼:

而不是使用defualt登錄表單我創建了一個自定義的控制器和與該控制器

代碼一起去定製freemarker的視圖

<form action="${rc.contextPath}/j_spring_security_check" method="post"> 
    <label for="username">Username</label><input type="text" id="username" name="j_username"><br/> 
    <label for="password">Password</label><input type="text" id="password" name="j_password"><br/> 
    <input type="submit" value="Login!"> 
</form> 

我的applicationContext-security.xml

<http> 
    <logout/> 
     <intercept-url pattern="/*" access="ROLE_ADMIN, ROLE_GUEST"/> 
     <intercept-url pattern="/security/login" filters="none"/> 
     <form-login login-page="/security/login" /> 
    </http> 

登錄工作像一個魅力,但是,當用戶輸入一個錯誤的用戶名或密碼沒有錯誤信息顯示,我不知道如何做到這一點。你能幫我解決這個問題嗎?

回答

6

誰不知道我是怎麼做的那些代碼:

<#if Session.SPRING_SECURITY_LAST_EXCEPTION?? && Session.SPRING_SECURITY_LAST_EXCEPTION.message?has_content> 
    <@spring.message "login.bad.credentials"/> 
</#if> 
0

您需要設置錯誤頁面:

<http> 
    ... 
    <form-login authentication-failure-url="/loginError.jsp" default-target-url="/start.jsp" /> 
    ... 
</http> 

loginError.jsp

... 
Login error. Reason: <c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}"/>. 
... 
+0

謝謝,但指定一個新的頁面是沒有必要的,但你的提示 與SPRING_SECU RITY_LAST_EXCEPTION.message讓我想到了。謝謝 – jakob 2009-11-13 08:15:55