2012-01-03 124 views
1

如何在春季安全

我已經寫了春天的安全XML文件的login.jsp,控制器運行自定義登錄頁面。但仍然無法運行。我得到了404錯誤。如何在xml中添加或配置。

謝謝

回答

1

您不需要自定義控制器。

你只需要一個JSP(x)中提出的兩個值

  • j_username
  • j_password

,需要更改配置爲使用表單

<beans:beans xmlns="http://www.springframework.org/schema/security" 
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd"> 


<http auto-config="true" use-expressions="true"> 
    <logout logout-url="/static/j_spring_security_logout"/> 
    <form-login login-processing-url="/static/j_spring_security_check" login-page="/login" authentication-failure-url="/login?login_error=t"/> 

    <intercept-url pattern="/js/**" access="permitAll" /> 
    <intercept-url pattern="/css/**" access="permitAll" /> 
    <intercept-url pattern="/images/**" access="permitAll" /> 
    <intercept-url pattern="/static/**" access="permitAll" /> 
    <intercept-url pattern="/login**" access="permitAll" /> 
    <intercept-url pattern="/**" access="isAuthenticated()" /> 
</http> 
... 

重要提示:您無需對01進行身份驗證以及login-processing-url

  • 登錄頁==當用戶需要輸入用戶名和密碼
  • 登錄處理的URL顯示的頁面==用戶的用戶名和密碼提交到

login.jspx:

<spring:url value="/static/j_spring_security_check" var="form_url" /> 
<form name="f" action="${fn:escapeXml(form_url)}" method="POST"> 
    <input type='text' name='j_username' /> 
    <input type='password' name='j_password' " /> 
    <c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}" /> 
</form> 
3

添加定義,就像下面你Spring上下文。

<security:http> 
<security:form-login login-page='/login' authentication-failure-url="/login?authfailed=true"/> 
<security:logout /> 
</security:http> 

您還必須使用<security:intercept-url>定義URL模式。