上一個例子只是初探了一下Spring Security的用法,簡單的不能再簡單的。今天進一步去體驗Spring Security的用法,豐富之前的例子。
在上一個demo的基礎上改進之處:
1、使用自己編寫的登錄頁面;
2、將明文密碼進行MD5加密;
3、進行國際化;
4、是用自己定義的頁面替代瀏覽器403無權訪問提示;
5、設置session過期跳轉頁面;
6、防止多次登錄;
自己定義的登陸頁面如下:login.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>login</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body onload='document.f.j_username.focus();'>
<h3>
${sessionScope.SPRING_SECURITY_LAST_EXCEPTION.message}
<br/>
用戶登錄
</h3>
<form name='f' action='<%=request.getContextPath() %>/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" />
</td>
</tr>
<tr>
<td colspan='2'>
<input name="reset" type="reset" />
</td>
</tr>
</table>
</form>
</body>
</html>
自定義無權訪問頁面accessDenied.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>無權訪問</title>
</head>
<body>
您的訪問被拒絕,您無權訪問該資源
</body>
</html>
自定義Session過期頁面sessionTimeout.jsp
<%@ page language="java" pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>用戶登錄已超時</title>
<style type="text/css">
body{
text-align: center;
}
#sessionOut {
margin-top: 50px;
padding: 15px 50px;
width: 500px;
border: 2px solid green;
background-color: yellow;
text-align: center;
}
a{
font-weight:bold;
font-family:"宋體";
font-size:18px;
}
</style>
</head>
<body>
<div id ="sessionOut">
您長時間未操作系統,爲確保您的資料及數據信息安全,
系統自動超時退出,請重新<a href="http://<%=request.getRemoteAddr()%>:<%= request.getServerPort()%>/security1">登錄</a>系統!
</div>
</body>
<script type="text/javascript">
if (self != top){
window.top.location = window.location;
}
</script>
</html>
修改配置文件applicationContext.xml達到以上功能:
<?xml version="1.0" encoding="UTF-8"?> <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' access-denied-page="/accessDenied.jsp"> <!-- login-page表示用戶登陸時顯示我們自定義的login.jsp authentication-failure-url表示用戶登陸失敗時,跳轉到哪個頁面,並添加一個error=true參數作爲登陸失敗的標示 default-target-url表示登陸成功時,跳轉到哪個頁面 --> <form-login login-page="/login.jsp" authentication-failure-url="/login.jsp?error=true" default-target-url="/index.jsp" /> <!--登錄頁面不進行過濾,後面加一個*那是因爲請求頁面後面會帶參數--> <intercept-url pattern="/login.jsp*" filters="none"/> <intercept-url pattern="/admin.jsp" access="ROLE_ADMIN" /> <intercept-url pattern="/**" access="ROLE_USER" /> <!-- 檢測失效的sessionId,超時時定位到另外一個URL --> <session-management invalid-session-url="/sessionTimeout.jsp" > <!-- 防止第二次登錄 如果想讓第一次登錄失效第二次登錄啓用則不要配置error-if-maximum-exceeded="true" --> <concurrency-control max-sessions="1" error-if-maximum-exceeded="true"/> </session-management> </http> <authentication-manager> <authentication-provider> <!-- 加密用戶的密碼 --> <password-encoder hash="md5"/> <user-service> <!-- <user name="admin" password="admin" authorities="ROLE_USER, ROLE_ADMIN" /> <user name="user" password="user" authorities="ROLE_USER" /> --> <user name="admin" password="21232f297a57a5a743894a0e4a801fc3" authorities="ROLE_USER, ROLE_ADMIN" /> <user name="user" password="ee11cbb19052e40b07aac0ca060c23ee" authorities="ROLE_USER" /> </user-service> </authentication-provider> </authentication-manager> <!-- 國際化 --> <beans:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <!-- 如果不加載自己的國際化文件,去加載 Security 內部的國際化文件classpath:org/springframework/security/messages_zh_CN --> <beans:property name="basename" value="classpath:messages_zh_CN"/> </beans:bean> </beans:beans>
上面每個新添加的功能配置多進行了相應的註釋說明。