shiro md5加密

shiro md5加密

shrio 提供了一些字符串加密的api

        Object result = new SimpleHash("md5", "1234", ByteSource.Util.bytes("ckh"), 10);
        System.out.println(result);
        SimpleHash simpleHash = new SimpleHash("md5","1234",null,1);
        System.out.println(simpleHash);
        Md5Hash md5Hash = new Md5Hash("1234");
        System.out.println(md5Hash.toString());

在shiro中的认证中添加md5加密的方法

package cn.ckh2019.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.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

/**
 * @author Chen Kaihong
 * 2019-06-09 21:48
 */
public class UserRealm extends AuthorizingRealm {

    @Override
    public String getName() {
        return "userRealm";
    }


    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    /**
     * 完成身份认证(从数据库中取数据)
     * 返回认证信息,如果认证失败返回null
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //获取用户输入的用户名
        String username = (String) token.getPrincipal();
        //根据用户名到数据库查询密码信息
        //假定从数据库获取的密码是1234经过md5加密后的字符串
        String password = "6fbf9e1b65c1bef784eafff34b93b482";
        //假定从数据库获取的盐值是 "ckh"
        String salt = "ckh";
        //将从数据库查询的信息封装到SimpleAuthenticationInfo中
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username,password,ByteSource.Util.bytes(salt),getName());
        return info;
    }
}

下面是shiro.ini

[main]
credentialsMatcher= org.apache.shiro.authc.credential.HashedCredentialsMatcher
credentialsMatcher.hashAlgorithmName=md5
credentialsMatcher.hashIterations=10

userRealm=cn.ckh2019.realm.UserRealm
userRealm.credentialsMatcher=$credentialsMatcher

securityManager.realm=$userRealm

测试代码

        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
        SecurityManager securityManager = factory.getInstance();
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken("张三","1234");
        //6.进行用户验证
        try {
            subject.login(token);
            //7.通过subject来判断用户是否通过验证
            if (subject.isAuthenticated()) {
                System.out.println("登录成功");
            }
        }catch(IncorrectCredentialsException e) {
            System.out.println("密码错误");
        }catch(UnknownAccountException e){
            System.out.println("用户名错误");
        }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值