shiro的基础知识储备--认证加密流程

使用MD5和salt进行加密再进行认证

shiro的基础知识储备–认证流程中创建了连接数据库的类。但是对于一些系统的用户名和用户名密码都是需要进行加密进行保存的,那我们在使用shiro进行认证的时候,就需要把MD5和salt加入。

1,Md5和 salt概念

实际应用就是将盐和散列后的值存在数据库中,自动realm从数据库取出盐和加密后的值有shiro完成密码校验。
Salt(盐)’:让用户的密码更加复杂,比如输出的密码是1234然后再后面生成一个随机的字符串,让密码更加的复杂。
MD5算法用来加密或者是签名(校验和,是校验两个文件内容是否相同)
特点:不可逆,只能通过明文推成密文,还有如果内容相同无论执行多少次,MD5生成的结果始终是一致的。
生成的结果:始终是一个16进制32位长度的字符串

2, MD5+salt+hash使用流程图

md5具体使用的过程

3,Shiro中使用 MD5+salt+hash算法方式:

(1)自定义Realm类
package com.example.shiro.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

//使用自定义reaml加入MD5+salt+hash
public class customerMd5Realm extends AuthorizingRealm {


//    自定义授权方法,略过
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    }


//   自定义认证方法
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
//        获取身份信息
        String principal = (String) authenticationToken.getPrincipal();
//        根据用户名查询数据库
        if ("xiaochen".equals(principal)) {
//            这是单纯的MD55算法生成的密码
            SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(principal, "202cb962ac59075b964b07152d234b70", this.getName());
/**
 *  md5+salt生成的密码验证方式,多了一个参数,是salt生成随机后缀的表达式
 * 参数1:数据库中的用户名 参数2:数据库md5+salt之后的密码 参数3:注册时的随机盐 参数4:realm的名字
 * 并且shiro会自动识别随机盐
 * 如果想使用MD5+salt+hash就要在指定算法的地方去设置散列的次数
 */
            SimpleAuthenticationInfo simpleAuthenticationInfo1 = new SimpleAuthenticationInfo(principal, "8a83592a02263bfe6752b2b5b03a4799", ByteSource.Util.bytes("X0*7ps"), this.getName());
            return simpleAuthenticationInfo;
        }


        return null;
    }
}

(2)主方法调用自定义Realm测试
package com.example.shiro.demoshiro;

import com.example.shiro.realm.customerMd5Realm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TestCustomerMd5RealmAuthenicator {
    public static void main(String[] args) {
//        创建安全管理器
        DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
//        设置realm使用hash凭证匹配器,我们自定义的realm继承了AuthorizingRealm,而AuthorizingRealm中可以去设置凭证匹配器,但是我们要创建一个匹配器的对象
        customerMd5Realm realm = new customerMd5Realm();
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();//创建匹配器的对象
//        使用算法
        hashedCredentialsMatcher.setHashAlgorithmName("md5");//给这个匹配器命名
//        如果使用md5+salt+hash时,指定其散列次数
        hashedCredentialsMatcher.setHashIterations(1024);//给这个匹配器设置hash次数
        realm.setCredentialsMatcher(hashedCredentialsMatcher);//给这个realm设置这个匹配器
//        注入realm
        defaultSecurityManager.setRealm(realm);
//        将安全管理器注入安全工具
        SecurityUtils.setSecurityManager(defaultSecurityManager);
//        通过安全工具类获取subject
        Subject subject = SecurityUtils.getSubject();
//        认证
        UsernamePasswordToken token = new UsernamePasswordToken("xiaochen", "123");
        try {
            subject.login(token);
            System.out.println("登陆成功");
        } catch (UnknownAccountException e) {
            e.printStackTrace();
            System.out.println("认证失败");

        }
        catch (IncorrectCredentialsException e) {
            e.printStackTrace();
            System.out.println("密码错误");
        }
            }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值