Shiro的Realms

2023-10-08 04:50
文章标签 shiro realms

本文主要是介绍Shiro的Realms,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

为什么80%的码农都做不了架构师?>>>   hot3.png

Realms概述

n概述

  Realm 是一个能够访问应用程序特定的安全数据(如用户、角色及权限)的组件。

  Realm 通常和数据源是一对一的对应关系,如关系数据库,LDAP 目录,文件系统,或其他类似资源。Realm 实质上就是一个特定安全的DAO。

  因为这些数据源大多通常存储身份验证数据(如密码的凭证)以及授权数据(如角色或权限),每个Realm能够执行身份验证和授权操作。

 

n关于Realm的配置

  这个在前面讲过了,这个就不去赘述了

 

理解Realms的认证实现

n前面学到过,Shiro的认证过程最终会交由Realm执行,这时会调用Realm的getAuthenticationInfo(token)方法。

  该方法通常会在org.apache.shiro.realm.AuthenticatingRealm中实现,当然,这个方法中会调用到具体realm实现的方法。

n该方法主要执行以下操作:

1、检查提交的进行认证的令牌信息

2、根据令牌信息从数据源(通常为数据库)中获取用户信息

3、对用户信息进行匹配验证。

4、验证通过将返回一个封装了用户信息的AuthenticationInfo实例。

5、验证失败则抛出AuthenticationException异常信息。

 

  这是对所有Realm getAuthenticationInfo 实现的最高级别的工作流。

  验证通过后,就返回一个非空的AuthenticationInfo 实例来代表来自于该数据源的Subject 帐户信息。

 

Shiro默认的Realms的认证实现

n所有Shiro 立即可用的Realm 的实现默认使用SimpleCredentialsMatcher。SimpleCredentialsMatcher 执行一个普通的直接相等性的检查,也就是在存储的帐户credentials 与在AuthenticationToken 所提交的之间的检查。

n使用Hashing Credentials

  如果要使用Hashing Credentials,那么需要在配置中告诉验证器,使用相应的匹配器,这个在前面示例过。

  但是前面直接使用的Sha256Matcher,已经不推荐使用了,现在推荐使用统一的HashedCredentialsMatcher,然后配置具体的算法名称,这些名称按照Java Security Framework里面的标准名称来配置。常见的名称有:

  MD5、AES 、DES 、SHA-1、SHA-256、SHA-384、SHA-512……很多

  具体可以参见:http://docs.oracle.com/javase/6/docs/technotes/guides/security/StandardNames.html

 

 

n新的实现配置示例:

[main]
sha256Matcher = org.apache.shiro.authc.credential.HashedCredentialsMatcher
sha256Matcher.hashAlgorithmName = SHA-256
sha256Matcher.storedCredentialsHexEncoded=false
iniRealm.credentialsMatcher = $sha256Matcher
myRealm1=cn.javass.hello.MyRealm
myRealm1.credentialsMatcher = $sha256Matcher
authenticator = org.apache.shiro.authc.pam.ModularRealmAuthenticator
authcStrategy = org.apache.shiro.authc.pam.AllSuccessfulStrategy
authenticator.authenticationStrategy = $authcStrategy
authenticator.realms=$myRealm1,iniRealm
securityManager.authenticator = $authenticator
[users]
javass =NVsbv8lnJc3Oj0onCP2jEKgObRMxWuxOXu0qdf6AMs4=,role1
[roles]
role1 = p1,p2

  当然,别忘了在MyRealm中,创建SimpleAuthenticationInfo时传的密码就应该是加密后的字符串了

 

n你还可以在密码加密的时候,加点salt,使密码更安全。这种方式目前默认的iniRealm没有支持,只能是在自己扩展的Realm里面使用

n新的配置文件示例:

