spring security登錄認(rèn)證授權(quán)的項(xiàng)目實(shí)踐
是什么
Spring Security 主要實(shí)現(xiàn)了Authentication(認(rèn)證,解決who are you? ) 和 Access Control(訪問控制,也就是what are you allowed to do?,也稱為Authorization)。Spring Security在架構(gòu)上將認(rèn)證與授權(quán)分離,并提供了擴(kuò)展點(diǎn)
使用-1 快速開始 引入依賴,出現(xiàn)登錄頁面
<!-- 實(shí)現(xiàn)對(duì) Spring MVC 的自動(dòng)化配置 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 實(shí)現(xiàn)對(duì) Spring Security 的自動(dòng)化配置 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>

自定義登錄的用戶名密碼(練手)
配置類繼承適配器 WebSecurityConfigurerAdapter, 重寫configure方法,實(shí)現(xiàn)UserDetailsService接口,設(shè)置用戶名,密碼

從數(shù)據(jù)庫讀取密碼
配置類繼承適配器 WebSecurityConfigurerAdapter, 重寫configure方法,實(shí)現(xiàn)UserDetailsService接口,調(diào)用UserDetailsService的已有的實(shí)現(xiàn)類,或者調(diào)用自定義的類,實(shí)現(xiàn)了UserDetailsService實(shí)現(xiàn)類接口,重寫loadUserByUsername方法(數(shù)據(jù)庫查登錄的用戶名密碼)


認(rèn)證邏輯
認(rèn)證邏輯:SecurityContextPersistenceFilter Security上下文持久化過濾器拿到登錄信息,生成session存儲(chǔ) ->SecurityContextHolderStrategy 策略 ->ThreadLocalSecurityContextHolderStrategy 用的ThreadLocal
AbstractAuthenticationProcessingFilter 的 doFilter方法->attemptAuthentication方法->AuthenticationManager接口的authenticate接口方法
->retrieveUser->getUserDetailsService().loadUserByUsername(獲取到自定義的數(shù)據(jù)庫獲取用戶名密碼方法)–>authenticate方法中 調(diào)用additionalAuthenticationChecks
檢查用戶名密碼的準(zhǔn)確性–>正確的話就走認(rèn)證成功的邏輯,認(rèn)證成功后 ->SecurityContextHolder.getContext().setAuthentication設(shè)置登錄信息到上下文;錯(cuò)誤就走認(rèn)真失敗的邏輯
自定義認(rèn)證成功失敗的處理

](http://img.jbzj.com/file_images/article/202501/2025012011244112.png)

自定義登錄成功失敗邏輯(執(zhí)行方法步驟)原理:認(rèn)證成功后: successForwardUrl–>ForwardAuthenticationSuccessHandler->調(diào)用了AuthenticationSuccessHandler 的onAuthenticationSuccess 方法
因此可以實(shí)現(xiàn)AuthenticationSuccessHandler接口,重寫 onAuthenticationSuccess 方法
認(rèn)證失敗同理:failureForwardUrl->ForwardAuthenticationFailureHandler->調(diào)用了 ForwardAuthenticationFailureHandler的 onAuthenticationFailure 方法
因此可以實(shí)現(xiàn) ForwardAuthenticationFailureHandler接口,重寫 onAuthenticationFailure 方法
會(huì)話管理(session)
是什么?http請(qǐng)求是無狀態(tài)的,因此需要在瀏覽器和服務(wù)器端存儲(chǔ)狀態(tài),即會(huì)話。
tomcat 會(huì)生成session對(duì)象,響應(yīng)的時(shí)候會(huì)帶給客戶端存到瀏覽器的Cookie中,
下次再請(qǐng)求服務(wù)器時(shí),帶上JSESSIONID,和服務(wù)器的JSESSIONID比對(duì),相同的話響應(yīng),否則不響應(yīng)
原理 :SecurityContextHolder.getContext().getAuthentication()可以獲取到登錄信息,因?yàn)榈卿浀恼J(rèn)證信息存在 SecurityContext上下文中
AbstractAuthenticationProcessingFilter 的 doFilter方法 -> successfulAuthentication 認(rèn)證成功后 ->SecurityContextHolder.getContext().setAuthentication 登錄的認(rèn)證信息存在 SecurityContext上下文中
會(huì)話并發(fā)控制-spring security實(shí)現(xiàn)
同一個(gè)賬號(hào)只能同時(shí)登錄一次(后面登錄人把前面登錄人擠掉)—>可以用redis實(shí)現(xiàn),此處是用spring security實(shí)現(xiàn)
http.sessionManagement().maximumSessions(1)原理:AbstractAuthenticationProcessingFilter -> sessionStrategy.onAuthentication()-> ConcurrentSessionControlAuthenticationStrategy 的onAuthentication方法比較當(dāng)前登錄的session個(gè)數(shù)是否小于我們自己設(shè)置 -> 是的話就session 設(shè)置過期- >ConcurrentSessionFilter 并發(fā)session過濾器 -> doFilter方法 -> 沒有過期則登錄成功,否則走session過期策略
可自定義實(shí)現(xiàn)SessionInformationExpiredStrategy接口重寫onExpiredSessionDetected方法
問題:http.sessionManagement().maximumSessions(1).maxSessionsPreventsLogin(true) //超過最大的登錄數(shù),阻止登錄
問題:我們?cè)趛ml配置的http session設(shè)置的超時(shí)時(shí)間和內(nèi)部SessionInformation 是重新的一個(gè)session,超時(shí)時(shí)間和之前不同,導(dǎo)致http session設(shè)置的超時(shí)時(shí)間超時(shí)了,但是SessionInformation 沒超時(shí)會(huì)報(bào)多人同時(shí)登錄的錯(cuò)誤最好不要用,可以用redis實(shí)現(xiàn)
會(huì)話并發(fā)控制-redis實(shí)現(xiàn)
實(shí)現(xiàn)原理:引入的spring session包中有個(gè)session倉庫的過濾器 SessionRepositoryFilter -> doFilter方法 ->commitSession ->sessionRepository.save(session)實(shí)現(xiàn)了重寫了RedisSessionRepository中save 方法,把session存到redis中
1.引入spring session依賴
<dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.1.0</version> </dependency>
2.修改application.yml
session:
store-type: redis
redis:
host: localhost
port: 6379
3.開啟多個(gè)項(xiàng)目實(shí)例即可測(cè)試
Remember me實(shí)現(xiàn)
設(shè)置數(shù)據(jù)源,Spring Security會(huì)自動(dòng)把用戶信息存儲(chǔ)到數(shù)據(jù)源中,下次登錄直接比對(duì)數(shù)據(jù)庫值就ok
原理:RememberMeAuthenticationFilter -> doFilter方法 -> autoLogin ->AbstractRememberMeServices的autoLogin ->PersistentTokenBasedRememberMeServices 的processAutoLoginCookie 取出remember-me的值拆分為series和token 去數(shù)據(jù)庫根據(jù)series找到記錄,看token是否一致一致就調(diào)用 getUserDetailsService().loadUserByUsername方法,實(shí)現(xiàn)自動(dòng)登錄


