如果用戶還沒有登錄,這意味着他沒有ROLE_USER
,所以Spring Security將不允許訪問/login
。
添加use-expressions="true"
to <http>
element和變化攔截線,以這樣的:
<http use-expressions="true">
<!-- ... -->
<security:intercept-url pattern="/login*" access="permitAll" />
另外,確保login.jsp的是/WEB-INF/jsp/login.jsp
下。
編輯後評論:
似乎是你有沒有春季安全配置好,所以具有此開始:
<http auto-config='true' use-expressions="true">
<intercept-url pattern="/login*" access="permitAll"/>
<intercept-url pattern="/**" access="ROLE_USER" />
<!-- use login-page='/login' assuming you've got
Spring MVC configured to redirect to login.jsp -->
<form-login login-page='/login'/>
</http>
,或者,如果你使用Spring安全3.1:
<http pattern="/login" security="none" />
<http auto-config='true'>
<intercept-url pattern="/**" access="ROLE_USER" />
<!-- use login-page='/login' assuming you've got
Spring MVC configured to redirect to login.jsp -->
<form-login login-page='/login'/>
</http>
創建/WEB-INF/jsp/login.jsp
帶有示例內容的文件:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<h3>Login</h3>
<c:if test="${not empty error}">
<div class="errorblock">
Your login attempt was not successful, try again.<br /> Caused :
${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message}
</div>
</c:if>
<form name='f' action="<c:url value='j_spring_security_check' />"
method='POST'>
<table>
<tr>
<td>User:</td>
<td><input type='text' name='j_username' value=''>
</td>
</tr>
<tr>
<td>Password:</td>
<td><input type='password' name='j_password' />
</td>
</tr>
<tr>
<td colspan='2'><input name="submit" type="submit"
value="submit" />
</td>
</tr>
</table>
</form>
</body>
</html>
更重要的是,請閱讀Spring Security,因爲這些都是這個框架的基礎。
錯誤看起來像您正在返回不存在的視圖名稱「登錄」(即沒有相應的JSP)。這與Spring Security沒有任何關係。這完全在您的控制器配置中,並且在Spring Security過濾器鏈被調用後發生。 –