资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

shiro教程(2)-shiro介绍

shiro教程系列

shiro教程(3)-shiro授权

创新互联服务项目包括莲花网站建设、莲花网站制作、莲花网页制作以及莲花网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,莲花网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到莲花省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

shiro介绍 

1.1 什么是shiro

Shiro是apache旗下一个开源框架,它将软件系统的安全认证相关的功能抽取出来,实现用户身份认证,权限授权、加密、会话管理等功能,组成了一个通用的安全认证框架。

1.2 为什么要学shiro

既然shiro将安全认证相关的功能抽取出来组成一个框架,使用shiro就可以非常快速的完成认证、授权等功能的开发,降低系统成本。

shiro使用广泛,shiro可以运行在web应用,非web应用,集群分布式应用中越来越多的用户开始使用shiro。

Java领域中spring security(原名Acegi)也是一个开源的权限管理框架,但是spring security依赖spring运行,而shiro就相对独立,最主要是因为shiro使用简单、灵活,所以现在越来越多的用户选择shiro。

 

1.3 Shiro架构

 

  shiro教程(2)- shiro介绍

 

 

 

最基础的是Realm接口,CachingRealm负责缓存处理,AuthenticationRealm负责认证,AuthorizingRealm负责授权,通常自定义的realm继承AuthorizingRealm。

 

2.3.2 自定义Realm

 

[java]view plain copy print? shiro教程(2)- shiro介绍  shiro教程(2)- shiro介绍

  1. public class CustomRealm1 extends AuthorizingRealm {  

  2.   

  3.    

  4.   

  5. @Override  

  6.   

  7. public String getName() {  

  8.   

  9. return "customRealm1";  

  10.   

  11. }  

  12.   

  13.    

  14.   

  15. //支持UsernamePasswordToken  

  16.   

  17. @Override  

  18.   

  19. public boolean supports(AuthenticationToken token) {  

  20.   

  21. return token instanceof UsernamePasswordToken;  

  22.   

  23. }  

  24.   

  25.    

  26.   

  27. //认证  

  28.   

  29. @Override  

  30.   

  31. protected AuthenticationInfo doGetAuthenticationInfo(  

  32.   

  33. AuthenticationToken token) throws AuthenticationException {  

  34.   

  35. //从token中 获取用户身份信息  

  36.   

  37. String username = (String) token.getPrincipal();  

  38.   

  39. //拿username从数据库中查询  

  40.   

  41. //....  

  42.   

  43. //如果查询不到则返回null  

  44.   

  45. if(!username.equals("zhang")){//这里模拟查询不到  

  46.   

  47. return null;  

  48.   

  49. }  

  50.   

  51. //获取从数据库查询出来的用户密码  

  52.   

  53. String password = "123";//这里使用静态数据模拟。。  

  54.   

  55. //返回认证信息由父类AuthenticatingRealm进行认证  

  56.   

  57. SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(  

  58.   

  59. username, password, getName());  

  60.   

  61.    

  62.   

  63. return simpleAuthenticationInfo;  

  64.   

  65. }  

  66.   

  67.    

  68.   

  69. //授权  

  70.   

  71. @Override  

  72.   

  73. protected AuthorizationInfo doGetAuthorizationInfo(  

  74.   

  75. PrincipalCollection principals) {  

  76.   

  77. // TODO Auto-generated method stub  

  78.   

  79. return null;  

  80.   

  81. }  

  82.   

  83.    

  84.   

  85. }  

  86.   

  87.    


2.3.3 shiro-realm.ini

[html]view plain copy print? shiro教程(2)- shiro介绍  shiro教程(2)- shiro介绍

  1. [main]  

  2.   

  3. #自定义 realm  

  4.   

  5. customRealm=com.sihai.shiro.authentication.realm.CustomRealm1  

  6.   

  7. #将realm设置到securityManager  

  8.   

  9. securityManager.realms=$customRealm  

  10.   

  11.    


 

2.3.4 测试代码

 

测试代码同入门程序,将ini的地址修改为shiro-realm.ini。

 

分别模拟账号不存在、密码错误、账号和密码正确进行测试。

 

2.4 散列算法