退出登錄
原理:LogoutFilter-> doFilter方法 -> SecurityContextLogoutHandler的logout方法,setAuthentication為null清除認(rèn)證狀態(tài),銷毀HTTPSession對(duì)象
SimpleUrlLogoutSuccessHandler的onLogoutSuccess 重定向到登錄頁面
csrf跨站請(qǐng)求偽造
從 Spring Security4開始CSRF防護(hù)默認(rèn)開啟,默認(rèn)會(huì)攔截請(qǐng)求,進(jìn)行CSRF處理。CSRF為了保證不是其他第三方網(wǎng)站訪問,要求訪問時(shí)攜帶參數(shù)名為 _csrf 值為token(token 在服務(wù)端產(chǎn)生,在渲染請(qǐng)求頁面時(shí)埋入頁面)的內(nèi)容,如果token和服務(wù)端的token匹配成功,則正常訪問。
原理:CsrfFilter -> doFilter方法 -> 如果沒有token 先生成一個(gè),然后放到填充到CSRF_TOKEN中返回給頁面,會(huì)再走一遍doFilter方法去比對(duì)CSRF_TOKEN和服務(wù)端的token是否能匹配成功,成功則可以訪問,否則不能訪問



授權(quán)
這里是引用訪問控制url的匹配
antMatchers ant 表達(dá)式,統(tǒng)配符 ? 匹配任何單字符,* 匹配0或者任意數(shù)量的字符, ** 匹配0或者更多的目錄
hasAuthority 有…的角色權(quán)限
hasAnyAuthority 有多個(gè)角色權(quán)限,符合一個(gè)即可
自定義403處理方案
使用 Spring Security 時(shí)經(jīng)常會(huì)看見 403(無權(quán)限)。Spring Security 支持自定義權(quán)限受限處理,需要
實(shí)現(xiàn) AccessDeniedHandler接口
基于方法的授權(quán)
1.JSR250注解,可以定義在方法和類上面
配置類開啟@EnableGlobalMethodSecurity(jsr250Enabled = true,securedEnabled = true,prePostEnabled = true)
@RolesAllowed 訪問對(duì)應(yīng)方法時(shí)所應(yīng)該具有的角色
@PermitAll 不進(jìn)行權(quán)限控制
@DenyAll 無論什么角色都不能訪問
2.@Secured注解專門用于判斷是否具有角色的,能寫在方法或類上。參數(shù)要以 ROLE_開頭
配置類開啟@EnableGlobalMethodSecurity(jsr250Enabled = true,securedEnabled = true,prePostEnabled = true)
3.支持表達(dá)式注解
配置類開啟@EnableGlobalMethodSecurity(jsr250Enabled = true,securedEnabled = true,prePostEnabled = true)
使用@PreAuthorize和@PostAuthorize 在方法之前,之后進(jìn)行訪問控制






