MD5散列算法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_38087648/article/details/79993446

散列算法:

通常需要对密码进行散列,常用的有md5,sha
对md5密码,如果知道散列后的值可以通过穷举算法,得到md5密码对应的明文。建议对md5进行散列时加salt(盐),进行加密相当于对原始+盐进行散列

正常使用时散列方法:

在程序中对原始密码+盐进行散列,将散列值存储到数据库中,并且还要讲盐也要存储在数据库中,如果进行密码对比时,使用相同方法,将原始密码+盐进行散列,进行对比

散列方法测试

public static void main(String[] args) {
        //原始密码
        String source = "111111";
        //盐
        String salt = "wasd";
        //散列次数
        int hashIterations = 2;
        //散列一次:1b2814e7f4cbb32fea953252e45fface
        //散列两次:174aec66f457f5169dbb1922393b6b4c
        //构造方法中:
        //第一个参数:明文,原始密码
        //第二个参数:盐,通过使用随机数
        //第三个参数:散列的次数,比如散列两次,相当于md5(md5(''))
        Md5Hash md5Hash = new Md5Hash(source, salt, hashIterations);

        String password_md5 = md5Hash.toString();
        System.out.println(password_md5);

        //第一个参数:散列算法
        SimpleHash simpleHash = new SimpleHash("md5", source, salt, hashIterations);
        System.out.println(simpleHash.toString());
    }

ini配置文件整合和为读取数据库

[main]
#定义凭证匹配器
credentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher
#散列算法
credentialsMatcher.hashAlgorithmName=md5
#散列次数
credentialsMatcher.hashIterations=2

#将凭证匹配器设置到realm
customRealm=cn.dinggc.shiro.realm.CustomRealMd5
customRealm.credentialsMatcher=$credentialsMatcher
securityManager.realms=$customRealm

自定义realmmd5

public class CustomRealMd5 extends AuthorizingRealm{

    //设置realm的名称
    @Override
    public void setName(String name) {
        super.setName("customRealmMd5");
    }
    //用于认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        // TODO Auto-generated method stub
        //token是用户输入的
        //第一步从token中取出身份信息
        String userCode = (String)token.getPrincipal();
        //第二步:根据用户输入的userCode从数据库查询
        //...
        //模拟从数据库查询密码,散列值
        String password = "174aec66f457f5169dbb1922393b6b4c";
        //从数据库中获取salt
        String salit = "wasd";
        //如果查询不到返回null
        //数据库中用户账号是zhangsan
        /*if(!userCode.equals("zhangsansan")) {
            return null;
        }*/
        //如果查询到返回认证信息AuthenticationInfo

        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(userCode, password,ByteSource.Util.bytes(salit),this.getName());
        return simpleAuthenticationInfo;
    }

    //用于授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
        // TODO Auto-generated method stub
        return null;
    }
}

md5测试

// 用户登录和退出
                @Test
                public void testCustomRealmMd5() {
                    // 创建securityManager工厂,用过ini配置文件创建securityManager工厂
                    Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-realm-md5.ini");
                    // 创建SecurityManager
                    SecurityManager securityManager = factory.getInstance();
                    // 将securityManager设置到当前的运行环境中
                    SecurityUtils.setSecurityManager(securityManager);
                    // 从SecurityUtils里边创建一个subject
                    Subject subject = SecurityUtils.getSubject();
                    // 在认证提交前准备token(令牌)
                    UsernamePasswordToken token = new UsernamePasswordToken("zhangsan", "111111");
                    try {
                        // 执行认证提交
                        subject.login(token);
                    } catch (AuthenticationException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    //是否认证通过
                    boolean isAuthenticated = subject.isAuthenticated();
                    System.out.println("是否认证通过: " + isAuthenticated);
                    //退出操作
                    subject.logout();
                    //是否认证通过
                    isAuthenticated = subject.isAuthenticated();
                    System.out.println("是否认证通过: " + isAuthenticated);
                }

猜你喜欢

转载自blog.csdn.net/qq_38087648/article/details/79993446