在前面三個博客的例子中,登錄頁面都是用的Spring Security本身提供的,這明顯不符合實際開發場景,同時也沒有退出和註銷按鈕,所以在每次測試的時候都要經過關閉瀏覽器來註銷達到清除session的效果。html
login.jsp:java
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>自定義登錄頁面</title>
</head>
<body>
<div class="error ${param.error == true ? '' : 'hide'}">
登錄失敗<br>
${sessionScope['SPRING_SECURITY_LAST_EXCEPTION'].message}
</div>
<form method="post" action="${pageContext.request.contextPath}/j_spring_security_check" style="width:260px; text-align: center">
<fieldset>
<legend>登錄</legend>
用戶: <input type="text" name="j_username" style="width: 150px;"
value="${sessionScope['SPRING_SECURITY_LAST_USERNAME']}" /><br />
密碼: <input type="password" name="j_password" style="width: 150px;" /><br />
<input type="checkbox" name="_spring_security_remember_me" />兩週以內沒必要登錄<br />
<input type="submit" value="登錄" /> <input type="reset" value="重置" />
</fieldset>
</form>
</body>
</html>複製代碼
說明:特別要注意的是form表單的action是提交登錄信息的地址,這是security內部定義好的,同時自定義form時,要把form的action設置爲/jspringsecurity_check。注意這裏要使用絕對路徑,避免登錄頁面存放的頁面可能帶來的問題。jusername,輸入登錄名的參數名稱,jpassword,輸入密碼的參數名稱,這兩個正常狀況下也不會修改。mysql
springsecurityrememberme,選擇是否容許自動登陸的參數名稱。能夠直接把這個參數設置爲一個checkbox,無需設置value,Spring Security會自行判斷它是否被選中,這也是security內部提供的,只須要配置,不須要本身實現。spring
配置文件以下:sql
<?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/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">
<!-- <http pattern="/login.jsp" security="none"></http> -->
<http auto-config="false">
<intercept-url pattern="/login.jsp" access="IS_AUTHENTICATED_ANONYMOUSLY" />
<intercept-url pattern="/adminPage.jsp" access="ROLE_ADMIN" />
<intercept-url pattern="/**" access="ROLE_USER" />
<form-login login-page="/login.jsp" default-target-url="/index.jsp"
authentication-failure-url="/login.jsp?error=true" />
<logout invalidate-session="true"
logout-success-url="/login.jsp"
logout-url="/j_spring_security_logout"/>
</http>
<!-- 數據源 -->
<beans:bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<!-- 此爲c3p0在spring中直接配置datasource c3p0是一個開源的JDBC鏈接池 -->
<beans:property name="driverClass" value="com.mysql.jdbc.Driver" />
<beans:property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/springsecuritydemo?useUnicode=true&characterEncoding=UTF-8" />
<beans:property name="user" value="root" />
<beans:property name="password" value="" />
<beans:property name="maxPoolSize" value="50"></beans:property>
<beans:property name="minPoolSize" value="10"></beans:property>
<beans:property name="initialPoolSize" value="10"></beans:property>
<beans:property name="maxIdleTime" value="25000"></beans:property>
<beans:property name="acquireIncrement" value="1"></beans:property>
<beans:property name="acquireRetryAttempts" value="30"></beans:property>
<beans:property name="acquireRetryDelay" value="1000"></beans:property>
<beans:property name="testConnectionOnCheckin" value="true"></beans:property>
<beans:property name="idleConnectionTestPeriod" value="18000"></beans:property>
<beans:property name="checkoutTimeout" value="5000"></beans:property>
<beans:property name="automaticTestTable" value="t_c3p0"></beans:property>
</beans:bean>
<authentication-manager>
<authentication-provider>
<jdbc-user-service data-source-ref="dataSource"
users-by-username-query="select username,password,status as enabled from user where username = ?"
authorities-by-username-query="select user.username,role.name from user,role,user_role
where user.id=user_role.user_id and
user_role.role_id=role.id and user.username=?"/>
</authentication-provider>
</authentication-manager>
</beans:beans>複製代碼
說明:express
<http auto-config="false" use-expressions="true">
<intercept-url pattern="/login.jsp" access="permitAll" />
<intercept-url pattern="/adminPage.jsp" access="hasRole('ROLE_ADMIN')" />
<intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
<form-login login-page="/login.jsp" default-target-url="/index.jsp"
authentication-failure-url="/login.jsp?error=true" />
<logout invalidate-session="true"
logout-success-url="/login.jsp"
logout-url="/j_spring_security_logout"/>
</http>複製代碼
配置文件中的其餘配置在前面幾篇博客中都有詳細的講解,這裏就不贅述了。瀏覽器
index.jsp微信
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>this is a user page</h1>
<a href="${pageContext.request.contextPath}/j_spring_security_logout">退出登錄</a>
</body>
</html>複製代碼
adminPage.jspsession
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>this is a admin page</h1>
<a href="${pageContext.request.contextPath}/j_spring_security_logout">退出登錄</a>
</body>
</html>複製代碼
這裏定義了兩個頁面,index.jsp用戶和管理員均可以訪問,adminPage.jsp只有管理員能夠訪問,同時兩個頁面都有註銷按鈕,註銷按鈕提交的地址也就是上面配置文件中的地址/jspringsecurity_logout。pom.xml和前面的同樣,這裏就不貼了。jsp
當輸入普通用戶的用戶名和密碼,同時勾選2周不用登錄後,由於adminPage.jsp頁面要有管理員權限才能訪問,因此普通用戶訪問失敗,index.jsp頁面就能夠訪問;這時關閉頁面後,再次訪問資源,由於勾選了2周不用登錄,因此能夠成功訪問;可是當點擊退出登陸後,再次訪問是就會跳轉到登錄頁面,要求登錄才能訪問。
當輸入管理員名和密碼,同時勾選2周不用登錄,驗證成功後,跳轉到index.jsp,同時adminPage.jsp也能夠訪問,這時把一個頁面關閉再從新訪問資源時,由於勾選2周不用登錄,因此能夠成功訪問;而後註銷,這是再訪問資源時,就會跳轉到登錄頁面,要求登錄才能訪問。微信公衆號關注:ByteZ,獲取更多學習資料