1. 程式人生 > >spring boot shiro redis整合principals.getPrimaryPrincipal()強制轉換型別錯誤

spring boot shiro redis整合principals.getPrimaryPrincipal()強制轉換型別錯誤

 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());