• shiro之 散列算法(加密算法)


    1. 在身份认证过程中往往会涉及加密。如果不加密那么数据信息不安全。Shiro内容实现比较多的散列算法。如:MD5,SHA等。并且提供了加盐功能。比如“1111”的MD5码为:“b59c67bf196a4758191e42f76670ceba”,这个MD5码可以很多破解网站上找到对应的原密码。但是如果为“111”+姓名 那么能找到原密码的难度会增加。

    2. 测试MD5案例:

    public static void main(String[] args) {
            //使用md5加密算法 加密
            Md5Hash md5 = new Md5Hash("1111");
            System.out.println("1111=="+md5.toString());
            //加 盐
            md5 = new Md5Hash("1111", "wh");
            System.out.println("1111=="+md5.toString());
            //迭代次数
            md5 = new Md5Hash("1111", "wh", 2);
            System.out.println("1111=="+md5.toString());
            SimpleHash hash = new SimpleHash("md5", "1111", "wh", 2);
            System.out.println(hash.toString());
        }

    3. 在自定义的Reaml中使用散列算法:

        Realm的实现:

    public class UserRealm extends AuthorizingRealm{
        @Override
        public String getName() {
            return "userRealm";
        }
        
        //完成身份认证(从数据库中取数据)并且返回认证信息
        //如果身份认证失败 返回null
        @Override
        protected AuthenticationInfo doGetAuthenticationInfo(
                AuthenticationToken token) throws AuthenticationException {
            //获取用户输入的用户名
            String username = (String)token.getPrincipal();//获取身份信息
            System.out.println("username====="+username);
            //根据用户名到数据库查询密码信息---模拟
            //假定从数据库获取的密码为1111和盐 值(颜值)
            String pwd = "e41cd85110c7533e3f93b729b25235c3";
            String salt ="sxt";
            //将从数据库中查询的信息封装到SimpleAuthenticationInfo中
            SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username,pwd,ByteSource.Util.bytes(salt),getName());
            return info;
        }
        //授权的信息
        @Override
        protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
            return null;
        }
    }

    配置文件:

    [main]
    credentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher
    credentialsMatcher.hashAlgorithmName=md5
    credentialsMatcher.hashIterations=2
    userRealm=cn.wh.realm.UserRealm
    userRealm.credentialsMatcher=$credentialsMatcher
    securityManager.realm=$userRealm
  • 相关阅读:
    文件上传Web小案例
    加密方法(MD5加密)
    解决中文乱码(不可能解决不了)
    jquery的一些常见使用方法
    Ajax的作用
    日期时间格式的转换
    前端点击复制内容
    uniapp 移动端防止点击事件穿透
    getCurrentPages 获取当前网页完整的URL
    关闭微信浏览器网页
  • 原文地址:https://www.cnblogs.com/forever2h/p/6856253.html
Copyright © 2020-2023  润新知