分享

shiro加盐走源码

 Bladexu的文库 2018-07-11
一直不明白为什么shiro能根据用户的密码和预设定的散列次数以及盐来生成密码和已有密码来匹配?
于是有了整个测试:

测试环境:springboot+shiro
账号0324(今天是3月24号)
密码也是0324
唯一不同的就是我在shiroConfiguration里面给密码散列次数的不同。
数据库存入的密码只散列了2次,而Debug走的时候我散列了三次
基于这个场景,有了这一篇文章。
我会把shiroConfiguration整个类放在最下面



new一个认证信息,这里是根据用户名从数据库查询到的结果:
参数:实体类,密码,盐,自定义realm类(注意:这里是从数据库拿到的信息,所以密码和盐都是暗码)
然后走下面的return语句,返回这个认证信息:

这句走完后直接进入源码:
  1. public final AuthenticationInfo getAuthenticationInfo(AuthenticationToken token)
  2. /* */ throws AuthenticationException
  3. /* */ {
  4. /* 565 */ AuthenticationInfo info = getCachedAuthenticationInfo(token);
  5. /* 566 */ if (info == null)
  6. /* */ {
  7. /* 568 */ info = doGetAuthenticationInfo(token);一、
  8. /* 569 */ log.debug("Looked up AuthenticationInfo [{}] from doGetAuthenticationInfo", info);
  9. /* 570 */ if ((token != null) && (info != null))
  10. /* 571 */ cacheAuthenticationInfoIfPossible(token, info);二、
  11. /* */ }
  12. /* */ else {
  13. /* 574 */ log.debug("Using cached authentication info [{}] to perform credentials matching.", info);
  14. /* */ }
  15. /* */
  16. /* 577 */ if (info != null)
  17. /* 578 */ assertCredentialsMatch(token, info);三、这个是重点
  18. /* */ else {
  19. /* 580 */ log.debug("No AuthenticationInfo found for submitted AuthenticationToken [{}]. Returning null.", token);
  20. /* */ }
  21. /* */
  22. /* 583 */ return info;
  23. /* */ }
一、doGetAuthenticationInfo(token)方法


这个token是用户输入的信息

token的信息:用户输入的:


inof认证信息:从数据库查询到的:

二、方法:cacheAuthenticationInfoIfPossible(token, info)方法:
  1. /* */ private void cacheAuthenticationInfoIfPossible(AuthenticationToken token, AuthenticationInfo info)
  2. /* */ {
  3. /* 506 */ if (!(isAuthenticationCachingEnabled(token, info))) {
  4. /* 507 */ log.debug("AuthenticationInfo caching is disabled for info [{}]. Submitted token: [{}].", info, token);
  5. /* */
  6. /* 509 */ return;
  7. /* */ }
  8. /* */
  9. /* 512 */ Cache cache = getAvailableAuthenticationCache();
  10. /* 513 */ if (cache != null) {
  11. /* 514 */ Object key = getAuthenticationCacheKey(token);
  12. /* 515 */ cache.put(key, info);
  13. /* 516 */ log.trace("Cached AuthenticationInfo for continued authentication. key=[{}], value=[{}].", key, info);
  14. /* */ }
  15. /* */ }
  16. 进入方法:
  17. isAuthenticationCachingEnabled(token, info)
  18. /* */ protected boolean isAuthenticationCachingEnabled(AuthenticationToken token, AuthenticationInfo info)
  19. /* */ {
  20. /* 536 */ return isAuthenticationCachingEnabled();
  21. /* */ }
