Shiro第二篇【介绍Shiro、认证流程、自定义realm、自定义realm支持md5】

Dear 丶 2021-09-17 07:32 628阅读 0赞

什么是Shiro

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

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

Shiro架构:

这里写图片描述

  • subject:主体,可以是用户也可以是程序,主体要访问系统,系统需要对主体进行认证、授权。
  • securityManager:安全管理器,主体进行认证和授权都 是通过securityManager进行。
  • authenticator:认证器,主体进行认证最终通过authenticator进行的。
  • authorizer:授权器,主体进行授权最终通过authorizer进行的。
  • sessionManager:web应用中一般是用web容器对session进行管理,shiro也提供一套session管理的方式。
  • SessionDao: 通过SessionDao管理session数据,针对个性化的session数据存储需要使用sessionDao。
  • cache Manager:缓存管理器,主要对session和授权数据进行缓存,比如将授权数据通过cacheManager进行缓存管理,和ehcache整合对缓存数据进行管理。
  • realm:域,领域,相当于数据源,通过realm存取认证、授权相关数据。

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

  • 比如md5散列算法。

为什么使用Shiro

我们在使用URL拦截的时候,要将所有的URL都配置起来,繁琐、不易维护

而我们的Shiro实现系统的权限管理,有效提高开发效率,从而降低开发成本。

Shiro认证

导入jar包

我们使用的是Maven的坐标就行了

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

当然了,我们也可以把Shiro相关的jar包全部导入进去

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

Shiro认证流程

这里写图片描述

通过配置文件创建工厂

这里写图片描述

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

这里写图片描述

小结

ModularRealmAuthenticator作用进行认证,需要调用realm查询用户信息(在数据库中存在用户信息)
ModularRealmAuthenticator进行密码对比(认证过程)。
realm:需要根据token中的身份信息去查询数据库(入门程序使用ini配置文件),如果查到用户返回认证信息,如果查询不到返回null

自定义realm

从第一个认证程序我们可以看见,我们所说的流程,是认证器去找realm去查询我们相对应的数据。而默认的realm是直接去与配置文件来比对的,一般地,我们在开发中都是让realm去数据库中比对。
因此,我们需要自定义realm

这里写图片描述

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

配置realm

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

这里写图片描述

测试自定义realm

同上边的入门程序,需要更改ini配置文件路径:

  1. 同上边的入门程序,需要更改ini配置文件路径:
  2. Factory<SecurityManager> factory = new IniSecurityManagerFactory(
  3. "classpath:shiro-realm.ini");

散列算法

我们如果知道md5,我们就会知道md5是不可逆的,但是如果设置了一些安全性比较低的密码:111111…即时是不可逆的,但还是可以通过暴力算法来得到md5对应的明文…

建议对md5进行散列时加salt(盐),进行加密相当 于对原始密码+盐进行散列。\

正常使用时散列方法:

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

测试:

  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. }

自定义realm支持md5

自定义realm

  1. public class CustomRealmMd5 extends AuthorizingRealm {
  2. // 设置realm的名称
  3. @Override
  4. public void setName(String name) {
  5. super.setName("customRealmMd5");
  6. }
  7. // 用于认证
  8. @Override
  9. protected AuthenticationInfo doGetAuthenticationInfo(
  10. AuthenticationToken token) throws AuthenticationException {
  11. // token是用户输入的
  12. // 第一步从token中取出身份信息
  13. String userCode = (String) token.getPrincipal();
  14. // 第二步:根据用户输入的userCode从数据库查询
  15. // ....
  16. // 如果查询不到返回null
  17. // 数据库中用户账号是zhangsansan
  18. /* * if(!userCode.equals("zhangsansan")){// return null; } */
  19. // 模拟从数据库查询到密码,散列值
  20. String password = "f3694f162729b7d0254c6e40260bf15c";
  21. // 从数据库获取salt
  22. String salt = "qwerty";
  23. //上边散列值和盐对应的明文:111111
  24. // 如果查询到返回认证信息AuthenticationInfo
  25. SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(
  26. userCode, password, ByteSource.Util.bytes(salt), 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. }

配置文件:

这里写图片描述

测试:

  1. // 自定义realm实现散列值匹配
  2. @Test
  3. public void testCustomRealmMd5() {
  4. // 创建securityManager工厂,通过ini配置文件创建securityManager工厂
  5. Factory<SecurityManager> factory = new IniSecurityManagerFactory(
  6. "classpath:shiro-realm-md5.ini");
  7. // 创建SecurityManager
  8. SecurityManager securityManager = factory.getInstance();
  9. // 将securityManager设置当前的运行环境中
  10. SecurityUtils.setSecurityManager(securityManager);
  11. // 从SecurityUtils里边创建一个subject
  12. Subject subject = SecurityUtils.getSubject();
  13. // 在认证提交前准备token(令牌)
  14. // 这里的账号和密码 将来是由用户输入进去
  15. UsernamePasswordToken token = new UsernamePasswordToken("zhangsan",
  16. "222222");
  17. try {
  18. // 执行认证提交
  19. subject.login(token);
  20. } catch (AuthenticationException e) {
  21. // TODO Auto-generated catch block
  22. e.printStackTrace();
  23. }
  24. // 是否认证通过
  25. boolean isAuthenticated = subject.isAuthenticated();
  26. System.out.println("是否认证通过:" + isAuthenticated);
  27. }

发表评论

表情:
评论列表 (有 0 条评论,628人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Shiro定义Realms

    Shiro 的三个核心组件:Subject, SecurityManager 和 Realms. 来回顾一下这三个组件的关系 ![Shiro 的三个核心组件][Shiro]