shiro無法進入授權的方法org.crazycake.shiro.exception.PrincipalInstanceException: class java.util.HashMap must has getter for field: id


rg.crazycake.shiro.exception.PrincipalInstanceException: class java.util.HashMap must has getter for field: id
We need a field to identify this Cache Object in Redis. So you need to defined an id field which you can get unique id to identify this principal. For example, if you use UserInfo as Principal class, the id field maybe userId, userName, email, etc. For example, getUserId(), getUserName(), getEmail(), etc.
Default value is "id", that means your principal object has a method called "getId()"

shiro與redis整合過程中出現以上的問題:根據錯誤的信息,是信息認證實體中需要id的getter信息。根據官方文檔配置相應的模塊:

cacheManager.principalIdFieldName = <your id field name of principal object>

在spring boot 中的非xml配置如下:

/**
* cacheManager 緩存 redis實現
* 使用的是shiro-redis開源插件
*
* @return
*/
public RedisCacheManager cacheManager() {
RedisCacheManager redisCacheManager = new RedisCacheManager();
redisCacheManager.setRedisManager(redisManager());
redisCacheManager.setPrincipalIdFieldName("id");
return redisCacheManager;
}
在shiro realm中修改認證的信息配置如下:
@Override
public AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
String useranme = (String) token.getPrincipal();
System.out.println("登錄方法:" + useranme);
User user = new User();
user = userService.getCredential(useranme);
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), ByteSource.Util.bytes(user.getSalt()), getName());
Session session = SecurityUtils.getSubject().getSession();
session.setAttribute("username", useranme);
return info;
}


 

調整之后可以正常進入授權啦~~
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM