zoukankan      html  css  js  c++  java
  • 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进行散列对比时和原始数据库中的散列值使用的算法 一致。

  • 相关阅读:
    Java静态方法 与 非静态方法(实例方法)的区别
    java实现多态 方法的重写和重载的区别
    Linxu系统dpkg命令
    2016/5/23 阴天
    C#网络应用编程 类,构造函数,方法,属性和字段
    网络应用编程中的 ref
    编程经验(C#)
    Unity常用API备忘录
    Unity快捷键总结
    Unity ScriptObject创建Asset文件
  • 原文地址:https://www.cnblogs.com/qlqwjy/p/7257543.html
Copyright © 2011-2022 走看看