目錄
前言
一、用戶登錄密碼加密認證
二、記住我功能
前言
本次筆記的記錄是接SSM項目集成Spring Security 4.X版本 之 加入DWZ,J-UI框架實現登錄和主頁菜單顯示-CSDN博客https://blog.csdn.net/u011529483/article/details/136255768?spm=1001.2014.3001.5502
文章之后補全spring-security登錄時用戶認證進行密碼加密驗證和實現記住我功能。
一、用戶登錄密碼加密認證
1. 修改上一篇文章中的項目wqdemotwo的spring-security.xml配置文件
打開如下標紅的兩處配置:
什么意思呢?
<bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"></bean> 是在配置文件中聲明一個bean,實現BCryptPasswordEncoder類。
<security:password-encoder ref="passwordEncoder"/> 是將聲明的bean “passwordEncoder” 注入到?authentication-provider 中去。對應的?AuthenticationProvider 類是認證提供者。此處指定了myUserDetailsService 服務類(獲取用戶詳情,如 用戶名,密碼等)
2. 生成加密后的密碼,實現登錄認證
進入加密算法實現類:BCryptPasswordEncoder
進入接口:PasswordEncoder
encode()方法對明文密碼進行加密。我們調用這個方法對數據庫中的密碼進行加密:
此處我隨意找一個類生成加密密碼:就 LoginController 類吧
運行項目生成加密密碼:$2a$10$n/q4O15Lg9d1WvelfRu9i.qrgE5iVjTeD4.hXAqsLZaGRmTKoGxtK錄入到數據庫中。
完成,現在重新啟動項目查看登錄效果:用戶:zhangsan,密碼:123456
系統登錄成功:
執行loadUserByUsername查詢用戶詳情方法后,后臺打印結果,密碼加密了。
3. 補充說明
總結一下:整個登錄認證過程中我們并沒有做密碼的對比校驗,但是密碼輸入錯誤是無法登錄認證成功的。這是因為密碼比對交由 SpringSecurity 處理了,登錄頁面傳入 用戶名 通過詳情查詢方法loadUserByUsername 獲取了UserDetails(得到了數據庫中的用戶名和密碼)然后返回給了SpringSecurity 進行密碼校驗。
spring-security.xml 配置文件中注入了AuthenticationManager 身份驗證管理器類,AuthenticationProvider認證提供者類,PasswordEncoder接口的實現類等。執行時會執行一系列相關類(源碼就不研究了,有情懷的小伙伴可以試試)。最終完成用戶登錄認證。而?密碼的校驗是由BCryptPasswordEncoder類 通過實現PasswordEncoder接口 的matches方法來完成。
我們可以來驗證一下。將數據庫中用戶zhangsan的密碼改回明文123456。此時運行項目進行登錄,結果是登錄失敗了:
后臺打印數據庫獲取的密碼是123456明文,小伙伴注意到下圖最后一行的紅字。
BCryptPasswordEncoder.matches Encoded password does not look like BCrypt:什么意思呢?就是BCryptPasswordEncode類的matches方法中報出了 密碼不屬于自己(BCrypt)的編碼格式,下圖:
matches方法中如果通過了編碼格式校驗則進入checkpw(rawPassword.toString(), encodedPassword)方法,rawPassword登錄頁面傳入的,encodedPassword數據庫中存儲的。所以密碼的校驗是由BCryptPasswordEncoder類 通過實現PasswordEncoder接口 的matches方法來完成。
二、記住我功能
1. 將上篇文章項目wqdemotwo的spring-security.xml配置文件,打開如下兩處配置
說明:token-validity-seconds="300" 屬性指定免登錄訪問資源的維持時間,超過這個時間沒有任何資源請求,便需要重新登錄。
及
注意:第一次運行項目時要將<!--<property name="createTableOnStartup" value="true"/>--> 配置打開,createTableOnStartup屬性是當項目啟動時,springSecurity創建表存儲remember me相關信息,第二次啟動時要注釋這個屬性。
????說明:<property name="dataSource" ref="dataSource"/>指定數據庫數據源,如oracle。ref="dataSource"注入的是applicationContext.xml配置文件中的數據源,如下圖:
2. 修改登錄頁面
增加 記住我 復選框,如圖:
到此記住我功能配置完成,打開spring-security.xml文件的createTableOnStartup屬性運行項目,按預期設想應該在數據庫生成表PERSISTENT_LOGINS:
-- Create table
create table PERSISTENT_LOGINS
(username VARCHAR2(64) not null,series VARCHAR2(64) not null,token VARCHAR2(64) not null,last_used TIMESTAMP(6) not null
);
-- Create/Recreate primary, unique and foreign key constraints
alter table PERSISTENT_LOGINSadd primary key (SERIES)
登錄頁面選擇?記住我 登錄成功后,關閉瀏覽器可以實現免登錄再次訪問資源。
選擇?記住我?進行登錄,成功登錄了。此時我打開oracle數據庫生成了PERSISTENT_LOGINS表
頁面也成功訪問到資源:
此時關閉瀏覽器,再次打開瀏覽器因該可以不用登錄就可以直接訪問主頁面。但是我的想法是美好的,事實卻是訪問?http://localhost:8080/wqdemotwo_war/system/index?跳轉到了登錄頁面,經過努力查找原因是:spring-security.xml文件的?<security:intercept-url pattern="/**" access="isFullyAuthenticated()"/> 配置導致,需要改成 <security:intercept-url pattern="/**" access="isAuthenticated()"/> 這個。
isAuthenticated():Returns true if the user is not anonymous(如果用戶不是匿名的,則返回true)
isFullyAuthenticated(): Returns true if the user is not an anonymous or a remember-me user(如果用戶不是匿名用戶或記住我的用戶,則返回true)
修改后再次運行項目,便可以實現 記住我 功能,即:登錄成功后關閉瀏覽器,再次打開瀏覽器訪問資源可以免登錄訪問。(記住注釋掉spring-security.xml文件的<!--<property name="createTableOnStartup" value="true"/>-->屬性)
好了,今天的記錄到此結束。