spring boot + shiro 热部署 取session 问题

 
  

开发环境可以这么用  正式环境注释就可以了

/**
 * 获取session中的对象
 *
 * @return
 */
public static SysUser getSubject() {
    Session session = SecurityUtils.getSubject().getSession(false);
    SysUser sysUser = new SysUser();
    try {
        sysUser = (SysUser) session.getAttribute(Constant.LOGONUSER);
    } catch (ClassCastException ex) {
        //用于redis session 使用了 spring devtools 导致的类型转换异常
        sysUser = Global.convertObjToEntity(session.getAttribute(Constant.LOGONUSER));
    }
    return sysUser;
}

private static SysUser convertObjToEntity(Object redisObj) {
    SysUser sysUser = new SysUser();
    sysUser.setId(NumberUtils.toLong(ReflectUtils.getFieldValue(redisObj, SysUser.ID)+"",0));
    sysUser.setUsername(ReflectUtils.getFieldValue(redisObj, SysUser.USERNAME)+"");
    sysUser.setPassword(ReflectUtils.getFieldValue(redisObj, SysUser.PASSWORD)+"");
    sysUser.setDepartmentId(NumberUtils.toLong(ReflectUtils.getFieldValue(redisObj, SysUser.DEPARTMENTID)+"",0));
    sysUser.setFlag(NumberUtils.toInt(ReflectUtils.getFieldValue(redisObj, SysUser.FLAG)+"",0));
    sysUser.setPhone(ReflectUtils.getFieldValue(redisObj, SysUser.PHONE)+"");
    sysUser.setPhoto(ReflectUtils.getFieldValue(redisObj, SysUser.PHOTO)+"");
    sysUser.setRealName(ReflectUtils.getFieldValue(redisObj, SysUser.REALNAME)+"");
    sysUser.setSalt(ReflectUtils.getFieldValue(redisObj, SysUser.SALT)+"");
    sysUser.setSeeDepartments(ReflectUtils.getFieldValue(redisObj, SysUser.SEEDEPARTMENTS)+"");
    sysUser.setSessionId(ReflectUtils.getFieldValue(redisObj, SysUser.SESSIONID)+"");
    Object loginDateObj = ReflectUtils.getFieldValue(redisObj, SysUser.LASTLOGINDATE);
    sysUser.setLastLoginDate(loginDateObj != null ? (Date) loginDateObj : null);
    sysUser.setLastLoginIp(ReflectUtils.getFieldValue(redisObj, SysUser.LASTLOGINIP)+"");
    return sysUser;

}
 
  

工具类就不贴了  网上有很多  自行查找就可以了  找不到给我留言

你可能感兴趣的:(spring,boot)