如果用户尚未登录,则表示他没有ROLE_USER
,因此 Spring Security 将不允许访问/login
。
添加use-expressions="true"
到<http>
元素并将截距线更改为:
<http use-expressions="true">
<!-- ... -->
<security:intercept-url pattern="/login*" access="permitAll" />
另外,确保 login.jsp 在/WEB-INF/jsp/login.jsp
.
评论后编辑:
似乎你根本没有 Spring Security 配置,所以从这个开始:
<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 Security 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,因为这些是这个框架的基础。