欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring security登錄認(rèn)證授權(quán)的項(xiàng)目實(shí)踐

 更新時(shí)間:2025年01月20日 11:31:23   作者:q琦一  
SpringSecurity主要實(shí)現(xiàn)了認(rèn)證(Authentication)和授權(quán)(AccessControl),本文就來介紹一下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)登錄頁(yè)面

<!-- 實(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ù)庫(kù)讀取密碼

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

在這里插入圖片描述

在這里插入圖片描述

認(rèn)證邏輯

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

自定義認(rèn)證成功失敗的處理

在這里插入圖片描述

](https://img-blog.csdnimg.cn/direct/7a9f49e75ae34c599bed5ed3ff7298c3.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倉(cāng)庫(kù)的過濾器 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ù)庫(kù)值就ok
原理:RememberMeAuthenticationFilter -> doFilter方法 -> autoLogin ->AbstractRememberMeServices的autoLogin ->PersistentTokenBasedRememberMeServices 的processAutoLoginCookie 取出remember-me的值拆分為series和token 去數(shù)據(jù)庫(kù)根據(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 重定向到登錄頁(yè)面

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)求頁(yè)面時(shí)埋入頁(yè)面)的內(nèi)容,如果token和服務(wù)端的token匹配成功,則正常訪問。
原理:CsrfFilter -> doFilter方法 -> 如果沒有token 先生成一個(gè),然后放到填充到CSRF_TOKEN中返回給頁(yè)面,會(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)到登錄頁(yè)面認(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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論