关于shiro的 subject.getPrincipal()方法

2023-05-16

1、说明

上一篇文章说明了 principal,而subject.getPrincipal();是用来干嘛的,他就是来获取你存储的principal,内部是怎么获取的那,多个principal怎么指定获取哪一个那。

2、解释

1)subject.getPrincipal();最后调用的是下面这个方法

public Object getPrimaryPrincipal() {
        if (isEmpty()) {
            return null;
        }
        return iterator().next();
    }

2)可见他便利的是集合,那就是说明每次调用都会不一样,那么就存在一定的风险性。如果我们希望每次调用都返回一个固定的值例如id,应该怎么办呐,其实非常简单,只需要扩展两个类即可。

3、编码

1)扩展SimplePrincipalCollection这个类,新建一个类来继承他,我们只需要重写他的一个方法就好,另外类他添加一个额外的来接受id的属性,这样在getPrimaryPrincipal方法中我们只需把id返回就好。代码如下

public class CustomSimplePrincipalCollection extends SimplePrincipalCollection {

    private Object primary;

    public CustomSimplePrincipalCollection() {
    }


    public CustomSimplePrincipalCollection(Object primary, Object principal, String realmName) {
        super(principal, realmName);
        this.primary = primary;
    }

    public CustomSimplePrincipalCollection(Object principal, String realmName) {
        super(principal, realmName);
    }

    public CustomSimplePrincipalCollection(Collection principals, String realmName) {
        super(principals, realmName);
    }

    public CustomSimplePrincipalCollection(PrincipalCollection principals) {
        super(principals);
    }

    @Override
    public Object getPrimaryPrincipal() {
        return primary;
    }

    public Object getCustomPrimaryPrincipal() {
        return primary;
    }
}

2)然后重写SimpleAuthenticationInfo,这个需要实现响应的接口,然后修改部分方法即可

public class CustomSimpleAuthenticationInfo implements MergableAuthenticationInfo, SaltedAuthenticationInfo {

    /**
     * The principals identifying the account associated with this AuthenticationInfo instance.
     */
    protected PrincipalCollection principals;
    /**
     * The credentials verifying the account principals.
     */
    protected Object credentials;

    /**
     * Any salt used in hashing the credentials.
     *
     * @since 1.1
     */
    protected ByteSource credentialsSalt;

    /**
     * Default no-argument constructor.
     */
    public CustomSimpleAuthenticationInfo() {
    }


    public CustomSimpleAuthenticationInfo(Object primary, Object principal, Object credentials, String realmName) {
        this.principals = new CustomSimplePrincipalCollection(primary, principal, realmName);
        this.credentials = credentials;
    }


    public CustomSimpleAuthenticationInfo(Object principal, Object hashedCredentials, ByteSource credentialsSalt, String realmName) {
        this.principals = new CustomSimplePrincipalCollection(principal, realmName);
        this.credentials = hashedCredentials;
        this.credentialsSalt = credentialsSalt;
    }


    public CustomSimpleAuthenticationInfo(PrincipalCollection principals, Object credentials) {
        this.principals = new CustomSimplePrincipalCollection(principals);
        this.credentials = credentials;
    }

    public CustomSimpleAuthenticationInfo(PrincipalCollection principals, Object hashedCredentials, ByteSource credentialsSalt) {
        this.principals = new CustomSimplePrincipalCollection(principals);
        this.credentials = hashedCredentials;
        this.credentialsSalt = credentialsSalt;
    }


    public PrincipalCollection getPrincipals() {
        return principals;
    }


    public void setPrincipals(PrincipalCollection principals) {
        this.principals = principals;
    }

    public Object getCredentials() {
        return credentials;
    }


    public void setCredentials(Object credentials) {
        this.credentials = credentials;
    }


    public ByteSource getCredentialsSalt() {
        return credentialsSalt;
    }


    public void setCredentialsSalt(ByteSource salt) {
        this.credentialsSalt = salt;
    }


