利用spring security控制用户权限,可以用其自带的登陆界面,还可以自定义登陆界面,其步骤是:
在security.xml中加入:
<http auto-config=’true’>
<intercept-url pattern="/login.jsp" access="IS_AUTHENTICATED_ANONYMOUSLY" />
<intercept-url pattern="/**" access="ROLE_READ_RESOURCES" />
<form-login login-page="/login.jsp"
authentication-failure-url="/error.jsp"
default-target-url="/" />
</http>
定义登陆界面:login.jsp
<form action="${pageContext.request.contextPath}/j_spring_security_check" >
<legend>登陆</legend>
<br /><br />
用户: <input type="text" name="j_username" value="${sessionScope['SPRING_SECURITY_LAST_USERNAME']}"/><br />
密码: <input type="password" name="j_password" /><br />
<input type="checkbox" name="_spring_security_remember_me" />两周之内不必登陆<br />
<input type="submit" value="登陆"/>
<input type="reset" value="重置"/>
</form>
登陆界面显示用户信息:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
…
<sec:authentication property="principal" var="authentication"/>
<sec:authorize ifAllGranted="ROLE_USER">可以访问</sec:authorize>
用户名:${authentication.username }<br />
</body>
</html>