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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值