shiroWeb项目-认证及MD5认证信息在页面显示(十)

realm设置完整认证信息

// realm的认证方法,从数据库查询用户信息
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        // token是用户输入的用户名和密码
        // 第一步从token中取出用户名
        String userCode = (String) token.getPrincipal();

        // 第二步:根据用户输入的userCode从数据库查询
        SysUser sysUser = null;
        try {
            sysUser = sysService.findSysUserByUserCode(userCode);
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        // 如果查询不到返回null
        if (sysUser == null) {//
            return null;
        }
        // 从数据库查询到密码
        String password = sysUser.getPassword();

        //
        String salt = sysUser.getSalt();

        // 如果查询到返回认证信息AuthenticationInfo

        // activeUser就是用户身份信息
        ActiveUser activeUser = new ActiveUser();

        activeUser.setUserid(sysUser.getId());
        activeUser.setUsercode(sysUser.getUsercode());
        activeUser.setUsername(sysUser.getUsername());
        // ..

        // 根据用户id取出菜单
        List<SysPermission> menus = null;
        try {
            // 通过service取出菜单
            menus = sysService.findMenuListByUserId(sysUser.getId());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 将用户菜单 设置到activeUser
        activeUser.setMenus(menus);

        // 将activeUser设置simpleAuthenticationInfo
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(activeUser, password,
                ByteSource.Util.bytes(salt), this.getName());

        return simpleAuthenticationInfo;
    }

Action中显示认证信息

@RequestMapping("/first.action")
        public String first(Model model)throws Exception{
            
            //从shiro的session中取activeUser
            Subject subject = SecurityUtils.getSubject();
            //取身份信息
            ActiveUser activeUser = (ActiveUser) subject.getPrincipal();
            //通过model传到页面
            model.addAttribute("activeUser", activeUser);
            
            return "/first";
        }

页面中取出认证信息

--------------------------------------------------------MD5认证-------------------------------------

数据库中存储到的md5的散列值,在realm中需要设置数据库中的散列值它使用散列算法 及散列次数,让shiro进行散列对比时和原始数据库中的散列值使用的算法 一致。

原文地址:https://www.cnblogs.com/qlqwjy/p/7257543.html