Shiro框架:Shiro简介、登陆认证入门程序、认证执行流程、使用自定义Realm进行登陆认证、Shiro的MD5散列算法

一、Shiro介绍:

1、什么是shiro:

(1)shiro是apache的一个开源框架,是一个权限管理的框架,实现用户认证、用户授权。

(2)spring中有spring security,是一个权限框架,但是它和spring依赖过于紧密,没有shiro使用简单。shiro不依赖于spring,shiro不仅可以实现 web应用的权限管理,还可以实现c/s系统,分布式系统权限管理,shiro属于轻量框架,越来越多企业项目开始使用shiro。

(3)使用shiro实现系统 的权限管理,有效提高开发效率,从而降低开发成本。

2、shiro架构:

(1)subject:主体,可以是用户也可以是程序,主体要访问系统,系统需要对主体进行认证、授权。

(2)securityManager:安全管理器,主体进行认证和授权都 是通过securityManager进行。

(3)authenticator:认证器,主体进行认证最终通过authenticator进行的。

(4)authorizer:授权器,主体进行授权最终通过authorizer进行的。

(5)sessionManager:web应用中一般是用web容器对session进行管理,shiro也提供一套session管理的方式。

(6)SessionDao:  通过SessionDao管理session数据,针对个性化的session数据存储需要使用sessionDao。

(7)cache Manager:缓存管理器,主要对session和授权数据进行缓存,比如将授权数据通过cacheManager进行缓存管理,和ehcache整合对缓存数据进行管理。

(8)realm:域,领域,相当于数据源,通过realm存取认证、授权相关数据。在realm中存储授权和认证的逻辑。

(9)cryptography:密码管理,提供了一套加密/解密的组件,方便开发。比如提供常用的散列、加/解密等功能。比如 md5散列算法。

3、相关jar包依赖:

与其它java开源框架类似,将shiro的jar包加入项目就可以使用shiro提供的功能了。shiro-core是核心包必须选用,还提供了与web整合的shiro-web、与spring整合的shiro-spring、与任务调度quartz整合的shiro-quartz等


 
 
  1. <dependency>
  2. <groupId>org.apache.shiro </groupId>
  3. <artifactId>shiro-core </artifactId>
  4. <version>1.4.0 </version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.shiro </groupId>
  8. <artifactId>shiro-web </artifactId>
  9. <version>1.4.0 </version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.apache.shiro </groupId>
  13. <artifactId>shiro-spring </artifactId>
  14. <version>1.4.0 </version>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.apache.shiro </groupId>
  18. <artifactId>shiro-ehcache </artifactId>
  19. <version>1.4.0 </version>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.apache.shiro </groupId>
  23. <artifactId>shiro-quartz </artifactId>
  24. <version>1.4.0 </version>
  25. </dependency>

也可以通过引入shiro-all包括shiro所有的包:


 
 
  1. <dependency>
  2. <groupId>org.apache.shiro </groupId>
  3. <artifactId>shiro-all </artifactId>
  4. <version>1.4.0 </version>
  5. </dependency>

 

 

二、Shiro认证入门程序搭建:

1、shiro认证流程:

2、导入jar包依赖:shiro-core.jar

3、工程结构:

4、编写shiro-first.ini配置文件:

通过此配置文件创建securityManager工厂。


 
 
  1. #对用户信息进行配置
  2. [ users]
  3. #用户账户和密码
  4. zhangsan= 111111
  5. lisi= 222222

5、Authentication类:


 
 
  1. //shiro入门程序测试类:
  2. public class Authentication {
  3. //用户登陆和退出测试
  4. @Test
  5. public void testLogin(){
  6. //1、创建securityManager工厂,通过ini配置文件创建securityManager工厂
  7. Factory<SecurityManager> factory=
  8. new IniSecurityManagerFactory( "classpath:shiro-first.ini");
  9. //2、创建securityManager
  10. SecurityManager securityManager=factory.getInstance();
  11. //3、将SecurityManager设置在当前运行环境中
  12. SecurityUtils.setSecurityManager(securityManager);
  13. //4、从SecurityUtils里面 创建一个subject;
  14. Subject subject=SecurityUtils.getSubject();
  15. //5、在认证提交前准备token(令牌)
  16. UsernamePasswordToken token= new UsernamePasswordToken( "zhangsan", "111111");
  17. try{
  18. //6、执行认证提交;
  19. subject.login(token);
  20. } catch (AuthenticationException e) {
  21. e.printStackTrace();
  22. }
  23. //7、是否认证通过
  24. boolean isAuthenticated = subject.isAuthenticated();
  25. System.out.println( "是否认证通过:"+isAuthenticated);
  26. //8、退出操作
  27. subject.logout();
  28. isAuthenticated = subject.isAuthenticated();
  29. System.out.println( "是否认证通过:"+isAuthenticated);
  30. }
  31. }

