Shiro安全框架04:使用散列密码进行认证

第一步:创建配置文件

配置文件设置使用的加密算法,以及散列次数

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

#指定realm
myRealm= com.ctbu.MyRealm
#配置散列
myRealm.credentialsMatcher=$credentialsMatcher
#配置自定义散列
securityManager.realms=$myRealm

第二步:自定义realm(安全数据源)

realm是要比对的信息来源,比如用户登录的时候要验证密码是否正确,就要从realm中取出原来的密码进行验证

public class MyRealm extends AuthorizingRealm {

    /**
     * 认证
     *
     * 123456:b56a632e8ba85414e43b2ebfc5deaaed  方式:md5 盐:ctbu 散列次数:3
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //判断当前用户是否存在
        //获取用户名
        String username = (String) token.getPrincipal();
        //从数据库中取出用户名和密码
        String name = "hello";
        String password = "b56a632e8ba85414e43b2ebfc5deaaed";

        //判断用户名是否相等
        if (!name.equals(username)) {
            return null;
        }
        //第三个参数是盐
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username, password, ByteSource.Util.bytes("ctbu"), this.getName());
        return info;
    }

    /**
     * 授权
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }
}

第三步:进行认证

public class App {
    public static void main(String[] args) {
        /**
         * 1.构建securityManager工厂
         * 2.通过工厂创建securityManager
         * 3.将securityManager设置到运行环境中
         * 4.创建一个Subject实例
         * 5.创建token令牌
         * 6.用户登录
         * 7.用户退出
         *
         *
         */
        // 1.构建securityManager工厂
        IniSecurityManagerFactory factory = new IniSecurityManagerFactory("classpath:shiro.ini");
        //2.通过工厂创建securityManager
        SecurityManager securityManager = factory.getInstance();
        //3.将securityManager设置到运行环境中
        SecurityUtils.setSecurityManager(securityManager);
        //4.创建一个Subject实例
        Subject subject = SecurityUtils.getSubject();
        //5.创建token令牌
        UsernamePasswordToken token = new UsernamePasswordToken("hello", "123456");


        try {
            //6.用户登录
            subject.login(token);
        } catch (UnknownAccountException e) {
            System.out.println("账号不存在");
        } catch (IncorrectCredentialsException e){
            System.out.println("密码不正确");
        }


        System.out.println("认证是否成功"+subject.isAuthenticated());
        //7.用户退出
        subject.logout();
        System.out.println("认证是否成功"+subject.isAuthenticated());
    }

}

注意:

注册加密时候使用的算法,加的盐,以及散列次数要和认证的时候保持一致

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值