【SpringBoot】 整合 Shiro 过程中遇到奇怪的类型转换问题

SpringBoot3集成Shiro常见问题与解决方案 在Java安全框架领域,Apache Shiro作为轻量级解决方案被广泛应用。其核心原理基于Subject、SecurityManager和Realm的三层架构,通过过滤器链实现认证授权。随着SpringBoot3的重大升级,特别是从J2EE转向Jakarta EE的命名空间变更,Shiro集成面临诸多技术挑战。本文针对Redis会话管理、注解失效等典型问题,提供经过生产验证的解决方案。通过正确配置依赖版本、调整过滤器注册方式、优化序列化方案等工程实践,可有效解决SpringBoot3环境下Shiro集成时 阅读详情

遇到问题

  最近在忙着重新给公司的系统搭建框架,用了 SpringBoot,整合了 Shrio 来做用户的认证和授权,也引入了Mybatis 的通用插件等等。
  起初并没有遇到什么困难,用 Shiro 的 session 管理来实现,使用的 sessionDao 层实现主要用的还是 RedisSessionDAO。登录认证一切OK。但是当我去修改缓存时候,需要从 session 当中将对象的属性取出来的时候(此时为 Object 类型),再转成对应的类型就发生了类型转换异常(不是同一类型)。
  上几张图说明我的问题。
  这里写图片描述
  这里的这个 objValue 拿到的实际上也是 session 中缓存的用户的信息。
  这里写图片描述
  从上两幅图我们可以看到,这两个是属于同一类型的。
  这里写图片描述
  但是实际上在 Debug 调试 objValue instanceof User 这段代码时候,其结果却是 false ,出乎了我的意料。
  然后我去掉 objValue instanceof User 这段代码,让程序进入这个判断执行语句 User user = (User) objValue 就出现了以下错误。
  这里写图片描述
 这里写图片描述
 我到这个异常的时候,我脑海中第一个想的就是:我的亲爸爸难道不是我亲爸爸?
 

问题原因

  尝试了一系列的测试都无果后,原本已经打算放弃了。想着从 shiro session 中取出来的对象实际上都是经过 redis 的反序列化之后取出来的,就顺着思考会不会是类加载的问题。
  查了一下之后,发现我项目启动时候加载项目当中的类所使用到的加载器是 org.springframework.boot.devtools.restart.classloader.RestartClassLoader , 这是因为之前在项目当中引入了 spring-boot-devtools 这个热部署包来提高效率。而我从 shiro session 取对象时候所用到的类加载器并不是这个,而是 sun.misc.Launcher.AppClassLoader ,从而导致我的类型的转换的异常。

解决方案

  1. 不使用 spring-boot-devtools 热部署
  2.在 resources 目录下面创建 META_INF 文件夹,然后创建 spring-devtools.properties文件,文件加上类似下面的配置:

restart.exclude.companycommonlibs=/mycorp-common-[\\w-]+\.jar
restart.include.projectcommon=/mycorp-myproj-[\\w-]+\.jar
(springboot)shiro安全框架自定义过滤器出现的几个疑难杂症解决方案 问题一:多次重复重定向问题(匹配多个过滤器链重复调用其对应过滤器) 问题二:shiro认证时Realm会执行两次 在使用springboot框架整合shiro安全认证框架时踩了很多坑,每次出问题网上都找不到其中的解决方案,这里贴两个我遇到的坑,以及其解决方案给大家,希望大家可以少走弯路。 问题一场景: // 自定义拦截器 Map<String, Filter> cus... 阅读详情

相关推荐

spring boot实战之集成spring-boot-devtools

1.简介 Spring Boot包括一组额外的工具,这些工具可以使应用程序开发体验更加愉快。 spring-boot-devtools模块可以包含在任何项目中,以提供其他开发时功能。 要包括devtools支持,请将模块依赖项添加到您的构建中 <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boo

蛐蛐的博客 1277

SpringBoot入门 - 配置热部署devtools工具