6、运行结果:

至此,一个简单的shiro入门程序就搭建完成了。

 

 

三、shiro的执行流程:

1、通过ini配置文件创建securityManager;

2、调用subject.login方法主体提交认证,提交的token;

3、securityManager进行认证,securityManager最终由ModularRealmAuthenticator进行认证;

4、ModularRealmAuthenticator调用IniRealm(给realm传入token) 去ini配置文件中查询用户信息;

5、IniRealm根据输入的token(UsernamePasswordToken)从 shiro-first.ini查询用户信息,根据账号查询用户信息(账号和密码):

(1)如果查询到用户信息,就给ModularRealmAuthenticator返回用户信息(账号和密码)

(2)如果查询不到,就给ModularRealmAuthenticator返回null

6、ModularRealmAuthenticator接收IniRealm返回Authentication认证信息

(1)如果返回的认证信息是null,ModularRealmAuthenticator抛出异常(org.apache.shiro.authc.UnknownAccountException

(2)如果返回的认证信息不是null(说明inirealm找到了用户),对IniRealm返回用户密码 (在ini文件中存在)和 token中的密码 进行对比,如果不一致抛出异常(org.apache.shiro.authc.IncorrectCredentialsException

小结:

ModularRealmAuthenticator的作用是进行认证,需要调用realm查询用户信息(在数据库中存在用户信息),

ModularRealmAuthenticator进行密码对比(认证过程)。

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

 

 

四、自定义Reaml进行用户认证:

实际开发需要realm从数据库中查询用户信息。

1、继承realm接口:

2、自定义realm的示例:

工程结构:

3、自定义realm:


 
 
  1. //自定义的Realm,需要继承AuthorizingRealm
  2. public class CustomRealm extends AuthorizingRealm{
  3. // 设置realm的名称
  4. @Override
  5. public void setName(String name) {
  6. super.setName( "customRealm");
  7. }
  8. // 用于认证
  9. @Override
  10. protected AuthenticationInfo doGetAuthenticationInfo(
  11. AuthenticationToken token) throws AuthenticationException {
  12. // token是用户输入的
  13. // 第一步从token中取出身份信息
  14. String userCode = (String) token.getPrincipal();
  15. // 第二步:根据用户输入的userCode从数据库查询
  16. // ....
  17. // 如果查询不到返回null
  18. //这个例子中假设数据库中用户账号是zhangsansan
  19. if(!userCode.equals( "zhangsan")){ //
  20. return null;
  21. }
  22. // 模拟从数据库查询到密码是111111
  23. String password = "111111";
  24. // 如果查询到返回认证信息AuthenticationInfo
  25. SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(
  26. userCode, password, this.getName());
  27. return simpleAuthenticationInfo;
  28. }
  29. // 用于授权
  30. @Override
  31. protected AuthorizationInfo doGetAuthorizationInfo(
  32. PrincipalCollection principals) {
  33. // TODO Auto-generated method stub
  34. return null;
  35. }
  36. }

4、配置realm:

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


 
 
  1. [main]
  2. #自定义的realm
  3. customRealm=com.zwp.shiro.realm.CustomRealm
  4. #将realm设置到securityManager,相当于spring中的注入
  5. securityManager.realms=$customRealm

5、测试:


 
 
  1. @Test
  2. public void testCustomRealm() {
  3. //1、创建securityManager工厂,通过ini配置文件创建securityManager工厂
  4. Factory<SecurityManager> factory=
  5. new IniSecurityManagerFactory( "classpath:shiro-realm.ini");
  6. //2、创建securityManager
  7. SecurityManager securityManager=factory.getInstance();
  8. //3、将SecurityManager设置在当前运行环境中
  9. SecurityUtils.setSecurityManager(securityManager);
  10. //4、从SecurityUtils里面 创建一个subject;
  11. Subject subject=SecurityUtils.getSubject();
  12. //5、在认证提交前准备token(令牌)
  13. UsernamePasswordToken token= new UsernamePasswordToken( "zhangsan", "111111");
  14. try{
  15. //6、执行认证提交;
  16. subject.login(token);
  17. } catch (AuthenticationException e) {
  18. e.printStackTrace();
  19. }
  20. //7、是否认证通过
  21. boolean isAuthenticated = subject.isAuthenticated();
  22. System.out.println( "是否认证通过:"+isAuthenticated);
  23. //8、退出操作
  24. subject.logout();
  25. isAuthenticated = subject.isAuthenticated();
  26. System.out.println( "是否认证通过:"+isAuthenticated);
  27. }

6、测试结果:

至此,Realm的配置就完成了。

 

 

五、Shiro的MD5加密算法:

1、散列算法:

在项目中,通常需要对密码进行散列,常用的有MD5、SHA。

(1)对md5密码,如果知道散列后的值可以通过穷举算法,得到md5密码对应的明文。因此,建议对md5进行散列时加salt(盐),进行加密相当于对原始密码+盐进行散列。

(2)正常使用时散列方法:

在程序中对原始密码+盐进行散列,将散列值存储到数据库中,并且还要将盐也要存储在数据库中。

(3)如果进行密码对比时,使用相同方法,将原始密码+盐进行散列,进行比对。

2、MD5散列测试程序:


 
 
  1. public class MD5Test {
  2. public static void main(String[] args) {
  3. //原始 密码
  4. String source = "111111";
  5. //盐
  6. String salt = "qwerty";
  7. //散列次数
  8. int hashIterations = 2;
  9. //上边散列1次:f3694f162729b7d0254c6e40260bf15c
  10. //上边散列2次:36f2dfa24d0a9fa97276abbe13e596fc
  11. //构造方法中:
  12. //第一个参数:明文,原始密码
  13. //第二个参数:盐,通过使用随机数
  14. //第三个参数:散列的次数,比如散列两次,相当 于md5(md5(''))
  15. Md5Hash md5Hash = new Md5Hash(source, salt, hashIterations);
  16. String password_md5 = md5Hash.toString();
  17. System.out.println(password_md5);
  18. //第一个参数:散列算法
  19. SimpleHash simpleHash = new SimpleHash( "md5", source, salt, hashIterations);
  20. System.out.println(simpleHash.toString());
  21. }
  22. }

3、自定义realm支持散列算法:

需求:实际开发时,realm要进行MD5值(明文散列后的值)的对比;

(1)项目结构:

(2)新建realm:(CustomRealmMd5.java)


 
 
  1. //自定义realm支持散列算法:
  2. public class CustomRealmMd5 extends AuthorizingRealm {
  3. // 设置realm的名称
  4. @Override
  5. public void setName(String name) {
  6. super.setName( "customRealmMd5");
  7. }
  8. @Override
  9. protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
  10. // token是用户输入的
  11. // 第一步从token中取出身份信息
  12. String userCode = (String) token.getPrincipal();
  13. // 第二步:根据用户输入的userCode从数据库查询
  14. // ....
  15. // 如果查询不到返回null
  16. // 数据库中用户账号是zhangsansan
  17. if(!userCode.equals( "zhangsan")){
  18. return null;
  19. }
  20. // 模拟从数据库查询到密码,散列值
  21. String password = "13f79dafcbbedc313273e2b891ac84d3";
  22. // 从数据库获取salt
  23. String salt = "qwerty";
  24. //上边散列值和盐对应的明文:123456 散列次数2
  25. // 如果查询到返回认证信息AuthenticationInfo
  26. SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(
  27. userCode, password, ByteSource.Util.bytes(salt), this.getName());
  28. return simpleAuthenticationInfo;
  29. }
  30. @Override
  31. protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
  32. return null;
  33. }
  34. }

(3)在realm的ini文件中配置凭证匹配器:


 
 
  1. [main]
  2. #定义凭证匹配器:
  3. credentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher
  4. #散列算法:
  5. credentialsMatcher.hashAlgorithmName=md5
  6. #散列次数:
  7. credentialsMatcher.hashIterations= 2
  8. #将凭证匹配器设置到realm
  9. customRealm=com.zwp.shiro.realm.CustomRealmMd5
  10. customRealm.credentialsMatcher=$credentialsMatcher
  11. securityManager.realms=$customRealm

(4)测试类:


 
 
  1. //自定义realm支持散列算法测试:
  2. @Test
  3. public void testCustomRealmMd5() {
  4. //1、创建securityManager工厂,通过ini配置文件创建securityManager工厂
  5. Factory<SecurityManager> factory=
  6. new IniSecurityManagerFactory( "classpath:shiro-realm-md5.ini");
  7. //2、创建securityManager
  8. SecurityManager securityManager=factory.getInstance();
  9. //3、将SecurityManager设置在当前运行环境中
  10. SecurityUtils.setSecurityManager(securityManager);
  11. //4、从SecurityUtils里面 创建一个subject;
  12. Subject subject=SecurityUtils.getSubject();
  13. //5、在认证提交前准备token(令牌)
  14. UsernamePasswordToken token= new UsernamePasswordToken( "zhangsan", "123456");
  15. try{
  16. //6、执行认证提交;
  17. subject.login(token);
  18. } catch (AuthenticationException e) {
  19. e.printStackTrace();
  20. }
  21. //7、是否认证通过
  22. boolean isAuthenticated = subject.isAuthenticated();
  23. System.out.println( "是否认证通过:"+isAuthenticated);
  24. //8、退出操作
  25. subject.logout();
  26. isAuthenticated = subject.isAuthenticated();
  27. System.out.println( "是否认证通过:"+isAuthenticated);
  28. }

至此,自定义realm支持散列算法的就完成了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值