spring boot项目使用ShiroUser shiroUser=(ShiroUser)principals.getPrimaryPrincipal() 报错
错误:java.lang.ClassCastException:com.zyc.springboot.shiro.ShiroUser cannot be cast to com.zyc.springboot.shiro.ShiroUser
原因不太清楚,解决办法暂时发现3种:
1:有可能是spring boot 热部署造成的,去掉spring boot 热部署试一试。
2:使用org.apache.commons.beanutils.BeanUtils类进去属性转换,其实就是相当于把数据反射出来
Object key = principals.getPrimaryPrincipal();
ShiroUser shiroUser=new ShiroUser();
try {
BeanUtils.copyProperties(shiroUser, key);
} catch (Exception e) {
}
3:把当前用户的登录名信息保存进去,而不是保存一个具体的对象。
原来的代码
SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(
shiroUser, user.getPassword(), this.getName());
新代码
SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(
userName, user.getPassword(), this.getName());