当前位置: 首页 > news >正文

spring cloud 使用oauth2 问题收集

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

添加的依赖为

        <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加载了,导致无法匹配,

通过查资料等最终确定问题是  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

相关文章:

  • vscode-pretter 插件支持 styled-components 格式化问题
  • 搭建微服务项目框架环境
  • 与MySQL的纠缠(卸载与安装)
  • nodejs+vue+elementui影城选座管理系统python518
  • 机器学习笔记之支持向量机(一)模型构建思路
  • python-(4-3)数据类型的应用(元组、集合)
  • Hbase基本概念
  • cmake是什么,为什么现在都用cmake,cmake编译原理和跨平台示例
  • IDEA中使用JIRA
  • 物联网-物联前端安全加密技术简介
  • 怎样在LaTeX中方便输入带圆圈的数字
  • 如何在 SAP ABAP ALV 报表里以交通灯的方式显示某一列的值
  • 【C++实现】浅聊定时器的实现,最小堆配合map实现定时器
  • Spring五大类注解读取存储Bean对象
  • 数据备份管理中的分类定级:方法、标准与策略
  • $translatePartialLoader加载失败及解决方式
  • .pyc 想到的一些问题
  • android 一些 utils
  • Computed property XXX was assigned to but it has no setter
  • CSS实用技巧
  • express.js的介绍及使用
  • iOS筛选菜单、分段选择器、导航栏、悬浮窗、转场动画、启动视频等源码
  • JavaScript异步流程控制的前世今生
  • js算法-归并排序(merge_sort)
  • mockjs让前端开发独立于后端
  • Python socket服务器端、客户端传送信息
  • Sass Day-01
  • WinRAR存在严重的安全漏洞影响5亿用户
  • 阿里云容器服务区块链解决方案全新升级 支持Hyperledger Fabric v1.1
  • 爬虫模拟登陆 SegmentFault
  • 使用Tinker来调试Laravel应用程序的数据以及使用Tinker一些总结
  • 微信小程序上拉加载:onReachBottom详解+设置触发距离
  • 小程序01:wepy框架整合iview webapp UI
  • 学习笔记TF060:图像语音结合,看图说话
  • python最赚钱的4个方向,你最心动的是哪个?
  • scrapy中间件源码分析及常用中间件大全
  • 容器镜像
  • ​HTTP与HTTPS:网络通信的安全卫士
  • ###C语言程序设计-----C语言学习(3)#
  • #pragam once 和 #ifndef 预编译头
  • (libusb) usb口自动刷新
  • (附源码)spring boot基于小程序酒店疫情系统 毕业设计 091931
  • (附源码)springboot青少年公共卫生教育平台 毕业设计 643214
  • (论文阅读30/100)Convolutional Pose Machines
  • (三) prometheus + grafana + alertmanager 配置Redis监控
  • (三分钟了解debug)SLAM研究方向-Debug总结
  • (十八)用JAVA编写MP3解码器——迷你播放器
  • (十五)devops持续集成开发——jenkins流水线构建策略配置及触发器的使用
  • (转) Face-Resources
  • (转)IOS中获取各种文件的目录路径的方法
  • (转)scrum常见工具列表
  • .NET 4.0网络开发入门之旅-- 我在“网” 中央(下)
  • .NET Core引入性能分析引导优化
  • .net操作Excel出错解决
  • .NET中的Exception处理(C#)