[main]
sha256Matcher = org.apache.shiro.authc.credential.HashedCredentialsMatcher
sha256Matcher.hashAlgorithmName = SHA-256
sha256Matcher.storedCredentialsHexEncoded=false
sha256Matcher.hashIterations = 10
myRealm1=cn.javass.hello.MyRealm
myRealm1.credentialsMatcher = $sha256Matcher
authenticator = org.apache.shiro.authc.pam.ModularRealmAuthenticator
authenticator.realms=$myRealm1
securityManager.authenticator = $authenticator
[users]
javass =kExd2f52W1M/wXidIRjOfMDj76DVo6e2md+7Rn4ubmY=,role1
[roles]
role1 = p1,p2

 

n获得加盐后的密码字符串

String ss = new Sha256Hash("cc","javass",10).toBase64();

 

n在自定义的realm中,返回的SimpleAuthenticationInfo需要修改一下,要加入salt的信息,当然,密码也需要是加密后的字符串,修改为:

SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username,"kExd2f52W1M/wXidIRjOfMDj76DVo6e2md+7Rn4ubmY=" .toCharArray(),ByteSource.Util.bytes("javass".getBytes()),getName()
);

 要保证你的Realm 实现必须返回一个SaltedAuthenticationInfo 实例而不是一个普通的AuthenticationInfo 实例。

使用默认的JdbcRealm

n这个需要在数据库中建立相应的表,然后配置相应的数据库连接,然后才能使用,这里以spring中的bean定义来说明一下,示例如下:

<bean id="myRealm" class="org.apache.shiro.realm.jdbc.JdbcRealm"><property name="dataSource" ref="dataSource" /><property name="authenticationQuery"value="select u.pwd from tbl_user u where u.uuid = ?" /><property name="userRolesQuery"value="select r.uuid from tbl_user_role ur left join tbl_role r on ur.roleUuid = r.uuid where ur.userUuid = ? " /><property name="permissionsQuery"value="select p.uuid from tbl_role r left join tbl_role_permission 
rp on r.uuid = rp.roleUuid left join tbl_permission p on 
rp.permissionUuid = p.uuid where r.uuid = ? " /><property name="permissionsLookupEnabled" value="true" /><property name="saltStyle" value="NO_SALT" />
</bean>


自定义Realm

    n自定义Reald非常简单,通常是继承AuthorizingRealm 抽象类,这个类实现了常用的authentication 及authorization 工作流来节省你的时间和精力。n然后覆盖doGetAuthenticationInfo,在这个方法里面实现获取用户信息

n基本的示例如下:

protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {//token中储存着输入的用户名和密码UsernamePasswordToken upToken = (UsernamePasswordToken)token;String username = upToken.getUsername();//通常是根据用户名去数据库中查询相应信息,这里就省略了//当然这里也可以对用户名和密码进行校验SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username, password .toCharArray(),getName());return info;
}

n然后覆盖doGetAuthorizationInfo,在这个方法里面实现获取用户权限的信息,基本的示例如下:

protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {String userName = (String) getAvailablePrincipal(principals);//通过用户名去获得用户的所有资源,并把资源存入info中//当然这里通常会操作数据库去获取SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();Set<String> s = new HashSet<String>();s.add("p1");s.add("p2");info.setStringPermissions(s);Set<String> r = new HashSet<String>();r.add("r1");r.add("r2");info.setRoles(r);return info;
}

 

在spring的配置文件中使用

n在Spring的配置文件中,配置的内容和ini文件是一样,只不过转换成xml的风格,用bean的方式来配置

n基本的示例如下:

<bean id="sha256Matcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher"><property name="hashAlgorithmName" value="SHA-256"></property><property name="storedCredentialsHexEncoded" value="false"></property><property name="hashIterations" value="10"></property>
</bean>
<bean id="myRealm2" class="cn.javass.hello.MyRealm"><property name="credentialsMatcher" ref="sha256Matcher"></property>
</bean>

 而且,由于把我们的Realm配置成bean了,自然就可以使用依赖注入等功能了。

私塾在线   原创,转载请注明   http://sishuok.com/forum/blogPost/list/0/7456.html



