如何在tomcat6中使用哈希密码和BASIC身份验证?

时间:2011-06-09 21:06:50

标签: tomcat hash basic-authentication sha

我想在tomcat-users.xml中使用哈希密码和BASIC身份验证。我在领域定义中添加了digest =“SHA”。 UserDatabase部分在$ TOMCAT_HOME / conf / server.xml中定义:

<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
          type="org.apache.catalina.UserDatabase"
          description="User database that can be updated and saved"
          factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
          pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
...
<Engine>
...
      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
             digest="SHA"
             resourceName="UserDatabase" />  
...
</Engine>

但重新启动后,tomcat仍将$ TOMCAT_HOME / conf / tomcat-users.xml密码中定义的所有密码视为纯文本密码。

<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<user username="guest" password="e5e9fa1ba31ecd1ae84f75caaa474f3a663f05f4" roles="role1" />
</tomcat-users>

即。我可以使用用户名/密码guest / e5e9fa1ba31ecd1ae84f75caaa474f3a663f05f4登录,但不能使用guest / secret来登录。

请指出我做错了什么?

1 个答案:

答案 0 :(得分:0)

您的解决方案应该适用于Tomcat 7,但从Tomcat 8开始,您必须在CredentialHandler内指定Realm部分,如下所示:

Tomcat 6 ... 7:

      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
             digest="sha"
             resourceName="UserDatabase" />  

Tomcat 8:

      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
             resourceName="UserDatabase">
           <CredentialHandler
              className="org.apache.catalina.realm.MessageDigestCredentialHandler"     
              algorithm="sha" />
      </Realm>

注意:要获得更安全的哈希,例如PBKDF2WithHmacSHA512,请参阅this answer