散列算法一般用于生成一段文本的摘要信息,散列算法不可逆,将内容可以生成摘要,无法将摘要转成原始内容。散列算法常用于对密码进行散列,常用的散列算法有MD5、SHA。

一般散列算法需要提供一个salt(盐)与原始内容生成摘要信息,这样做的目的是为了安全性,比如:111111的md5值是:96e79218965eb72c92a549dd5a330112,拿着“96e79218965eb72c92a549dd5a330112”去md5破解网站很容易进行破解,如果要是对111111和salt(盐,一个随机数)进行散列,这样虽然密码都是111111加不同的盐会生成不同的散列值。

 

2.4.1 例子

 

[java]view plain copy print? shiro教程(2)- shiro介绍  shiro教程(2)- shiro介绍

  1. //md5加密,不加盐  

  2.   

  3. String password_md5 = new Md5Hash("111111").toString();  

  4.   

  5. System.out.println("md5加密,不加盐="+password_md5);  

  6.   

  7. //md5加密,加盐,一次散列  

  8.   

  9. String password_md5_sale_1 = new Md5Hash("111111", "eteokues", 1).toString();  

  10.   

  11. System.out.println("password_md5_sale_1="+password_md5_sale_1);  

  12.   

  13. String password_md5_sale_2 = new Md5Hash("111111", "uiwueylm", 1).toString();  

  14.   

  15. System.out.println("password_md5_sale_2="+password_md5_sale_2);  

  16.   

  17. //两次散列相当于md5(md5())  

  18.   

  19.    

  20.   

  21. //使用SimpleHash  

  22.   

  23. String simpleHash = new SimpleHash("MD5", "111111", "eteokues",1).toString();  

  24.   

  25. System.out.println(simpleHash);  

  26.   

  27.    


 

 

2.4.2 在realm中使用

 

实际应用是将盐和散列后的值存在数据库中,自动realm从数据库取出盐和加密后的值由shiro完成密码校验。

 

2.4.2.1 自定义realm

 

[java]view plain copy print? shiro教程(2)- shiro介绍  shiro教程(2)- shiro介绍

  1. @Override  

  2.   

  3. protected AuthenticationInfo doGetAuthenticationInfo(  

  4.   

  5. AuthenticationToken token) throws AuthenticationException {  

  6.   

  7. //用户账号  

  8.   

  9. String username = (String) token.getPrincipal();  

  10.   

  11. //根据用户账号从数据库取出盐和加密后的值  

  12.   

  13. //..这里使用静态数据  

  14.   

  15. //如果根据账号没有找到用户信息则返回null,shiro抛出异常“账号不存在”  

  16.   

  17. //按照固定规则加密码结果 ,此密码 要在数据库存储,原始密码 是111111,盐是eteokues  

  18.   

  19. String password = "cb571f7bd7a6f73ab004a70322b963d5";  

  20.   

  21. //盐,随机数,此随机数也在数据库存储  

  22.   

  23. String salt = "eteokues";  

  24.   

  25. //返回认证信息  

  26.   

  27. SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(  

  28.   

  29. username, password, ByteSource.Util.bytes(salt),getName());  

  30.   

  31.    

  32.   

  33. return simpleAuthenticationInfo;  

  34.   

  35. }  


 

2.4.2.2 realm配置

 

配置shiro-cryptography.ini

 

[html]view plain copy print? shiro教程(2)- shiro介绍  shiro教程(2)- shiro介绍

  1. [main]  

  2.   

  3. #定义凭证匹配器  

  4.   

  5. credentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher  

  6.   

  7. #散列算法  

  8.   

  9. credentialsMatcher.hashAlgorithmName=md5  

  10.   

  11. #散列次数  

  12.   

  13. credentialsMatcher.hashIterations=1  

  14.   

  15.    

  16.   

  17. #将凭证匹配器设置到realm  

  18.   

  19. customRealm=com.sihai.shiro.authentication.realm.CustomRealm2  

  20.   

  21. customRealm.credentialsMatcher=$credentialsMatcher  

  22.   

  23. securityManager.realms=$customRealm  


 

2.4.2.3 测试代码

测试代码同上个章节,注意修改ini路径。


分享名称:shiro教程(2)-shiro介绍
网页链接:http://cdkjz.cn/article/jhgosh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220