转载于:https://my.oschina.net/boonya/blog/348146

这篇关于Shiro的Realms的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:https://blog.csdn.net/weixin_34174132/article/details/91752507
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/162804

相关文章

【Shiro】Shiro 的学习教程(三)之 SpringBoot 集成 Shiro

目录 1、环境准备2、引入 Shiro3、实现认证、退出3.1、使用死数据实现3.2、引入数据库,添加注册功能后端代码前端代码 3.3、MD5、Salt 的认证流程 4.、实现授权4.1、基于角色授权4.2、基于资源授权 5、引入缓存5.1、EhCache 实现缓存5.2、集成 Redis 实现 Shiro 缓存 1、环境准备 新建一个 SpringBoot 工程,引入依赖:

【Shiro】Shiro 的学习教程(二)之认证、授权源码分析

目录 1、背景2、相关类图3、解析3.1、加载、解析阶段3.2、认证阶段3.3、授权阶段 1、背景 继上节代码,通过 debug 进行 shiro 源码分析。 2、相关类图 debug 之前,先了解下一些类的结构图: ①:SecurityManager:安全管理器 DefaultSecurityManager: RememberMeManager:实现【记住我】功能

【Shiro】Shiro 的学习教程(一)之快速入门

目录 1、Shiro 简介2、Shiro 认证、授权2.1、认证2.2、授权 3、快速入门4、自定义 Realm5、加密6、实现授权 1、Shiro 简介 Shiro 官网:https://shiro.apache.org/ Shiro 是一个功能强大且易于使用的 Java 安全框架,它执行身份验证、授权、加密和会话管理。使用 Shiro 易于理解的 API,您可以快速轻松地保

shiro session 监听

spring 使用 shiro 后,由于shiro重新封装了原有的session,所以不能再使用原来的session监听方法了 (1)在shiro配额只文件中设置监听类   <!-- shiroSessionListener  监听类--><bean id="shiroSessionListener" class="com.listener.ShiroSessionListener"></

Shiro身份认证流程

http://www.toutiao.com/a6353832181929279746/?tt_from=mobile_qq&utm_campaign=client_share&app=explore_article&utm_source=mobile_qq&iid=5840657922&utm_medium=toutiao_ios

Shiro-721漏洞详解及复现

之前有些Shiro-550反序列化漏洞的文章Shiro-550漏洞详解及复现_shiro框架漏洞-CSDN博客 这里简单补充一下关于Shiro-721漏洞的内容。 目录 Shiro-721漏洞原理 Padding Oracle攻击 利用条件 影响版本 漏洞复现 Shiro-721漏洞原理 Shiro-721用到的加密方式是AES-128-CBC,跟Shiro-550最大的

进阶SpringBoot之 Shiro(6)整合 Thymeleaf

Subject:用户 SecurityManager:管理所有用户 Realm:连接数据 pom.xml 导入 thymeleaf-extras-shiro 的 jar 包,整合 shiro-thymeleaf <!-- shiro-thymeleaf 整合 --><dependency><groupId>com.github.theborakompanioni</grou

Shiro的认证原理(Subject#login的背后故事)

登录操作一般都是我们触发的: Subject subject = SecurityUtils.getSubject();AuthenticationToken authenticationToken = new ...subject.login(authenticationToken); Subject的登录将委托给SecurityManager,SecurityManager的logi

Shiro过滤器的维护与匹配执行

servlet的初始化会触发核心过滤器的创建: public Object getObject() throws Exception {if (instance == null) {instance = createInstance();}return instance;} 在createInstance方法中会调用 FilterChainManager manager = create

shiro的使用入门

shiro是一个权限控制框架,因为项目需要自己看了看,在这里把所有学到的分享一下。shiro主要由AuthorizationInfo、AuthenticationInfo、Subject构成一个权限环境,doGetAuthenticationInfo方法是用户登录的使用调用的( subject.login(token);),doGetAuthorizationInfo方法是在用户进行权限验