Shiro之加密加盐及凭证验证

我就是我 2022-05-21 09:57 283阅读 0赞

MD5加密在Shiro中使用极其简单:

shiro中工具类:SimpleHash

  1. //SimpleHash构造器
  2. SimpleHash(String algorithmName, Object source, Object salt, int hashIterations)

参数解释:































参数名 参数解释 参数数据类型
algorithmName 加密类型[MD5、Md2、Sha1、Sha256等] String
source 要加密的对象 Object
salt 加盐对象,如果不打算加密时进行加盐则传null Object
hashIterations 对目标对象加密次数,次数越多可靠性越高。同时越复杂 int

使用示例:

  1. //SimpleHash加密
  2. SimpleHash simpleHash2 = new SimpleHash("MD5", "123456", salt, 0);
  3. //输出加密后结果[直接输出对象,或调用toString方法后就是加密结果]
  4. System.out.println(simpleHash2);

Shrio中可以通过修改实现了Realm接口的自定义Realm中的credentialsMatcher属性所对应的证书匹配器来使用加密设置。常用的是其实现类HashedCredentialsMatcher对象,在整合了Spring之后Spring的配置中如下:

  1. <!-- 配置自定义Realm,实现Realm接口或继承其实现类[例如:AuthenticatingRealm] -->
  2. <bean id="jdbcRealm" class="com.heiketu.shiro.realm.ShiroRealm">
  3. <!-- 配置自定义的凭证匹配器 -->
  4. <property name="credentialsMatcher">
  5. <!-- 内部构造:new SimpleHash(hashAlgorithmName, credentials, salt, hashIterations)对象 通过创建SimpleHash对象来进行加密 -->
  6. <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
  7. <!-- 采用MD5算法加密 -->
  8. <property name="hashAlgorithmName" value="MD5"></property>
  9. </bean>
  10. </property>
  11. </bean>
  12. <!-- 配置shiro的安全管理器 -->
  13. <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
  14. <property name="cacheManager" ref="cacheManager" />
  15. <!-- shiro中session相关配置 <property name="sessionMode" value="native" /> -->
  16. <property name="realm" ref="jdbcRealm" />
  17. </bean>
  18. <!-- shiro生命周期Post处理器 -->
  19. <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
  20. <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor" />
  21. <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
  22. <property name="securityManager" ref="securityManager" />
  23. </bean>
  24. <bean id="secureRemoteInvocationExecutor" class="org.apache.shiro.spring.remoting.SecureRemoteInvocationExecutor">
  25. <property name="securityManager" ref="securityManager" />
  26. </bean>
  27. <!-- shiro权限过滤:id要和web.xml中的ShiroFilter的filter-name一致[默认情况下, 如果不一致,spring会在filer的init-param中的targetBeanName配置参数中获取到对应的bean名称, 如果没有在容器中配置该bean,则会抛出NoSuchBeanDefinitionException异常] -->
  28. <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
  29. <property name="securityManager" ref="securityManager" />
  30. <!-- 登录页面 -->
  31. <property name="loginUrl" value="/login.jsp" />
  32. <!-- 登录成功页面 -->
  33. <property name="successUrl" value="/success.jsp" />
  34. <!-- 无权限页面 -->
  35. <property name="unauthorizedUrl" value="/unauthorized.jsp" />
  36. <!-- Shiro会通过Web.xml中的ShiroFilter过滤器调用该 id为shiroFilter的Bean,然后通过该Bean的property name为filterChainDefinitions中设置的value来 判断页面是否需要验证授权才能访问。 默认情况下,没有配置到value中的路径shiro不会拦截,而路径被标识为anon的则为 可以通过匿名访问[也就是可以直接访问],标识为authc则为需要授权才能访问的页面。 -->
  37. <property name="filterChainDefinitions">
  38. <value>
  39. <!-- anon:表示可以匿名访问 -->
  40. /login.jsp = anon
  41. /shiroRequest/login = anon
  42. /shiro/logout = logout
  43. <!-- authc:表示需要授权才可以访问 -->
  44. /** = authc
  45. </value>
  46. </property>
  47. </bean>

注:如果自定义Realm是继承自AuthenticatingRealm实现类,则需要实现:AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken arg0)抽象方法。

Shiro中doGetAuthenticationInfo方法的示例代码如下:

  1. @Override
  2. protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken arg0) throws AuthenticationException {
  3. //强转UsernamePasswordToken
  4. UsernamePasswordToken upToken = (UsernamePasswordToken)arg0;
  5. //用户名
  6. String username = upToken.getUsername();
  7. if("unkown".equals(username)) {
  8. throw new UnknownAccountException("未知用户");
  9. }
  10. //principal
  11. Object principal = username;
  12. //密码:加盐
  13. ByteSource bytes = ByteSource.Util.bytes("admin");
  14. String pass = new SimpleHash("MD5", "123456", bytes, 0).toString();
  15. String name2 = getName();
  16. //不加盐的设置
  17. //AuthenticationInfo info = new SimpleAuthenticationInfo(principal, pass, name2);
  18. AuthenticationInfo info = new SimpleAuthenticationInfo(username, pass, bytes, getName());
  19. return info;
  20. }

发表评论

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

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

相关阅读