2011-08-11 82 views
3

我想知道如何在Spring Security處理之前在登錄表單上進行額外驗證。我正在考慮一些LoginController,LoginForm bean,但我不知道如何將它們結合在一起,如何配置它工作。下面是我的「願景」。我已經搜索了一些例子如何在Spring Security中做到這一點,但無法找到。有人可以幫忙嗎?如何在Spring Security登錄表單中額外驗證?

LoginForm的豆:

public class LoginForm { 
    private String j_username; 
    private String j_password; 
    @NotEmpty 
    private Boolean acceptTerms; 

    public String getJ_password() { 
     return j_password; 
    } 

    public String getJ_username() { 
     return j_username; 
    } 

    public void setJ_password(String j_password) { 
     this.j_password = j_password; 
    } 

    public void setJ_username(String j_username) { 
     this.j_username = j_username; 
    } 

    public Boolean getAcceptTerms() { 
     return acceptTerms; 
    } 

    public void setAcceptTerms(Boolean accept) { 
     this.acceptTerms = acceptTerms; 
    } 
} 

形式:

<c:url value="/login" var="secureUrl"/> 
<form:form id="jf" commandName="loginForm" action="${secureUrl}" method="post"> 
    <form:input path="j_username"/> 
    <form:input path="j_password"/> 
    <form:checkbox path="acceptTerms"/> 
</form> 

的LoginController:

@Controller 
class LoginController { 
    @RequestMapping(value = "/login", method = RequestMethod.POST) 
    public String logging(@ModelAttribute @Valid LoginForm loginForm) { 
       ... 
    } 
} 

回答

2

你可以繼承UsernamePasswordAuthenticationFilter:

http://static.springsource.org/spring-security/site/docs/3.0.x/apidocs/org/springframework/security/web/authentication/UsernamePasswordAuthenticationFilter.html

您可以重寫attemptAuthentication()方法,讓它調用super.attemptAuthentication(),如果它返回一個非null的Authentication對象,請執行您的額外工作(查看用戶是否選中了該框)。

http://static.springsource.org/spring-security/site/docs/3.0.x/apidocs/org/springframework/security/web/authentication/UsernamePasswordAuthenticationFilter.html#attemptAuthentication(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)

爲如何指定客戶過濾器中的文檔是在這裏:

http://static.springsource.org/spring-security/site/docs/3.0.x/reference/ns-config.html#ns-custom-filters

1

只需添加上@sdouglass評論之上的更多信息。

在篩選器上,您無法訪問表單以進行綁定。

我設法通過執行以下操作來通過默認驗證來獲取它。

https://gist.github.com/3137040

3
@RequestMapping(value = "/login", method = RequestMethod.GET) 
public ModelAndView signInPage(
        @RequestParam(value = "error", required = false) String error, 
        @RequestParam(value = "logout", required = false) String logout) 
{ 
    ModelAndView mav = new ModelAndView(); 
    //Initially when you hit on login url then error and logout both null 
    if (error != null) { 
     mav.addObject("error", "Invalid username and password!"); 
    } 

    if (logout != null) { 
     mav.addObject("msg", "You've been logged out successfully."); 
    } 
    mav.setViewName("login/login.jsp"); 
} 

現在,如果萬一登錄成爲uncessful然後再重新打這個URL與錯誤的URL追加如春的安全文件設置失敗URL。彈簧安全文件:

<security:form-login 
    authentication-failure-url="/login?error=1" 
/> 

然後您的網址變成url/login?error=1。然後自動signInPage方法會調用並帶有一些錯誤值。現在錯誤不是null,您可以設置任何對應於url的字符串,我們可以使用以下標籤在jsp上顯示:

<c:if test="${not empty error}"> 
    <div class="error">${error}</div> 
</c:if>