    @SuppressWarnings("unchecked")
    public void merge(AuthenticationInfo info) {
        if (info == null || info.getPrincipals() == null || info.getPrincipals().isEmpty()) {
            return;
        }

        if (this.principals == null) {
            this.principals = info.getPrincipals();
        } else {
            if (!(this.principals instanceof MutablePrincipalCollection)) {
                this.principals = new SimplePrincipalCollection(this.principals);
            }
            ((MutablePrincipalCollection) this.principals).addAll(info.getPrincipals());
        }

        //only mess with a salt value if we don't have one yet.  It doesn't make sense
        //to merge salt values from different realms because a salt is used only within
        //the realm's credential matching process.  But if the current instance's salt
        //is null, then it can't hurt to pull in a non-null value if one exists.
        //
        //since 1.1:
        if (this.credentialsSalt == null && info instanceof SaltedAuthenticationInfo) {
            this.credentialsSalt = ((SaltedAuthenticationInfo) info).getCredentialsSalt();
        }

        Object thisCredentials = getCredentials();
        Object otherCredentials = info.getCredentials();

        if (otherCredentials == null) {
            return;
        }

        if (thisCredentials == null) {
            this.credentials = otherCredentials;
            return;
        }

        if (!(thisCredentials instanceof Collection)) {
            Set newSet = new HashSet();
            newSet.add(thisCredentials);
            setCredentials(newSet);
        }

        // At this point, the credentials should be a collection
        Collection credentialCollection = (Collection) getCredentials();
        if (otherCredentials instanceof Collection) {
            credentialCollection.addAll((Collection) otherCredentials);
        } else {
            credentialCollection.add(otherCredentials);
        }
    }


    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof SimpleAuthenticationInfo)) return false;

        CustomSimpleAuthenticationInfo that = (CustomSimpleAuthenticationInfo) o;

        //noinspection RedundantIfStatement
        if (principals != null ? !principals.equals(that.principals) : that.principals != null) return false;

        return true;
    }


    public int hashCode() {
        return (principals != null ? principals.hashCode() : 0);
    }


    public String toString() {
        return principals.toString();
    }

3)然后就是进行调用

 return new CustomSimpleAuthenticationInfo(admin.getId(), list, admin.getPsd(), this.getClass().getName());

4)之后你在调用subject.getPrincipal()返回的都是同一个值 也就是id,也就是CustomSimpleAuthenticationInfo(admin.getId(), list, admin.getPsd(), this.getClass().getName())的第一个参数。

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

