spring?cloud?使用oauth2?問題匯總
OAth2是一個標(biāo)準(zhǔn)的授權(quán)協(xié)議。
在認(rèn)證與授權(quán)的過程中,主要包含以下3種角色。
服務(wù)提供方 Authorization Server。
資源持有者 Resource Server。
客戶端 Client。
下面重點介紹下spring cloud 使用oauth2問題,內(nèi)容如下所示:
1、spring boot 集成oauth2,帶了token卻訪問時各種禁止訪問,追蹤代碼過濾器發(fā)現(xiàn)變?yōu)槟涿脩魧?dǎo)致無法訪問授權(quán)資源,添加過濾器各種都沒效果,甚至添加了過濾器登錄都登錄不了了,
添加的依賴為
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <!--oauth2依賴--> <dependency> <groupId>org.springframework.security.oauth</groupId> <artifactId>spring-security-oauth2</artifactId> <version>2.3.3.RELEASE</version> </dependency>
將其直接改為spring-cloud-starter-oauth2 依賴,問題解決
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-oauth2</artifactId> <version>2.2.5.RELEASE</version> </dependency>
2、spring cloud oauth2 使用自定義 UserDetails 后,通過
authentication.getPrincipal() instanceof OpenUserDetails
獲取用戶信息時,老是報類型匹配失敗
(OpenUserDetails) authentication.getPrincipal() 使用這句時直接報錯
java.lang.ClassCastException: com.kou.auth.OpenUserDetails cannot be cast to com.kou.auth.OpenUserDetails
通過classloader看,同一個類被不同的classloader加載了,導(dǎo)致無法匹配,
通過查資料等最終確定問題是 spring-boot-devtools 這個依賴引起的
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency>
問題分析
分析出ClassLoader不同導(dǎo)致的類型轉(zhuǎn)換異常,Spring的dev-tools為了實現(xiàn)重新裝載class自己實現(xiàn)了一個類加載器,來加載項目中會改變的類,方便重啟時將新改動的內(nèi)容更新進(jìn)來,其實其中官方文檔中是有做說明的:
By default, any open project in your IDE will be loaded using the “restart” classloader, and any regular .jar file will be loaded using the “base” classloader. If you work on a multi-module project, and not each module is imported into your IDE, you may need to customize things. To do this you can create a
META-INF/spring-devtools.properties file. The spring-devtools.properties file can contain restart.exclude. and restart.include. prefixed properties. The include elements are items that should be pulled up into the “restart” classloader, and the exclude elements are items that should be pushed down into the “base”
classloader. The value of the property is a regex pattern that will be applied to the classpath.
處理方法,將其刪掉
或者
在resources目錄下面創(chuàng)建META_INF文件夾,然后創(chuàng)建spring-devtools.properties文件,文件加上類似下面的配置:
restart.exclude.companycommonlibs=/mycorp-common-[\w-]+.jar restart.include.projectcommon=/mycorp-myproj-[\w-]+.jar
到此這篇關(guān)于spring cloud 使用oauth2 問題匯總的文章就介紹到這了,更多相關(guān)spring cloud 使用oauth2內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
postman中POST請求時參數(shù)包含參數(shù)list設(shè)置方式
這篇文章主要介紹了postman中POST請求時參數(shù)包含參數(shù)list設(shè)置方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05使用JavaIO流和網(wǎng)絡(luò)制作一個簡單的圖片爬蟲
這篇文章主要介紹了使用JavaIO流和網(wǎng)絡(luò)制作一個簡單的圖片爬蟲,通過關(guān)鍵字爬取百度圖片,這個和我們使用搜索引擎搜索百度圖片是一樣的,只是通過爬蟲可以學(xué)習(xí)技術(shù)的使用,需要的朋友可以參考下2023-04-04springboot如何使用@ConfigurationProperties封裝配置文件
springboot如何使用@ConfigurationProperties封裝配置文件的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08基于java springboot + mybatis實現(xiàn)電影售票管理系統(tǒng)
這篇文章主要介紹了基于java springboot + mybatis實現(xiàn)的完整電影售票管理系統(tǒng)基于java springboot + mybatis,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08