zoukankan      html  css  js  c++  java
  • 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;
    }


    调整之后可以正常进入授权啦~~
     
  • 相关阅读:
    IIS Express 配置缓存位置
    Docker Demo on Docker
    前端的哪些坑
    如何在container中编译dotnet的eShopOnContainers
    JQuery 常用的那些东西
    jQuery选择器大全
    Js 跨域CORS报错 Response for preflight has invalid HTTP status code 405
    WPF 通过透明度遮罩和变换制作倒影效果
    Ons 让人欲哭无泪问题,官方介绍不详
    如何转换任何配置文件 文件中的内容
  • 原文地址:https://www.cnblogs.com/youran-he/p/10514661.html
Copyright © 2011-2022 走看看