Shiro实战学习笔记(2)-自定义Realm

本文介绍了如何自定义Apache Shiro Realm,以从数据库中获取认证和授权信息,并演示了MD5加盐的密码验证实践。通过CustomRealm和CustomMd5Realm示例,探讨了如何使用Shiro进行身份验证和授权管理。
摘要由CSDN通过智能技术生成

1 自定义realm

package org.tzb.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;

/**
 * @Description 自定义realm, 将认证授权数据的来源转为数据库实现
 * @Author tzb
 * @Date 2021/8/27 22:03
 * @Version 1.0
 **/
public class CustomRealm extends AuthorizingRealm {

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

    /**
     * 认证
     *
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        // 在token中获取用户名
        String username = (String) token.getPrincipal();
        System.out.println("token获取的用户名:" + username);

        //TODO,根据身份信息使用jdbc,mybatis查询相关的数据库
        if ("Mike".equals(username)) {
            //参数1,2:数据库查到的用户名和密码
            //参数3:当前realm的名字
            SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo("Mike","123",this.getName());
            return simpleAuthenticationInfo;
        }

        return null;
    }
}

在这里插入图片描述

/**
 * @Description TODO
 * @Author tzb
 * @Date 2021/8/27 22:05
 * @Version 1.0
 **/
public class TestCustomRealmAuthenticator {

    public static void main(String[] args) {
        //1.创建SecurityManager
        DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();

        //2.设置realm
        defaultSecurityManager.setRealm(new CustomRealm());

        //3.安全工具类设置
        SecurityUtils.setSecurityManager(defaultSecurityManager);

        //4.通过安全工具类获取subject
        Subject subject = SecurityUtils.getSubject();

        //创建Token
        UsernamePasswordToken token = new UsernamePasswordToken("Mike","123");

        //执行认证
        try {
            subject.login(token);
            System.out.println("查询授权状态:" + subject.isAuthenticated());
        } catch (AuthenticationException e) {
            e.printStackTrace();
        }
        
    }
}

在这里插入图片描述

2 MD5和随机盐

在这里插入图片描述
在这里插入图片描述

2.1 测试案例

public class TestShiroMD5 {

    public static void main(String[] args) {
        //md5
        Md5Hash md5Hash = new Md5Hash("123");
        System.out.println(md5Hash.toHex());

        //MD5+salt
        Md5Hash md5Hash1 = new Md5Hash("123", "qq");
        System.out.println(md5Hash1.toHex());

        //md5 + salt + hash散列
        Md5Hash md5Hash2 = new Md5Hash("123", "qq", 1024);
        System.out.println(md5Hash2.toHex());

    }
}

在这里插入图片描述

2.2 案例

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;

/**
 * @Description md5 + salt + hash
 * @Author tzb
 * @Date 2021/8/28 10:41
 * @Version 1.0
 **/
public class CustomMd5Realm extends AuthorizingRealm {
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        //获取身份信息
        String principal = (String) token.getPrincipal();

        //根据用户名查数据库
        if ("Mike".equals(principal)) {
            SimpleAuthenticationInfo simpleAuthenticationInfo =
                    new SimpleAuthenticationInfo("Mike",
                            "ead587102bc9adbf3ffda3f28d2e5dc8",
                            ByteSource.Util.bytes("qq"), this.getName());
            return simpleAuthenticationInfo;
        }

        return null;
    }
}

/**
 * @Description TODO
 * @Author tzb
 * @Date 2021/8/27 22:05
 * @Version 1.0
 **/
public class TestCustomMd5RealmAuthenticator {

    public static void main(String[] args) {
        //1.创建SecurityManager
        DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();

        CustomMd5Realm realm = new CustomMd5Realm();

        HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
        credentialsMatcher.setHashAlgorithmName("md5");
        credentialsMatcher.setHashIterations(1024);

        //设置realm使用hash凭证匹配器
        realm.setCredentialsMatcher(credentialsMatcher);

        //2.设置realm
        defaultSecurityManager.setRealm(realm);

        //3.安全工具类设置
        SecurityUtils.setSecurityManager(defaultSecurityManager);

        //4.通过安全工具类获取subject
        Subject subject = SecurityUtils.getSubject();

        //创建Token
        UsernamePasswordToken token = new UsernamePasswordToken("Mike","123");

        //执行认证
        try {
            subject.login(token);
            System.out.println("查询授权状态:" + subject.isAuthenticated());
        } catch (AuthenticationException e) {
            e.printStackTrace();
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值