三、方法assertCredentialsMatch(token, info);
这里的token和info的信息都在上面截图:
token是用户输入的信息
info是认证信息那里从数据库获取到的信息
  1. /* */ protected void assertCredentialsMatch(AuthenticationToken token, AuthenticationInfo info)
  2. /* */ throws AuthenticationException
  3. /* */ {
  4. /* 595 */ CredentialsMatcher cm = getCredentialsMatcher();一、
  5. /* 596 */ if (cm != null) {
  6. /* 597 */ if (cm.doCredentialsMatch(token, info))二、这个方法更重要,正式比较开始了
  7. /* */ return;
  8. /* 599 */ String msg = "Submitted credentials for token [" + token + "] did not match the expected credentials.";
  9. /* 600 */ throw new IncorrectCredentialsException(msg);
  10. /* */ }
  11. /* */
  12. /* 603 */ throw new AuthenticationException("A CredentialsMatcher must be configured in order to verify credentials during authentication. If you do not wish for credentials to be examined, you can configure an " + AllowAllCredentialsMatcher.class.getName() + " instance.");
  13. /* */ }
  14. 一、CredentialsMatcher cm = getCredentialsMatcher();
  15. 一、方法:重要的看下面图片:
  16. /* */ public CredentialsMatcher getCredentialsMatcher()
  17. /* */ {
  18. /* 194 */ return this.credentialsMatcher;
  19. /* */ }
  20. 二、cm.doCredentialsMatch(token, info) **********
  21. 二、看代码,这里的token和info还是咱们截图的信息
  22. /* */ public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info)
  23. /* */ {
  24. /* 379 */ Object tokenHashedCredentials = hashProvidedCredentials(token, info);二A
  25. /* 380 */ Object accountCredentials = getCredentials(info);
  26. /* 381 */ return equals(tokenHashedCredentials, accountCredentials);
  27. /* */ }
  28. 二AhashProvidedCredentials(token, info)
  29. 进入这个方法:
  30. /* */ protected Object hashProvidedCredentials(AuthenticationToken token, AuthenticationInfo info)
  31. /* */ {
  32. /* 403 */ Object salt = null;
  33. /* 404 */ if (info instanceof SaltedAuthenticationInfo) {
  34. /* 405 */ salt = ((SaltedAuthenticationInfo)info).getCredentialsSalt();二AA
  35. /* */
  36. /* */ }
  37. /* 408 */ else if (isHashSalted()) {
  38. /* 409 */ salt = getSalt(token);
  39. /* */ }
  40. /* */
  41. /* 412 */ return hashProvidedCredentials(token.getCredentials(), salt, getHashIterations());二AB
  42. /* */ }
  43. salt = ((SaltedAuthenticationInfo)info).getCredentialsSalt();二AA
  44. 这里你就会发现,为什么在info里面设置ByteSource了
  45. 进入二AA
  46. /* */ public ByteSource getCredentialsSalt()
  47. /* */ {
  48. /* 167 */ return this.credentialsSalt;
  49. /* */ }
  50. hashProvidedCredentials(token.getCredentials(), salt, getHashIterations());二AB
  51. 进入二AB
  52. 这三个参数的截图我放下面,大家看,其实第一个是用户输入的密码,第二个是上面算出的盐,第三个是散列次数三,
  53. 这几个参数里面除了密码是用户输入的,其他俩个参数都是shiroconfigura里面配置的
  54. 见图--我是盐jpg,我是散列次数jpg
  55. 我们进入该方法:
  56. /* */ protected Hash hashProvidedCredentials(Object credentials, Object salt, int hashIterations)
  57. /* */ {
  58. /* 444 */ String hashAlgorithmName = assertHashAlgorithmName();获取加密算法,md5
  59. 参数分别是:算法名字:md5 用户输入的密码:0324 盐还是那个截图 散列次数:3
  60. 为了更直观一点,我又截图了,见下面 重点图1,重点图2,重点图3,重点图4
  61. /* 445 */ return new SimpleHash(hashAlgorithmName, credentials, salt, hashIterations);见下面具体代码
  62. /* */ }
  63. 下面是SimpleHash方法:
  64. /* */ public SimpleHash(String algorithmName, Object source, Object salt)
  65. /* */ throws CodecException, UnknownAlgorithmException
  66. /* */ {
  67. /* 139 */ this(algorithmName, source, salt, 1);
  68. /* */ }
  69. /* */ 这里是源码部分,但都是空格我就删除了,大家可以自己debug进去看
  70. /* 167 */ if (!(StringUtils.hasText(algorithmName))) {
  71. /* 168 */ throw new NullPointerException("algorithmName argument cannot be null or empty.");
  72. /* */ }
  73. /* 170 */ this.algorithmName = algorithmName;
  74. /* 171 */ this.iterations = Math.max(1, hashIterations);
  75. /* 172 */ ByteSource saltBytes = null;
  76. /* 173 */ if (salt != null) {
  77. /* 174 */ saltBytes = convertSaltToBytes(salt);
  78. /* 175 */ this.salt = saltBytes;
  79. /* */ }
  80. /* 177 */ ByteSource sourceBytes = convertSourceToBytes(source);
  81. /* 178 */ hash(sourceBytes, saltBytes, hashIterations);
  82. /* */ }
  83. 这一步完毕后,它其实做的就是:
  84. 拿用户输入的密码与我们的配置文件的信息来匹配计算,来得到一个加密后的密码
  85. 这个时候让我们回到上面**********部分代码来看,大家搜索就可以找到,我为方便,把它们粘贴下来:
  86. 二、看代码,这里的token和info还是咱们截图的信息
  87. /* */ public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info)
  88. /* */ {
  89. /* 379 */ Object tokenHashedCredentials = hashProvidedCredentials(token, info);二A
  90. /* 380 */ Object accountCredentials = getCredentials(info);二B
  91. /* 381 */ return equals(tokenHashedCredentials, accountCredentials);二C
  92. /* */ }
  93. 我们进入二B来看:它其实就是将我们从数据库查到的暗码拿出来了!
  94. /* */ protected Object getCredentials(AuthenticationInfo info)
  95. /* */ {
  96. /* 345 */ Object credentials = info.getCredentials();
  97. /* */
  98. /* 347 */ byte[] storedBytes = toBytes(credentials);
  99. /* */
  100. /* 349 */ if ((credentials instanceof String) || (credentials instanceof char[]))
  101. /* */
  102. /* */ {
  103. /* 352 */ if (isStoredCredentialsHexEncoded())
  104. /* 353 */ storedBytes = Hex.decode(storedBytes);
  105. /* */ else {
  106. /* 355 */ storedBytes = Base64.decode(storedBytes);
  107. /* */ }
  108. /* */ }
  109. /* 358 */ AbstractHash hash = newHashInstance();
  110. /* 359 */ hash.setBytes(storedBytes);
  111. /* 360 */ return hash;
  112. /* */ }
  113. return equals(tokenHashedCredentials, accountCredentials);二C
  114. 真正比较开始了,上面我已经详细介绍了 tokenHashedCredentials是如何生成的。这里我们又获取到了数据库的密码,就开始比较了。
  115. 由此整个过程结束。
  116. 我会将这个代码截图下面,见图密码1,密码2
  117. 显然比较结果是错误的。走最初的源码
  118. /* 595 */ CredentialsMatcher cm = getCredentialsMatcher();
  119. /* 596 */ if (cm != null) {
  120. /* 597 */ if (cm.doCredentialsMatch(token, info))
  121. /* */ return;
  122. /* 599 */ String msg = "Submitted credentials for token [" + token + "] did not match the expected credentials.";
  123. /* 600 */ throw new IncorrectCredentialsException(msg);
  124. /* */ }
  125. 这个时候它就会把密码不正确的异常给出:整个过程我已经描述的很清楚了。