关于shiro的 subject.getPrincipal()方法 的相关文章

  • shiro和thymealeaf整合

    shiro和thymealeaf整合 一 加入依赖
  • shiro入门详解以及使用方法、shiro认证与shiro授权

    shiro介绍 什么是shiro shiro是Apache的一个开源框架 它将软件系统的安全认证相关的功能抽取出来 实现用户身份认证 权限授权 加密 会话管理等功能 组成了一个通用的安全认证框架 它可以实现如下的功能 1 验证用户 2 对用
  • Shiro学习(5)-会话管理

    1 会话管理 相关的 API 会话监听器 配置案例 2 缓存 3 记住我
  • Shiro简单配置Springboot版(3)

    6 整合SpringBoot项目实战 6 0 整合思路 6 1 创建springboot项目 6 2 引入shiro依赖
  • Shiro 如何使用注解式进行授权操作呢?

    转自 Shiro 如何使用注解式进行授权操作呢 下文笔者讲述Shiro进行注解式授权操作的方法分享 如下所示 Shiro注解授权 常使用以下注解字段 如 RequiresRoles RequiresPermissions RequiresA
  • Shiro实战学习笔记(2)-自定义Realm

    1 自定义realm package org tzb realm import org apache shiro authc AuthenticationException import org apache shiro authc Aut
  • Shiro之@RequiresPermissions注解原理详解

    前言 shiro为我们提供了几个权限注解 如下图 这几个注解原理都类似 这里我们讲解 RequiresPermissions的原理 铺垫 第一 首先要清楚 RequiresPermissions的基本用法 就是在Controller的方法里
  • Shiro简单配置Springboot版(1)

    1 权限的管理 1 1 什么是权限管理 基本上涉及到用户参与的系统都要进行权限管理 权限管理属于系统安全的范畴 权限管理实现对用户访问系统的控制 按照安全规则或者安全策略控制用户可以访问而且只能访问自己被授权的资源 权限管理包括用户身份认证
  • Shiro权限框架-限制密码重试次数(8)

    1 实现原理 保证原子性 单系统 AtomicLong计数 集群系统 RedissionClient提供的RAtomicLong计数 1 获取系统中是否已有登录次数缓存 缓存对象结构预期为 用户名 登录次数 2 如果之前没有登录缓存 则创建
  • shiro多项目跳转用户身份失效问题排查

    shiro多项目跳转用户身份失效问题排查 1 身份失效问题 最近在项目中遇到过一个问题 统一登录系统中有各个子系统的模块 可点击子系统模块进行跳转 如下图所示 如上图 当用户点击子系统B新窗口打开时 实现跳转成功 当再回到原统一登录系统页面
  • SpringBoot 整合 Shiro 常见配置

    目录 一 Shiro 基础解释 过滤器 AOP 实现安全认证权限管理逻辑 了解 Shiro 的组织架构 二 SpringBoot 整合 Shiro 1 在项目中使用 Shiro 需要配置的地方 2 代码示例 引入依赖 请求接口 自定义 Re
  • 关于shiro反序列化漏洞一次完整的攻击

    1 1 漏洞原理 Apache Shiro框架提供了记住密码的功能 RememberMe 用户登录成功后会生成经过加密并编码的cookie 在服务端对rememberMe的cookie值 先base64解码然后AES解密再反序列化 就导致了
  • Java面试题--shiro

    Shiro可以做哪些工作 Shiro可以帮助我们完成 认证 授权 加密 会话管理 与Web集成 缓存等 shiro有哪些组件 Authentication 身份认证 登录 验证用户是不是拥有相应的身份 Authorization 授权 即权
  • springboot整合shiro-登录失败次数限制(八)

    原文地址 转载请注明出处 https blog csdn net qq 34021712 article details 80461177 王赛超 这次讲讲如何限制用户登录尝试次数 防止坏人多次尝试 恶意暴力破解密码的情况出现 要限制用户登
  • shiro使用自定义realm实现数据认证

    自定义realm实现数据认证 在开发中 有时会与一些nosql或者其他地方保存的数据进行认证 这时候 shiro定义的那些realm类可能不能满足实际的功能需求 这时候我们可以通过自定义一个realm来沟通这些数据 实现认证和权限控制 首先
  • Spring MongoDB 和 Apache Shiro

    我正在尝试将 Apache Shiro 与 Spring 和 MongoDB 结合使用 我正在使用自动连接的 Spring 数据存储库 我为 Shiro 创建了自己的自定义领域 它使用 Spring 数据存储库与 Mongo 进行通信 pu
  • 带有 jdbc 和哈希密码的 shiro

    这是我的 shiro 配置 main authc loginUrl site index jsp authc usernameParam user authc passwordParam pass authc rememberMeParam
  • 非重放热可观察

    原问题 我有一个场景 我有多个IObservable我想要组合的序列Merge然后听 但是 如果其中之一产生错误 我不希望它使其他流的所有内容崩溃 也不希望重新订阅序列 这是一个 永远持久 的序列 我通过附加一个来做到这一点Retry 合并
  • 使用 ini 文件进行 Spring MVC 和 Shiro 配置

    我正在尝试使用 Spring MVC 和 Apache Shiro 建立一个环境 我正在关注 shiro apache org 中提到的文章 我在 web xml 中使用 Spring 的 DelegatingFilterProxy 作为
  • Shiro/Stormpath 通过 REST

    我是新来的士郎 我们正在尝试将 Shiro 与 Stormpath 一起使用 我一直在尝试剖析这些例子 以找出我想做的解决方案 但到目前为止我还没有成功 目前 我只是尝试创建 REST 服务来执行我想要的操作 稍后我将绑定一个真正的客户端

