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