授權(quán)原理:FilterSecurityInterceptor -> doFilter方法 -> invoke-> beforeInvocation -> attemptAuthorization -> decide(AffirmativeBased) -> vote 投票是否通過授權(quán)通過則可以訪問資源,失敗則跳轉(zhuǎn)到登錄頁面認(rèn)證
到此這篇關(guān)于spring security登錄認(rèn)證授權(quán)的項(xiàng)目實(shí)踐的文章就介紹到這了,更多相關(guān)spring security登錄認(rèn)證授權(quán)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Spring Security 自定義短信登錄認(rèn)證的實(shí)現(xiàn)
- Springboot+Spring Security實(shí)現(xiàn)前后端分離登錄認(rèn)證及權(quán)限控制的示例代碼
- Java SpringSecurity+JWT實(shí)現(xiàn)登錄認(rèn)證
- SpringBoot security安全認(rèn)證登錄的實(shí)現(xiàn)方法
- SpringSecurity實(shí)現(xiàn)前后端分離登錄token認(rèn)證詳解
- Springboot整合SpringSecurity實(shí)現(xiàn)登錄認(rèn)證和鑒權(quán)全過程
- springsecurity實(shí)現(xiàn)用戶登錄認(rèn)證快速使用示例代碼(前后端分離項(xiàng)目)
- Spring Security實(shí)現(xiàn)登錄認(rèn)證實(shí)戰(zhàn)教程
- SpringSecurity 自定義認(rèn)證登錄的項(xiàng)目實(shí)踐
相關(guān)文章
最新Spring Security實(shí)戰(zhàn)教程之Spring Security安全框架指南
SpringSecurity是Spring生態(tài)系統(tǒng)中的核心組件,提供認(rèn)證、授權(quán)和防護(hù)機(jī)制,以保護(hù)應(yīng)用免受各種安全威脅,它支持多種認(rèn)證方式,并通過攔截器和過濾器鏈進(jìn)行安全檢查,本文通過搭建SpringBoot+SpringSecurity項(xiàng)目,幫助如何快速上手并應(yīng)用SpringSecurity,感興趣的朋友一起看看吧2025-03-03
SpringBoot實(shí)現(xiàn)文件下載的限速功能
在SpringBoot項(xiàng)目中,實(shí)現(xiàn)文件下載的限速功能可以有效控制服務(wù)器帶寬的占用,并防止單個(gè)用戶消耗過多的資源,本文將通過具體的代碼示例和詳細(xì)的流程解釋,介紹如何在SpringBoot項(xiàng)目中實(shí)現(xiàn)文件下載的限速功能,需要的朋友可以參考下2024-07-07
SpringBoot中JSON數(shù)值溢出問題的處理方案
SpringBoot接口因JSON數(shù)值超出int范圍報(bào)錯(cuò),解決方案為:將字段改為L(zhǎng)ong、全局異常處理返回友好提示、添加數(shù)據(jù)校驗(yàn),推薦使用Long存儲(chǔ)大數(shù)值,避免溢出,本文通過代碼講解的非常詳細(xì),需要的朋友可以參考下2025-08-08
springboot整合mybatis-plus基于注解實(shí)現(xiàn)一對(duì)一(一對(duì)多)查詢功能
這篇文章主要介紹了springboot整合mybatis-plus基于純注解實(shí)現(xiàn)一對(duì)一(一對(duì)多)查詢功能,因?yàn)楸救瞬捎玫氖莝pring-boot進(jìn)行開發(fā),本身springboot就提倡采用不用配置自動(dòng)配置的方式,所以真心希望mybatis(不是mybatis-plus)這點(diǎn)需要繼續(xù)努力2021-09-09