这个图片中,其实就是从shiroConfigure里面获取咱们配置的信息:
也就是:
加密方法是:md5
散列次数是:3




我是盐jpg

我是散列次数jpg


重点图1,


重点图2,


重点图3,


重点图4,



密码1

密码2


下面整个就是整个ShiroConfiguration了
  1. import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
  2. import org.apache.shiro.cache.ehcache.EhCacheManager;
  3. import org.apache.shiro.mgt.SecurityManager;
  4. import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
  5. import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
  6. import org.apache.shiro.web.mgt.CookieRememberMeManager;
  7. import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
  8. import org.apache.shiro.web.servlet.SimpleCookie;
  9. import org.springframework.context.annotation.Bean;
  10. import org.springframework.context.annotation.Configuration;
  11. import org.springframework.core.annotation.Order;
  12. import javax.servlet.Filter;
  13. import java.util.LinkedHashMap;
  14. import java.util.Map;
  15. @Configuration
  16. @Order(1)
  17. public class ShiroConfiguration {
  18. /**
  19. * ShiroFilterFactoryBean 处理拦截资源文件问题。
  20. * 注意:单独一个ShiroFilterFactoryBean配置是或报错的,以为在
  21. * 初始化ShiroFilterFactoryBean的时候需要注入:SecurityManager Filter Chain定义说明
  22. * 1、一个URL可以配置多个Filter,使用逗号分隔 2、当设置多个过滤器时,全部验证通过,才视为通过
  23. * 3、部分过滤器可指定参数,如perms,roles
  24. */
  25. @Bean
  26. public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
  27. ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
  28. // 必须设置 SecurityManager
  29. shiroFilterFactoryBean.setSecurityManager(securityManager);
  30. //验证码过滤器
  31. Map<String, Filter> filtersMap = shiroFilterFactoryBean.getFilters();
  32. KaptchaFilter kaptchaFilter = new KaptchaFilter();
  33. filtersMap.put("kaptchaFilter", kaptchaFilter);
  34. //实现自己规则roles,这是为了实现or的效果
  35. RoleFilter roleFilter = new RoleFilter();
  36. filtersMap.put("roles", roleFilter);
  37. shiroFilterFactoryBean.setFilters(filtersMap);
  38. // 拦截器.
  39. //rest:比如/admins/user/**=rest[user],根据请求的方法,相当于/admins/user/**=perms[user:method] ,其中method为post,get,delete等。
  40. //port:比如/admins/user/**=port[8081],当请求的url的端口不是8081是跳转到schemal://serverName:8081?queryString,其中schmal是协议http或https等,serverName是你访问的host,8081是url配置里port的端口,queryString是你访问的url里的?后面的参数。
  41. //perms:比如/admins/user/**=perms[user:add:*],perms参数可以写多个,多个时必须加上引号,并且参数之间用逗号分割,比如/admins/user/**=perms["user:add:*,user:modify:*"],当有多个参数时必须每个参数都通过才通过,想当于isPermitedAll()方法。
  42. //roles:比如/admins/user/**=roles[admin],参数可以写多个,多个时必须加上引号,并且参数之间用逗号分割,当有多个参数时,比如/admins/user/**=roles["admin,guest"],每个参数通过才算通过,相当于hasAllRoles()方法。//要实现or的效果看http://zgzty.blog.163.com/blog/static/83831226201302983358670/
  43. //anon:比如/admins/**=anon 没有参数,表示可以匿名使用。
  44. //authc:比如/admins/user/**=authc表示需要认证才能使用,没有参数
  45. //authcBasic:比如/admins/user/**=authcBasic没有参数表示httpBasic认证
  46. //ssl:比如/admins/user/**=ssl没有参数,表示安全的url请求,协议为https
  47. //user:比如/admins/user/**=user没有参数表示必须存在用户,当登入操作时不做检查
  48. Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
  49. // 配置退出过滤器,其中的具体的退出代码Shiro已经替我们实现了
  50. filterChainDefinitionMap.put("/logout", "logout");
  51. //配置记住我或认证通过可以访问的地址
  52. filterChainDefinitionMap.put("/index", "user");
  53. filterChainDefinitionMap.put("/", "user");
  54. filterChainDefinitionMap.put("/login", "kaptchaFilter");
  55. // <!-- 过滤链定义,从上向下顺序执行,一般将 /**放在最为下边 -->:这是一个坑呢,一不小心代码就不好使了;
  56. filterChainDefinitionMap.put("/operation", "roles[admin,aix]");//测试权限页面
  57. //这段是配合 actuator框架使用的,配置相应的角色才能访问
  58. // filterChainDefinitionMap.put("/health", "roles[aix]");//服务器健康状况页面
  59. // filterChainDefinitionMap.put("/info", "roles[aix]");//服务器信息页面
  60. // filterChainDefinitionMap.put("/env", "roles[aix]");//应用程序的环境变量
  61. // filterChainDefinitionMap.put("/metrics", "roles[aix]");
  62. // filterChainDefinitionMap.put("/configprops", "roles[aix]");
  63. //开放的静态资源
  64. filterChainDefinitionMap.put("/favicon.ico", "anon");//网站图标
  65. filterChainDefinitionMap.put("/AdminLTE-2.3.7/**", "anon");//配置static文件下资源能被访问的,这是个例子
  66. filterChainDefinitionMap.put("/kaptcha.jpg", "anon");//图片验证码(kaptcha框架)
  67. filterChainDefinitionMap.put("/**", "authc");
  68. // 如果不设置默认会自动寻找Web工程根目录下的"/login.jsp"页面
  69. shiroFilterFactoryBean.setLoginUrl("/login");
  70. // 登录成功后要跳转的链接
  71. shiroFilterFactoryBean.setSuccessUrl("/index");
  72. // 未授权界面
  73. shiroFilterFactoryBean.setUnauthorizedUrl("/errorView/403_error.html");//不生效(详情原因看MyExceptionResolver)
  74. shiroFilterFactoryBean.setUnauthorizedUrl("/404");//访问页面,而该角色没有该页面的权限
  75. shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
  76. return shiroFilterFactoryBean;
  77. }
  78. @Bean
  79. public SecurityManager securityManager() {
  80. DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
  81. // 设置realm.
  82. securityManager.setRealm(myShiroRealm());
  83. //注入缓存管理器;
  84. //注意:开发时请先关闭,如不关闭热启动会报错
  85. //securityManager.setCacheManager(ehCacheManager());//这个如果执行多次,也是同样的一个对象;
  86. //注入记住我管理器;
  87. securityManager.setRememberMeManager(rememberMeManager());
  88. return securityManager;
  89. }
  90. /**
  91. * 身份认证realm; (这个需要自己写,账号密码校验;权限等)
  92. */
  93. @Bean
  94. public MyShiroRealm myShiroRealm() {
  95. MyShiroRealm myShiroRealm = new MyShiroRealm();
  96. myShiroRealm.setCredentialsMatcher(hashedCredentialsMatcher());
  97. return myShiroRealm;
  98. }
  99. /**
  100. * 凭证匹配器 (由于我们的密码校验交给Shiro的SimpleAuthenticationInfo进行处理了
  101. * 所以我们需要修改下doGetAuthenticationInfo中的代码; @return
  102. */
  103. @Bean
  104. public HashedCredentialsMatcher hashedCredentialsMatcher() {
  105. HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
  106. hashedCredentialsMatcher.setHashAlgorithmName("md5");// 散列算法:这里使用MD5算法;
  107. // hashedCredentialsMatcher.setHashIterations(2);// 散列的次数,比如散列两次,相当于md5(md5(""));
  108. hashedCredentialsMatcher.setHashIterations(3);// 散列的次数,比如散列两次,相当于md5(md5(""));
  109. hashedCredentialsMatcher.setStoredCredentialsHexEncoded(true);//表示是否存储散列后的密码为16进制,需要和生成密码时的一样,默认是base64;
  110. return hashedCredentialsMatcher;
  111. }
  112. /**
  113. * 开启shiro aop注解支持. 使用代理方式;所以需要开启代码支持;
  114. * @param securityManager
  115. * @return
  116. */
  117. @Bean
  118. public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
  119. AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
  120. authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
  121. return authorizationAttributeSourceAdvisor;
  122. }
  123. /**
  124. * shiro缓存管理器;
  125. * 需要注入对应的其它的实体类中:
  126. * 1、安全管理器:securityManager
  127. * 可见securityManager是整个shiro的核心;
  128. *
  129. * @return
  130. */
  131. @Bean
  132. public EhCacheManager ehCacheManager(){
  133. EhCacheManager cacheManager = new EhCacheManager();
  134. cacheManager.setCacheManagerConfigFile("classpath:config/ehcache-shiro.xml");
  135. return cacheManager;
  136. }
  137. /**
  138. * cookie对象;
  139. * @return
  140. * */
  141. @Bean
  142. public SimpleCookie rememberMeCookie(){
  143. //System.out.println("ShiroConfiguration.rememberMeCookie()");
  144. //这个参数是cookie的名称,对应前端的checkbox的name = rememberMe
  145. SimpleCookie simpleCookie = new SimpleCookie("rememberMe");
  146. //<!-- 记住我cookie生效时间30天 ,单位秒;-->
  147. simpleCookie.setMaxAge(259200);
  148. return simpleCookie;
  149. }
  150. /**
  151. * cookie管理对象;
  152. * @return
  153. */
  154. @Bean
  155. public CookieRememberMeManager rememberMeManager(){
  156. //System.out.println("ShiroConfiguration.rememberMeManager()");
  157. CookieRememberMeManager cookieRememberMeManager = new CookieRememberMeManager();
  158. cookieRememberMeManager.setCookie(rememberMeCookie());
  159. return cookieRememberMeManager;
  160. }
  161. }


结论:
读源码真的能明白很多东西,或许你刚开始并不知道它是做什么的,甚至可能你连单词都看不懂!
但是你要明白书读百遍其义自见的含义,哈哈。
希望可以帮到大家!

都是自己手动写的,或许有不对的地方,希望大神指出。需要源码的可以联系我。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多