温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

spring cloud使用oauth2问题怎么解决

发布时间:2022-09-05 11:29:20 来源:亿速云 阅读:124 作者:iii 栏目:开发技术

本篇内容主要讲解“spring cloud使用oauth2问题怎么解决”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“spring cloud使用oauth2问题怎么解决”吧!

OAth3是一个标准的授权协议。

在认证与授权的过程中,主要包含以下3种角色。

服务提供方 Authorization Server。
资源持有者 Resource Server。
客户端 Client。

下面重点介绍下spring cloud 使用oauth3问题,内容如下所示:

1、spring boot 集成oauth3,带了token却访问时各种禁止访问,追踪代码过滤器发现变为匿名用户导致无法访问授权资源,添加过滤器各种都没效果,甚至添加了过滤器登录都登录不了了,

添加的依赖为

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <!--oauth3依赖-->
        <dependency>
            <groupId>org.springframework.security.oauth</groupId>
            <artifactId>spring-security-oauth3</artifactId>
            <version>2.3.3.RELEASE</version>
        </dependency>

将其直接改为spring-cloud-starter-oauth3 依赖,问题解决

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-oauth3</artifactId>
            <version>2.2.5.RELEASE</version>
        </dependency>

2、spring cloud oauth3 使用自定义 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加载了,导致无法匹配,

通过查资料等最终确定问题是  spring-boot-devtools 这个依赖引起的

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

问题分析
分析出ClassLoader不同导致的类型转换异常,Spring的dev-tools为了实现重新装载class自己实现了一个类加载器,来加载项目中会改变的类,方便重启时将新改动的内容更新进来,其实其中官方文档中是有做说明的:

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目录下面创建META_INF文件夹,然后创建spring-devtools.properties文件,文件加上类似下面的配置:

	restart.exclude.companycommonlibs=/mycorp-common-[\w-]+.jar
    restart.include.projectcommon=/mycorp-myproj-[\w-]+.jar

到此,相信大家对“spring cloud使用oauth2问题怎么解决”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI