• 自定义shiro的验证方式


    在认证、授权内部实现机制中,最终处理都将交给Real进行处理。因为在Shiro中,最终是通过Realm来获取应用程序中的用户、角色及权限信息的。

    在应用程序中要做的是自定义一个Realm类,继承AuthorizingRealm抽象类,重载doGetAuthenticationInfo (),重写获取用户信息的方法。而授权实现则与认证实现非常相似,在我们自定义的Realm中,重载doGetAuthorizationInfo()方法,重写获取用户权限的方法。

    public class ShiroDbRealm extends AuthorizingRealm {
    
        /**
         * 登录认证/获取用户信息
         */
        @Override
        protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)
                throws AuthenticationException {
            IShiro shiroFactory = ShiroFactroy.me();
            UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
            //根据前端传的用户,查找数据库用户的记录封装在user里
            User user = shiroFactory.user(token.getUsername());
            ShiroUser shiroUser = shiroFactory.shiroUser(user);
            SimpleAuthenticationInfo info = shiroFactory.info(shiroUser, user, super.getName());
            return info;
        }
    
        /**
         * 权限认证/获取用户权限
         */
        @Override
        protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
            IShiro shiroFactory = ShiroFactroy.me();
            ShiroUser shiroUser = (ShiroUser) principals.getPrimaryPrincipal();
            List<Integer> roleList = shiroUser.getRoleList();
    
            Set<String> permissionSet = new HashSet<>();
            Set<String> roleNameSet = new HashSet<>();
    
            for (Integer roleId : roleList) {
                List<String> permissions = shiroFactory.findPermissionsByRoleId(roleId);
                if (permissions != null) {
                    for (String permission : permissions) {
                        if (ToolUtil.isNotEmpty(permission)) {
                            permissionSet.add(permission);
                        }
                    }
                }
                String roleName = shiroFactory.findRoleNameByRoleId(roleId);
                roleNameSet.add(roleName);
            }
    
            SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
            info.addStringPermissions(permissionSet);
            info.addRoles(roleNameSet);
            return info;
        }
    
        /**
         * 设置认证加密方式
         */
        @Override
        public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) {
            super.setCredentialsMatcher(new CustomCredentialsMatcher());
        }
    }

    自定义密码认证方式

    自定义实现类继承SimpleCredentialsMatcher,重载doCredentialsMatch方法,自定义验证方式

    public class CustomCredentialsMatcher extends SimpleCredentialsMatcher {
        @Override
        public boolean doCredentialsMatch(AuthenticationToken authcToken, AuthenticationInfo info) {
    
            UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
            Object tokenCredentials = encrypt(String.valueOf(token.getPassword()));
            //System.err.println("encryptionPw:"+encrypt(String.valueOf(token.getPassword())));
            Object accountCredentials = getCredentials(info);
            //将密码加密与系统加密后的密码校验,内容一致就返回true,不一致就返回false
            return equals(tokenCredentials, accountCredentials);
        }
    
        //密码加密方法
        private String encrypt(String data) {
            String encryptionPw = ShiroKit.md5(data);
            return encryptionPw;
        }
    }

    最后在自定义的Realm类中设置。

    /**
         * 设置认证加密方式
         */
        @Override
        public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) {
            super.setCredentialsMatcher(new CustomCredentialsMatcher());
        }
  • 相关阅读:
    android如何在代码中设置margin
    也许游戏 它P/N图分析
    【淡墨Unity3D Shader计划】四 热带雨林的文章: 排除、深度测试、Alpha测试和基本雾编译
    HDU 3060 多边形面积并
    onmouseover和onmouseout的烦恼
    LoaderManager使用具体解释(三)---实现Loaders
    [每天一个Linux小技巧] gdb 下一次运行多个命令
    VB.NET版机房收费系统—DataGridView应用
    数据结构导论第一遍
    轻松搞定面试中的二叉树题目
  • 原文地址:https://www.cnblogs.com/xiaowangxiao/p/11216716.html
Copyright © 2020-2023  润新知