1. 今日內容
文章目錄
2. 自定義登入學習
在前一篇文章中,我們提到了,當指定http
元素的auto-config=”true”
時,就相當於如下內容的簡寫:
<security:http>
<security:form-login/>
<security:http-basic/>
<security:logout/>
</security:http>
這些元素負責建立表單登錄、基本的認證和登出處理。它們都可以通過指定對應的屬性來改變它們的行爲。
下面,我們來介紹這些元素的用法,來實現自定義登入頁面。
2.1 form-login
http元素下的form-login元素是用來定義表單登錄信息的。當我們什麼屬性都不指定的時候Spring Security會爲我們生成一個默認的登錄頁面。如果不想使用默認的登錄頁面,我們可以指定自己的登錄頁面。
* form-login標籤下的屬性
1. login-page:指定登入頁面的路徑
2. login-processing-url:表示登入頁面中提交登錄時的請求地址,默認是“/login”。這個只是Spring Security用來標記登錄
頁面使用的提交地址,真正關於登錄這個請求是不需要用戶自己處理的,只需要前端的請求和這個一樣就能交給spring-security處理
3. username-parameter:表示登錄時提交表單時,用戶名的key,默認是“username”
4. password-parameter:表示登錄時提交表單時,用戶名的key,默認是“password”
5. authentication-failure-url:身份驗證失敗後發送的請求(可以直接寫也頁面,相當於直接請求頁面)
6. default-target-url:身份驗證登陸成功後發送的請求(可以直接寫也頁面,相當於直接請求頁面,但是一般寫頁面)
7. authentication-success-forward-url:身份驗證登陸成功後,發送的請求也成功後,再發出請求。(可以直接寫也頁面,相當於直接請求頁面,一般用於default-target-url的後續請求)
8. always-use-default-target: 是否使用default-target-url。默認爲false。true 適用於後臺管理系統,防止訪問歷史記錄
false 適用於前臺頁面,提升用戶體驗
注意:
- default-target-url一般用來指定登入成功後立馬跳轉到主界面,authentication-success-forward-url用來指定登入成功且跳到主界面後發送請求用戶其他信息的請求。
- 如果想要default-target-url生效,必須設置always-use-default-target=“true”
2.2 http-basic
通過定義http元素下的http-basic元素可以達到使用彈出對話框的形式進行身份驗證,之前使用的是表單。
注意:如果同時設置表單和對話框,會使用表單的形式,對話框失效。
比如使用如下代碼:
<security:http auto-config="true" use-expressions="false">
<security:http-basic/>
<security:intercept-url pattern="/**" access="ROLE_USER" />
</security:http>
效果如下:
2.3 logout
logout用來指定註銷後的跳轉的頁面和處理url:
* logout 標籤的屬性:
1. logout-url:登出處理鏈接,默認爲"/logout"
注:登出操作 只需要前端註銷的請求是/logout,即可註銷
比如:
logout-url=“/logout”
前端:<a href="${pageContext.request.conextPath}/logout"/>
2. logout-successurl:登出跳轉的頁面
3. invalidate-session: 是否刪除session ,默認爲"false"。
3. 自定義登入案例
3.1 login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title></head>
<body>
<form action="login" method="post">
<table>
<tr>
<td>用戶名:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>密碼:</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="登錄"/> <input type="reset" value="重置"/></td>
</tr>
</table>
</form>
</body>
</html>
3.2 success.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title></head>
<body> success html<br> <a href="logout">退出</a></body>
</html>
3.3 failer.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title></head>
<body>登錄失敗</body>
</html>
3.4 web.xml文件
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-security.xml</param-value>
</context-param>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
3.5 spring-security.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
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.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
<!-- 配置不過濾的資源(靜態資源及登錄相關) -->
<security:http security="none" pattern="/login.html"/>
<security:http security="none" pattern="/failer.html"/>
<security:http auto-config="true" use-expressions="false">
<!-- 配置資料連接,表示任意路徑都需要ROLE_USER權限 -->
<security:intercept-url pattern="/**"
access="ROLE_USER"/>
<!-- 自定義登陸頁面:
login-page 自定義登陸頁面
authentication-failure-url 用戶權限校驗失敗之後纔會跳轉到這個頁面,如果數據庫中沒有這個用戶則不會跳轉到這個頁面。
default-target-url 登陸成功後跳轉的頁面。
-->
<security:form-login login-page="/login.html" login-processing-url="/login" username-parameter="username"
password-parameter="password" authentication-failure-url="/failer.html"
default-target-url="/success.html"/> <!-- 登出, invalidate-session 是否刪除session logout-url:登出處理鏈接 logout-successurl:登出成功頁面 注:登出操作 只需要鏈接到 logout即可登出當前用戶 -->
<security:logout invalidate-session="true" logout-url="/logout"
logout-success-url="/login.jsp"/>
<!-- 關閉CSRF,默認是開啓的 -->
<security:csrf disabled="true"/>
</security:http>
<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="user" password="{noop}user" authorities="ROLE_USER"/>
<security:user name="admin" password="{noop}admin" authorities="ROLE_ADMIN"/>
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
</beans>
3.6 結果
登入:
登入成功:
登入失敗: