Caused by: java.lang.ClassCastException: com.cfcc.bigdata.base.dto.auth.TsUser cannot be cast to com.cfcc.bigdata.base.dto.auth.TsUser
at com.cfcc.bigdata.base.shiro.ShiroSessionUtils.getLoginAccount(ShiroSessionUtils.java:67)
at com.cfcc.bigdata.auth.controller.LoginController.loginPost(LoginController.java:60)
... 83 common frames omitted
查看报错的位置,发现从Session 中或去的对象类型和转化类型相同
public static TsUser getLoginAccount() {
Object o = getAttribute(LOGIN_CURRENT_USER_KEY);
ClassLoader classLoader = o.getClass().getClassLoader();
String name = classLoader.toString();
System.out.println("name-->" + name);
String name01 = TsUser.class.getClassLoader().toString();
System.out.println("name01-->" + name01);
if (o != null) {
return (TsUser) o;
}
return null;
}
接着查看这两个类的加载器,是否相同,发现,会话中取的对像的类是JDK AppClassLoader 加载的,而不是devtool 的
RestartClassLoader 类加载器加载的。
name-->sun.misc.Launcher$AppClassLoader@18b4aac2name01-->org.springframework.boot.devtools.restart.classloader.RestartClassLoader@aa184c0
因此解决此问题的办法应该是,让Session 中的对象的类用RestartClassLoader来加载,因为Session 的管理是由第三方的Shiro框架做的,因此需要shiro的包由 RestartClassLoader 加载。
参考Springboot 官方文档:
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.
从中可以得出只需要在 META-INF/spring-devtools.properties 文件中配置需要加载的第三方jar包即可。
解决方案:
在resource下创建META-INF 文件夹,并创建spring-devtools.properties文件在里面添加需要由RestartClassLoader类加载器加载的jar文件
restart.include.shiro=/shiro-[\\w-\\.]+jar
注意:restart.include 是固定的,shiro 是自己定义的
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional> //表示此依赖不传递
</dependency>
</dependencies>
2. Settings --> 查找make project automatically --> 选中
3.CTRL + SHIFT + A --> 查找Registry --> 找到并勾选compiler.automake.allow.when.app.running
4.设置application.properties
spring.devtools.restart.enabled=true
5.Chrome禁用缓存
F12 --> NetWork --> Disable Cache(while DevTools is open)