【问题标题】:Does Apache Shiro support bCrypt?Apache Shiro 支持 bCrypt 吗?
【发布时间】:2015-11-22 08:11:01
【问题描述】:

Apache Shiro 身份验证框架是否支持使用 bCrypt 密码散列算法?如果没有,有没有办法让它与 Shiro 一起工作?

除了 Spring Security 之外,还有没有像 Shiro 这样支持 bCrypt 的其他身份验证框架?

【问题讨论】:

    标签: java shiro jbcrypt


    【解决方案1】:

    在 Apache Shiro JIRA (SHIRO-290) 上有一个关于此的开放功能请求。

    根据这个问题,会在1.3.0版本实现。

    【讨论】:

      【解决方案2】:

      我们的解决方案:(来自org.soluvas.security.shiro.BCryptPasswordService

      package org.soluvas.security.shiro;
      
      import org.apache.shiro.authc.AuthenticationInfo;
      import org.apache.shiro.authc.AuthenticationToken;
      import org.apache.shiro.authc.UsernamePasswordToken;
      import org.apache.shiro.authc.credential.CredentialsMatcher;
      import org.apache.shiro.authc.credential.HashingPasswordService;
      import org.apache.shiro.authc.credential.PasswordService;
      import org.apache.shiro.crypto.hash.Hash;
      import org.mindrot.jbcrypt.BCrypt;
      import org.soluvas.security.SecurityException;
      
      /**
       * Inspired by <a href="https://coderwall.com/p/ohycpq/using-bcrypt-with-shiro">Coderwall: Using BCrypt with Shiro</a>. Please vote for <a href="https://issues.apache.org/jira/browse/SHIRO-290">SHIRO-290</a>.
       *
       * <p>Requires:</p>
       *
       * <pre>{@code
       *     <dependency>
       *         <groupId>de.svenkubiak</groupId>
       *         <artifactId>jBCrypt</artifactId>
       *         <version>0.4.1</version>
       *     </dependency>
       * }</pre>
       *
       * <p>Usage:</p>
       *
       * <pre>{@code
       * @Bean
       * public JdbcRealm jdbcRealm() {
       *     final JdbcRealm jdbcRealm = new JdbcRealm();
       *     jdbcRealm.setDataSource(dataSource);
       *     // jdbcRealm.setAuthenticationQuery(Person2.SHIRO_AUTHENTICATION_QUERY);
       *     final PasswordMatcher passwordMatcher = new PasswordMatcher();
       *     passwordMatcher.setPasswordService(new BCryptPasswordService());
       *     jdbcRealm.setCredentialsMatcher(passwordMatcher);
       *     return jdbcRealm;
       * }
       * }</pre>
       */
      public class BCryptPasswordService implements PasswordService {
      
          @Override
          public String encryptPassword(Object plaintextPassword) throws IllegalArgumentException {
              final String str;
              if (plaintextPassword instanceof char[]) {
                  str = new String((char[]) plaintextPassword);
              } else if (plaintextPassword instanceof String) {
                  str = (String) plaintextPassword;
              } else {
                  throw new SecurityException("Unsupported password type: " + plaintextPassword.getClass().getName());
              }
              return BCrypt.hashpw(str, BCrypt.gensalt());
          }
      
          @Override
          public boolean passwordsMatch(Object submittedPlaintext, String encrypted) {
              return BCrypt.checkpw(new String((char[]) submittedPlaintext), encrypted);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2013-06-19
        • 2016-12-21
        • 1970-01-01
        • 2014-08-02
        • 1970-01-01
        • 2015-05-09
        • 2011-10-28
        • 2017-09-20
        • 2016-05-07
        相关资源
        最近更新 更多