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

2023-11-05

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();
        }

    }
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Shiro实战学习笔记(2)-自定义Realm 的相关文章

随机推荐

  • 前端技术搭建贪吃蛇小游戏(内含源码)

    功能介绍 以下是贪吃蛇小游戏的玩法和规则 游戏开始时 玩家控制一条小蛇在游戏区域内移动 通过吃食物来增加分数 小蛇的移动方向由玩家控制 可以使用键盘上的方向键来控制小蛇的移动方向 当小蛇吃到食物时 它会变长 并且玩家的分数会增加 如果小蛇撞
  • 拷贝构造函数(默认的,自定义的,什么时候一定要自定义,什么时候系统会自动调用)

    为什么有指针成员的类 要自定义拷贝构造函数 参考了 https blog csdn net caoshangpa article details 79226270 没有拷贝构造函数的类 系统会创建默认的拷贝构造函数 默认拷贝构造函数是浅拷贝
  • 【JSON 初级】

    概述 前后台数据交换的格式标准 一种优秀的 数据格式 采用键值对的方式 取数据 用键 优势 比XML更小 更快 更容易解析 JSON是存储和交换文本信息的语法 类似XML 工具 网上有校验json数据 并提示错误 将数据转化为json数据
  • 深入理解java虚拟机【并发编程缓存】

    2019独角兽企业重金招聘Python工程师标准 gt gt gt 随着多核CPU的高速发展 为了充分利用硬件的计算资源 操作系统的并发多任务功能正变得越来越重要 但是CPU在进行计算时 还需要从内存读取输出 并 将计算结果存放到内存中 然
  • 【华为OD机试】分苹果(C++ Python Java)2023 B卷

    时间限制 C C 1秒 其他语言 2秒 空间限制 C C 262144K 其他语言524288K 64bit IO Format lld 语言限定 C clang11 C clang 11 Pascal fpc 3 0 2 Java jav
  • 闲聊:自动化到底是干什么的?

    很多人会问 自动化到底是干什么的 也许是因为这个专业所要学习和掌握的知识太庞杂了 以至于自动化被称之为万能胶 干什么都行 却又都不专业 很大一部分同学上到大二大三还不知道自己具体能做什么 迷茫中便选择了转行 希望还在迷茫中的低年级的同学看了
  • C++11变长模板解析(深入理解C++11)

    参考自 深入理解C 11 变长模版 变长函数和变长的模版参数 变长函数 double sum int n 求n个double数据之和 double sum 0 va list args 接受输入数据的数据结构 需声明stdarg h va
  • 3D游戏设计作业9:游戏智能

    坦克对战游戏 AI 设计 游戏截图 1 作业要求 从商店下载游戏 Kawaii Tank 或 其他坦克模型 构建 AI 对战坦克 具体要求 使用 感知 思考 行为 模型 建模 AI 坦克 场景中要放置一些障碍阻挡对手视线 坦克需要放置一个矩
  • python 点名程序(随机点名不重复 可定义名字列表 语音播报 免费下载 多线程打包)

    python点名小程序 含有 调用windows本地语音播报 python多线程打包 等小技巧 软件获取 点击下方地址直接下载压缩包 免费为大家提供 软件获取地址 大家拿了软件别忘了给博主一个免费的赞 谢谢 解压压缩包 里面的MyAPP e
  • 配置和美化Arch Linux

    前面说了如何安装一个最小化的Arch Linux 现在来说说如何配置 配置网络 如果使用有线网络的话 将dhcp服务开机启动 systemctl enable dhcpcd 如果使用无线网络的话 使用wifi menu命令连接网络 如果在使
  • Base64图片上传

    文章目录 1 图片上传样式写法 2 Js写法 1 图片上传样式写法 div class form group div
  • python+OpenCV图像处理(五)图像的阈值分割

    图像的阈值处理 一幅图像包括目标物体 背景还有噪声 要想从多值的数字图像中直接提取出目标物体 常用的方法就是设定一个阈值T 用T将图像的数据分成两部分 大于T的像素群和小于T的像素群 这是研究灰度变换的最特殊的方法 称为图像的二值化 Bin
  • 分号与逗号的区别及举例_顿号与逗号与分号间的区别是什么?

    逗号把句子切分为意群 表示小于分号大于顿号的停顿 而逗号有哪些用法呢 以下是由小编整理关于逗号如何使用的内容 希望大家喜欢 逗号汉语用法句子内部主语与谓语之间如需停顿 用逗号 例如 我们看得见的星星 绝大多数是恒星 句子内部动词与宾语之间如
  • STM32设置为I2C从机模式

    STM32设置为I2C从机模式 目录 STM32设置为I2C从机模式 前言 1 硬件连接 2 软件编程 3 运行测试 3 1 I2C连续写入 3 2 I2C连续读取 3 3 I2C单次读写测试 4 总结 前言 STM32的I2C作为主机的情
  • pentaho安装

    注意 以下图片文字中的org mysql一律改为com mysql 1 pentaho社区版下载地址 https sourceforge net projects pentaho 2 下载以下两个文件 3 biserver ce 6 1 0
  • JMeter 设置请求头信息的详细步骤

    在使用 JMeter 的过程中 我们会遇到需要设置请求头信息的场景 比如 POST 传过去的 Body 数据是 json 格式的 需要填添加头信息 Content Type application json 在 header 中用 toke
  • python中错误Reshape your data either using array.reshape(-1, 1)

    1 错误 Traceback most recent call last File Users yuanbao PycharmProjects EnsembleLearning KNeighbors py line 16 in
  • 数据控制类别(CC1和CC2)——DO-178B/ED-12B学习笔记之七

    数据控制类别 CC1和CC2 DO 178B ED 12B学习笔记之七 为了理解数据控制类别 CC1和CC2 的定义 先看DO 178B的7 3条 原文 Software life cycle data can be assigned to
  • NUC980开源项目27-you should not run configure as root

    上面是我的微信和QQ群 欢迎新朋友的加入 项目码云地址 国内下载速度快 https gitee com jun626 nuc980 open source project 项目github地址 https github com Jun117
  • Shiro实战学习笔记(2)-自定义Realm

    1 自定义realm package org tzb realm import org apache shiro authc AuthenticationException import org apache shiro authc Aut