热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

shiro权限框架学习二(自定义Realm,MD5加密,加盐加密)

自定义Realmrealm:需要根据token中的身份信息去查询数据库(入门程序使用ini配置文件),如果查到用户返回认证
自定义Realm

realm:需要根据token中的身份信息去查询数据库(入门程序使用ini配置文件),如果查到用户返回认证信息,如果查询不到返回null。token就相当于是对用户输入的用户名和密码的一个封装。下面就是创建一个用户名密码token:

UsernamePasswordToken token = new UsernamePasswordToken("xuxu01", "123456");

Realm结构:

自定义Realm

package com.xuxu.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;public class MyRealm extends AuthorizingRealm{@Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {// TODO Auto-generated method stubreturn null;}/** 用户自定义认证*/@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {//第一步从token中取出身份信息(token代表用户输入的传下来的信息)String userName = (String) token.getPrincipal();//第二步:根据用户输入的userCode从数据库查询//数据库中通过名称查询出对应的用户信息//判断数据库中查出用户是否为空//假设数据库中查出用户账号密码为String name = "xuxu01";String password="123456";SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(name, password, this.getName());return authenticationInfo;}}

配置自定义Realm

需要在shiro-myrealm.ini配置realm注入到securityManager中。

[main]
#自定义realm
myRealm=com.xuxu.realm.MyRealm
#将myRealm设置进securityManager 相当于spring中注入自定义realm
securityManager.realms=$myRealm

测试java代码

package com.xuxu;import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.junit.Test;public class AuthenticationTest2_myRealm {/** 用户认证测试*/@Testpublic void authenticationTest(){//先通过ini对象创建securityManager工厂Factory factory = new IniSecurityManagerFactory("classpath:shiro-myrealm.ini");//通过工厂获取securityManagerSecurityManager securityManager = factory.getInstance();//将securityManager设入本地SecurityUtils中SecurityUtils.setSecurityManager(securityManager);//获取用户主体就是请求方Subject subject = SecurityUtils.getSubject();UsernamePasswordToken token = new UsernamePasswordToken("xuxu01","123456");try {subject.login(token);} catch (AuthenticationException e) {e.printStackTrace();}//判断是否登录成功System.out.println(subject.isAuthenticated());//登出subject.logout();System.out.println("退出登录");System.out.println(subject.isAuthenticated());}
}

结果

 

自定义Realm 支持MD5加盐验证

MD5工具类

package com.xuxu.util;import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;public class Md5Util {//构造方法中://第一个参数:明文,原始密码 //第二个参数:盐,通过使用随机数//第三个参数:散列的次数,比如散列两次,相当 于md5(md5(''))public static String md5Password(String source, String salt, int hashIterations){Md5Hash md5Hash = new Md5Hash(source, salt, hashIterations);String password_md5 = md5Hash.toString();return password_md5;}//第一个参数:散列算法 public static String md5Password(String algorithmName ,String source, String salt, int hashIterations){SimpleHash simpleHash = new SimpleHash(algorithmName, source, salt, hashIterations);String password_md5 = simpleHash.toString();return password_md5;}public static void main(String[] args) {//原始 密码 String source = "123456";//盐String salt = "3333";//散列次数int hashIterations = 2;String password_md5 = md5Password(source,salt,hashIterations);System.out.println(password_md5);// 第二种方法password_md5 = md5Password("md5",source,salt,hashIterations);System.out.println(password_md5.toString());//e5d2f047db39bac635faa95377be2de9//e5d2f047db39bac635faa95377be2de9}
}

原始密码为123456  盐为3333 散列次数2次后结果为

e5d2f047db39bac635faa95377be2de9

这个后期做为密码存入数据库中

自定义Realm

public class MyRealmMd5 extends AuthorizingRealm{@Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {// TODO Auto-generated method stubreturn null;}/** 用户自定义认证*/@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {//第一步从token中取出身份信息(token代表用户输入的传下来的信息)String userName = (String) token.getPrincipal();//第二步:根据用户输入的userCode从数据库查询//数据库中通过名称查询出对应的用户信息//判断数据库中查出用户是否为空//假设数据库中查出用户账号密码为String name = "xuxu01";String password="e5d2f047db39bac635faa95377be2de9";String salt = "3333";SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(name, password,ByteSource.Util.bytes(salt),this.getName());return authenticationInfo;}}

其中数据库中查询出来的密码为加盐散列后的密码e5d2f047db39bac635faa95377be2de9

在查询认证信息时 将盐传入ByteSource.Util.bytes(salt) 如果没有加盐就不需要这个参数

配置文件 shiro-myrealmmd5.ini

[main]
#定义凭证匹配器
credentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher
#散列算法
credentialsMatcher.hashAlgorithmName=md5
#散列次数
credentialsMatcher.hashIterations=2#将凭证匹配器注入到realm
myRealm=com.xuxu.realm.MyRealmMd5
myRealm.credentialsMatcher=$credentialsMatcher
#将myRealm设置进securityManager 相当于spring中注入自定义realm
securityManager.realms=$myRealm

测试类

public class AuthenticationTest3_myRealmMd5 {/** 用户认证测试*/@Testpublic void authenticationTest(){//先通过ini对象创建securityManager工厂Factory factory = new IniSecurityManagerFactory("classpath:shiro-myrealmmd5.ini");//通过工厂获取securityManagerSecurityManager securityManager = factory.getInstance();//将securityManager设入本地SecurityUtils中SecurityUtils.setSecurityManager(securityManager);//获取用户主体就是请求方Subject subject = SecurityUtils.getSubject();UsernamePasswordToken token = new UsernamePasswordToken("xuxu01","123456");try {subject.login(token);} catch (AuthenticationException e) {e.printStackTrace();}//判断是否登录成功System.out.println(subject.isAuthenticated());//登出subject.logout();System.out.println("退出登录");System.out.println(subject.isAuthenticated());}
}

结果

 

 

 

 

 


推荐阅读
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • Activiti7流程定义开发笔记
    本文介绍了Activiti7流程定义的开发笔记,包括流程定义的概念、使用activiti-explorer和activiti-eclipse-designer进行建模的方式,以及生成流程图的方法。还介绍了流程定义部署的概念和步骤,包括将bpmn和png文件添加部署到activiti数据库中的方法,以及使用ZIP包进行部署的方式。同时还提到了activiti.cfg.xml文件的作用。 ... [详细]
  • 大数据Hadoop生态(20)MapReduce框架原理OutputFormat的开发笔记
    本文介绍了大数据Hadoop生态(20)MapReduce框架原理OutputFormat的开发笔记,包括outputFormat接口实现类、自定义outputFormat步骤和案例。案例中将包含nty的日志输出到nty.log文件,其他日志输出到other.log文件。同时提供了一些相关网址供参考。 ... [详细]
  • 本文介绍了在go语言中利用(*interface{})(nil)传递参数类型的原理及应用。通过分析Martini框架中的injector类型的声明,解释了values映射表的作用以及parent Injector的含义。同时,讨论了该技术在实际开发中的应用场景。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 一、Hadoop来历Hadoop的思想来源于Google在做搜索引擎的时候出现一个很大的问题就是这么多网页我如何才能以最快的速度来搜索到,由于这个问题Google发明 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 本文介绍了C#中生成随机数的三种方法,并分析了其中存在的问题。首先介绍了使用Random类生成随机数的默认方法,但在高并发情况下可能会出现重复的情况。接着通过循环生成了一系列随机数,进一步突显了这个问题。文章指出,随机数生成在任何编程语言中都是必备的功能,但Random类生成的随机数并不可靠。最后,提出了需要寻找其他可靠的随机数生成方法的建议。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 使用Ubuntu中的Python获取浏览器历史记录原文: ... [详细]
  • 标题: ... [详细]
  • 如何提高PHP编程技能及推荐高级教程
    本文介绍了如何提高PHP编程技能的方法,推荐了一些高级教程。学习任何一种编程语言都需要长期的坚持和不懈的努力,本文提醒读者要有足够的耐心和时间投入。通过实践操作学习,可以更好地理解和掌握PHP语言的特异性,特别是单引号和双引号的用法。同时,本文也指出了只走马观花看整体而不深入学习的学习方式无法真正掌握这门语言,建议读者要从整体来考虑局部,培养大局观。最后,本文提醒读者完成一个像模像样的网站需要付出更多的努力和实践。 ... [详细]
  • 本文介绍了一个免费的asp.net控件,该控件具备数据显示、录入、更新、删除等功能。它比datagrid更易用、更实用,同时具备多种功能,例如属性设置、数据排序、字段类型格式化显示、密码字段支持、图像字段上传和生成缩略图等。此外,它还提供了数据验证、日期选择器、数字选择器等功能,以及防止注入攻击、非本页提交和自动分页技术等安全性和性能优化功能。最后,该控件还支持字段值合计和数据导出功能。总之,该控件功能强大且免费,适用于asp.net开发。 ... [详细]
author-avatar
1凡evan
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有