随机推荐

  • Arch 基本安装后的使用配置

    A 参考借用整理 1 Arch wiki https wiki archlinux org index php Installation guide B 注意 1 选择有很多 xff0c 可以根据自己需求来 xff0c 如速度 xff0c
  • 【IP技术】网络安全防护措施

    网络安全威胁造成的形式主要包含运用系统软件缺点或侧门 xff0c 运用网络防火墙安全隐患 xff0c 内部结构客户的泄密 泄露和毁坏 xff0c 动态口令进攻和拒绝服务式攻击等 针对该网络安全威胁 xff0c 现阶段的预防措施主要有五种 x
  • 如何搭建本地yum仓库

    一 yum简介 yum xff08 Yellow dog Updater Modified xff09 是一个在 Fedora 和 RedHat 以及 SUSE 中的 Shell 前端软件包管理器 基于 RPM 包管理 xff0c 能够从指
  • python_tweets.json (python数据挖掘入门与实践数据集下载)

    最近在看python数据挖掘入门与实践一书 xff0c 书不错 xff0c 有个不好的地方是 xff0c 书上所用的数据集 xff0c 有几个测试数据在网上非常不好找 下面几个资源是我自己整理出来的 xff0c 上传到CSDN xff0c
  • ios UILabel显示html文本

    let attrContent 61 try NSAttributedString data htmlContent options NSDocumentTypeDocumentAttribute NSHTMLTextDocumentTyp
  • 转行的辛苦

    我是2004年毕业的 xff0c 学的专业是市场营销 xff0c 毕业后来到深圳 xff0c 换了很多工作 xff0c 一直都无法找到令自己满意的工作 因为我非常喜欢计算机 xff0c 从中学到大学 xff0c 一直是班级里公认的计算机高手
  • 内存优化 和 性能优化 的总结

    从 检查内存 xff0c 减少使用 xff0c 复用 xff0c 以及及时释放几个维度去考虑 1 检查 可以ddms查看内存使用情况 xff0c 可以使用 adb shell dumpsys meminfo 查看 xff0c 也可以使用 l
  • ubuntu16.04 安装gnome经典桌面

    一直比较喜欢旧版本Ubuntu的Gnome风格的菜单栏 xff0c 在Ubuntu16 0 4中可以执行指令 xff1a sudo apt get install gnome session flashback 安装完成 xff0c 注销一
  • Gson在序列化反序列化中的TypeAdapter

    1 package waf json adatpter 2 3 import java io IOException 4 import java util ArrayList 5 import java util List 6 import
  • 技术泡妹子二:篡改百度首页,惊呆女神

    大多数网民上网的入口都是先打开百度 xff0c 然后再搜索xxx 进入 xff0c 为了给女神惊喜 xff0c 决定篡改百度首页让女神惊呆 xff0c 当然不是黑了百度 xff0c 目前没这个实力 xff0c 但是我们可以修改host文件
  • VC多线程中控制界面控件的几种方法

    转 http hi baidu com magicyang87 blog item 23bbf2fd72d6b81108244d73 html 为了保证界面的用户体验经常要把数据处理等放到子线程中进行 xff0c 然后把结果更新到主界面 x
  • 一次性打包学透 Spring

    不知从何时开始 xff0c Spring 这个词开始频繁地出现在 Java 服务端开发者的日常工作中 xff0c 很多 Java 开发者从工作的第一天开始就在使用 Spring Framework xff0c 甚至有人调侃 不会 Sprin
  • 关于产品的一些思考——写在前面的话

    自己是一个十足的Geek xff0c 喜欢使用各种新奇的东西 xff0c 包括软件 硬件 技术 xff0c 又因为自己一点点轻微的强迫症和完美主义 xff0c 在这个过程中总会有自己的一些思考 xff0c 又因为技术出身 xff0c 总会考
  • mybatis映射文件mapper.xml的写法。

    在学习mybatis的时候我们通常会在映射文件这样写 xff1a lt xml version 61 34 1 0 34 encoding 61 34 UTF 8 34 gt lt DOCTYPE mapper PUBLIC 34 myba
  • layer的弹出层的简单的例子

    如果不了级的基本的清楚官网查看api网址为 http layer layui com 我用的是iframe 如果是iframe层 layer open type 2 content 39 http sentsin com 39 这里cont
  • 左链接Column 'id' in field list is ambiguous

    如题错误如左链接Column 39 id 39 in field list is ambiguous 今天在写sm的时候 xff0c 用到两个表的联合查询出现的如下的错误 xff0c 仔细查找才发现原来两个表的id重复了 xff0c use
  • maven出现:Failed to execute goal on project ...: Could not resolve dependencies for project ...

    1 我的项目结构是一个父项目 xff0c 多个子项目目录如下 xff1a 2 我这里就举个例子 xff0c 所以应用的也就是core和domain这两个项目 3 两个项目都继承父项目 4 在模块中domain依赖于core xff0c 在c
  • EOS的CPU危机:BM的租赁模式或只是乌托邦

    摘要 xff1a 继RAM内存之后 xff0c EOS的CPU危机也爆发了 昨日 xff0c 由于BetDice和EOSBET为了保证游戏的运行 xff0c 占用了过多的主网CPU xff0c 导致用户资源紧张 xff0c 甚至无法转账 昔
  • 有关Shiro中Principal的使用

    1 定义 principal代表什么那 xff1f 如果阅读官方文档或者源码你会得到如下的定义 xff1a 解释 xff1a 1 xff09 可以是uuid 2 xff09 数据库中的主键 3 xff09 LDAP UUID或静态DN 4
  • 关于shiro的 subject.getPrincipal()方法

    1 说明 上一篇文章说明了 principal xff0c 而subject getPrincipal 是用来干嘛的 xff0c 他就是来获取你存储的principal xff0c 内部是怎么获取的那 xff0c 多个principal怎么