热部署和热加载是在应用正在运行的时候,自动更新(重新加载或者替换class等)应用的一种能力。(PS:spring-boot-devtools提供的方案也是要重启的,只是无需手动重启能实现自动加载而已。严格意义上,我们需要区分下热部署和热加载, 对于Java项目而言:热部署在服务器运行时重新部署项目它是直接重新加载整个应用,这种方式会释放内存,比热加载更加干净彻底,但同时也更费时间。热加载在运行时重新加载class,从而升级应用。热加载的实现原理主要依赖java的类加载机制。

DeveloperFire的博客 4457

C# : 计算并输出这一天是这一年的第几天

C#之输入某年,然后输入某月,再输入某日,计算并输出这一天是这一年的第几天的功能算法

saitoDeng的博客 973

Spring DevTools 介绍

Spring DevTools 介绍Spring Boot包括一组额外的工具,可以使应用程序开发体验更加愉快。 spring-boot-devtools模块可以包含在任何项目中,它可以节省大量的时间。 想要使用devtools支持,只需将模块依赖关系添加到你的构建中:Maven.<dependencies> <dependency> <groupId>org.springfr

偶尔记一下 - mybatis.io 7万+

旋转变压器软件解码simulink仿真

旋转变压器软件解码simulink仿真、电机转速位置测量

qq_31446727的博客 2584

AWS S3文件上传性能优化:Java SDK多线程与分段上传实战指南

本文详细介绍了如何通过Java SDK优化AWS S3文件上传性能,重点讲解多线程并发上传与分段上传(Multipart Upload)技术的实战应用。通过动态调整分段策略、线程池配置及内存优化技巧,可显著提升大文件上传效率,实测显示组合方案比单线程上传提速近5倍。文章还涵盖加密上传、断点续传等高级场景解决方案,并提供了完整的性能对比测试数据。

d6e7f8的博客 1017

Spring Boot - 实用功能01 - 配置热部署devtools工具

配置热部署devtools工具

qq_43350524的博客 3053

基于激光雷达的目标跟随功能移植到自己的ROS机器人小车上

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录前言一、下载目标跟随的源码二、修改话题名称1.修改/cmd_vel话题名称2.修改/scan话题名称3.修改双电机正反转4.目标跟随功能测试 前言 本人尝试把github上的一个目标跟随项目移植到自己的机器人小车上,并且成功了,最终小车可以跟着人移动,在这里分享一下心得。基于激光雷达的目标跟随,通过获取激光雷达的信息,跟随最近的物体。 提示:以下是本篇文章正文内容,下面案例可供参考 一、下载目标跟随的源码 源码地址:https:

weixin_44574633的博客 4463

Spring Boot DevTools开发者工具

正如前面在Restart vs Reload所描述的,重启功能时通过使用两个classloader实现的。如果这导致了其他的问题,你可能徐娅自定义类加载器。默认情况下,你的IDE中任何打开的项目都是用“restart”类记载器加载的,而任何常规的.jar 文件都是用 base 类加载器加载。如果你使用【mvn spring-boot:run】或【gradle bootRun】也是一样,包含了@SpringBootApplication的项目用 restart 类加载器加载,其他都用 base 类加载器。

baidu_38945557的博客 2612

RTDETR多模态融合实战:基于注意力机制的红外与可见光目标检测优化

本文深入探讨了基于RTDETR模型的红外与可见光多模态目标检测优化方案。针对复杂光照下的检测难题,文章提出并实战演示了如何利用注意力机制(如SimAM与交叉注意力)实现RGB与IR特征的智能动态融合,从而构建更鲁棒的“全天候”检测系统,有效提升安防、自动驾驶等场景在夜间及恶劣天气下的检测精度与可靠性。

weixin_29083373的博客 190

高频功率放大器的设计实现

摘 要 随着无线通信技术的高速发展,市场对射频电路的需求越来越大,同时对射频电路的性能要求也越来越高。高频功率放大器是位于无线发射级末端的重要部件,为了弥补信号在无线传输过程中的衰耗,要求发射机具有较大功率的输出,通信距离越远,要求的输出功率越大。在高频范围内,为了获得足够大的高频输出功率,就要采用高频功率放大器。 本次课程设计的高频功率放大器的技术指标包括输出功率、效率、功率增益、由于高频频功率放大器的输出功率比较大,耗能比较多,所以工作效率就显得比较重要。本课程设计的高频功率放大器电路由两极功率放大器

qq_43543515的博客 3万+

SpringBoot 整合 Shiro 过程中遇到奇怪的类型转换问题

  起初并没有遇到什么困难,用Shiro的session管理来实现,使用的sessionDao层实现主要用的还是RedisSessionDAO。登录认证一切OK。但是当我去修改缓存时候,需要从session当中将对象的属性取出来的时候(此时为Object类型),再转成对应的类型就发生了类型转换异常(不是同一类型)。  上几张图说明我的问题。    这里的这个objValue拿到的实际上也...

weixin_30699463的博客 122

【WRF-FDDA】观测 Obs nudging 的配置与使用说明(中)

WRF-FDDA

WW、forever的博客 282

Shiro系列(一)——Shiro + Springboot + JWT 整合

写在前面 本文的出现表示不再进行 Spring Security Oauth 实现的研究了,原因是原开源项目已经被废弃了不再更新了,而且 Oauth 实现的内容有些奇怪,新的项目 spring-authorization-server 目前才发布到 0.1.0,默认只提供了基于内存的实现,个人认为还不是很完善,不适合用到项目中。而且 Spring Security 的 Oauth 流程都实现了,要修改还得从新研究 spring-authorization-server 的实现逻辑,然后进行修改定制,太耗费精

玖涯博客 1647

PyTorch实战(21)——扩散模型(Diffusion Model)

扩散模型通过简单的噪声扩散过程,就能创造出高质量的超现实图像。在本节中,我们首先学习了扩散如何用于图像生成,了解了扩散模型的内部运作机制。随后基于动漫图像数据集,使用 PyTorch 和 Hugging Face 框架训练和运行自定义扩散模型,生成逼真动漫图像。

盼小辉丶的博客 3425

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

springboot 整合 shiro

踩的坑太多了,最大的还是包的引用,shiro给springboot专门有一个包,但是不好用,当全部自动化生成的时候,有些东西会奇奇怪怪的,所以还是自己用shiro-spring 进行一遍配置比较好,而且使用下面的这个包,可以使用thymeleaf-shiro,方便权限管理代码的写法。 <!--<dependency>--> &lt...

g123321dad的博客 4657

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

SpringBoot(38) —— SpringBoot整合Shiro环境搭建

目录1.环境搭建2.ShiroConfig3.创建用于测试的页面4.小结 1.环境搭建 创建一个SpringBoot项目,就作为父项目springboot-07-shiro的一个子模块创建即可 导入thymeleaf依赖<!--thymeleaf依赖--> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifa

Jzandth的博客 277

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

SpringBoot + shiro + thymeleaf 整合

springBoot中shiro配置类 /** * 作者: Lipf * 项目名: fresh * 时间: 2019/11/25 16:23 * 描述: */ @Configuration public class ShiroConfiguration { /** * thymeleaf模板支持shiro方言的配置 * @return */ ...

lpf12的博客 245

手把手教你SpringBoot+MyBatis+Shiro 搭建项目框架

链接:https://www.cnblogs.com/deng-cc/p/9415279.html写在前面的话一直想能仿公司框架的形式,着手做一个简单的...

emprere的博客 706

shiro版本_手把手教你SpringBoot+MyBatis+Shiro 搭建项目框架

链接:https://www.cnblogs.com/deng-cc/p/9415279.html写在前面的话一直想能仿公司框架的形式,着手做一个简单的脚手架,一来是带着目标性能更好地学习,接触新的技术,另外自己如果有什么想要实现的简单需求,就可以进行快速开发,主要还是希望能在权限上有所控制,所以最花时间的还是在Shiro上。其实目标在github已经有不少大佬的参考物了:zheng(h...

weixin_39942785的博客 168

SpringBoot+MyBatis+Shiro 搭建杂谈

点击上方好好学java,选择星标公众号重磅资讯、干货,第一时间送达 今日推荐:用好Java中的枚举,真的没有那么简单!个人原创+1博客:点击前往,查看更多 链接:https:...

好好学java 365

springboot整合shrio配置

springboot整合shiro框架 1. 导入依赖 <!-- 导入shiro框架--> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring-boot-web-starter</artifactId> ...

le6peer的博客 353

shiro汇总

springboot整合shiro-登录认证和权限管理:  springboot+shiro+redis项目整合:   SpringBoot 整合 Shiro 过程中遇到奇怪的类型转换问题:  在前后端分离的SpringBoot项目中集成Shiro权限框架: 后台使用shiro框架时,怎样使用它的会话管理系统(session),从而实现权限控制: ...

夕时明月 368

springboot-mybatisPlus-Shiro集成

详细小白教程,说明了从数据库建表开始,到最后验证shiro安全框架的全部代码,拿到就可直接用

weixin_47323161的博客 764

springBoot添加Shiro 事务失效

昨天遇到一个非常奇怪的问题,在一个Service中使用@Transactional注解的一个方法无论如何都不能开启事务。项目用的是Springboot和Mybatis Plus,权限验证用的是Shiro。Service层的伪代码如下: @Transactional(rollbackFor = Exception.class) public void register(String username, String password) { Member member = new Member();

bingoshy的博客 421
上一篇: 【SpringBoot】整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统实例
下一篇: 【SpringBoot】解决拦截器注入 Service 为空问题
黑豪Horace
博客等级 码龄10年 85粉丝 74原创
评论 4
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值