本篇博客基於https://www.cnblogs.com/my-program-life/p/12076474.html
一、自定義用戶訪問控制
1、在SecurityConfig類中重寫configure(HttpSecurity http)方法
@Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() // 設置用戶訪問權限,路徑為"/"的請求直接放行 .antMatchers("/").permitAll() // 只有角色為common才允許訪問 .antMatchers("/detail/common/**").hasRole("common") // 只有角色為vip才允許訪問 .antMatchers("/detail/vip/**").hasRole("vip") // 其他請求要求用戶必須先進行登錄認證 .anyRequest().authenticated() .and() .formLogin(); }
2、效果測試
進入首頁,點擊普通電影下的任意條目,自動跳轉到登錄頁面,在這里使用普通用戶登錄(用戶名czy,密碼123)
普通用戶沒有權限訪問VIP專享電影,在頁面會顯示403錯誤(禁止訪問)
二、自定義用戶登錄
1、自定義用戶登錄頁面
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>用戶登錄界面</title> <link rel="stylesheet" th:href="@{/login/css/bootstrap.min.css}"> <link rel="stylesheet" th:href="@{/login/css/signin.css}"> </head> <body class="text-center"> <form class="form-signin" th:action="@{/userLogin}" th:method="post"> <img class="mb-4" th:src="@{/login/img/login.jpg}" width="72px" height="72px"> <h1 class="h3 mb-3 font-weight-normal">請登錄</h1> <!-- 用戶登錄錯誤信息提示框 --> <div th:if="${param.error}" style="color: red;height: 40px;text-align: left;font-size: 1.1em"> <img th:src="@{/login/img/loginError.jpg}" width="20px"> 用戶名或密碼錯誤,請重新登錄! </div> <input type="text" name="name" class="form-control" placeholder="用戶名" required="required" autofocus="autofocus"> <input type="password" name="pwd" class="form-control" placeholder="密碼" required="required" autofocus="autofocus"> <button class="btn btn-lg btn-primary btn-block" type="submit">登錄</button> <p class="mt-5 mb-3 text-muted">Copyright© <span th:text="${currentYear}"></span>- <span th:text="${currentYear}+1"></span> </p> </form> </body> </html>
2、自定義用戶登錄跳轉
3.自定義用戶登錄控制
4、效果測試
輸入錯誤的密碼
輸入正確的用戶名和密碼,登錄成功
三、自定義用戶退出
1、添加自定義用戶退出鏈接
2、自定義用戶退出控制
3、效果測試
先進行登錄,然后點擊注銷按鈕后,再次進入詳情頁需要重新登錄
四、登錄用戶信息獲取
1、使用HttpSession獲取用戶信息

@GetMapping("/getuserBySession") @ResponseBody public void getUser(HttpSession session) { // 從當前HttpSession獲取綁定到此回話的所有對象的名稱 Enumeration<String> names = session.getAttributeNames(); while (names.hasMoreElements()) { // 獲取HttpSession中會話名稱 String element = names.nextElement(); // 獲取HttpSession中的應用上下文 SecurityContextImpl attribute = (SecurityContextImpl) session.getAttribute(element); System.out.println("element:" + element); System.out.println("attribute:" + attribute); // 獲取用戶相關信息 Authentication authentication = attribute.getAuthentication(); UserDetails principal = (UserDetails) authentication.getPrincipal(); System.out.println(principal); System.out.println("username:" + principal.getUsername()); } }
2、使用SecurityContextHolder獲取用戶信息(推薦使用)
五、記住我功能
1、基於簡單加密Token的方式
2、基於持久化Token的方式
創建persistent_logins表
# 記住我功能中創建持久化Token存儲的數據表 create table persistent_logins (username varchar(64) not null, series varchar(64) primary key, token varchar(64) not null, last_used timestamp not null);
六、CSRF防護功能
CSRF(Cross-site request forgery,跨站請求偽造),也被稱為“One Click Attack”(一鍵攻擊)或者“Session Riding”(會話控制),
通常縮寫為SCRF或者XSRF,是一種對網站的惡意利用。
CSRF攻擊要保護的對象是那些可以直接產生數據變化的服務,而對於讀取數據的服務,可以不進行CSRF保護。
例如,銀行轉賬操作會改變賬號金額,需要進行CSRF保護。
獲取銀行卡等級信息是讀取操作,不會改變數據,可以不需要保護。
防御CSRF攻擊的主要策略
(1)驗證HTTP Referer字段。
(2)在請求地址中添加Token並驗證。
(3)在HTTP頭中自定義屬性並驗證。
1、CSRF防護功能關閉
Spring Boot整合Spring Security默認開啟了CSRF防御功能,並要求數據修改的請求方法都需要經過
Security配置的安全認證后方可正常訪問,否則無法正常發送請求。
@Controller public class CSRFController { // 向用戶修改頁跳轉 @GetMapping("/toUpdate") public String toUpdate() { return "csrf/csrfTest"; } // 用戶修改提交處理 @ResponseBody @PostMapping(value = "/updateUser") public String updateUser(@RequestParam String username, @RequestParam String password, HttpServletRequest request) { System.out.println(username); System.out.println(password); String csrf_token = request.getParameter("_csrf"); System.out.println(csrf_token); return "ok"; } }
再次訪問,輸出OK
2、針對Form表單數據修改的CSRF Token配置
方法一:在提交的表單中添加CSRF Token信息的隱藏域
方法二:使用Thymeleaf模板的th:action屬性配置Form 表單數據修改后的請求路徑(會默認攜帶CSRF Token信息)
3、針對Ajax數據修改請求的CSRF Token配置
Security管理前端頁面