security - 如何在Tomcat中使用htpasswd保护?

标签 security tomcat .htpasswd security-constraint

我已经使用 Apache 的 htpasswd 命令创建了一个用户数据库文件。这个文件现在被其他几个应用程序使用,比如 apache 和 subversion。

中的用户是这样创建的:

htpasswd /path/to/users.htpasswd peter

这个用户文件是全局的,而不是每个目录。

如何让 Tomcat 6 使用同一个文件作为安全域?

最佳答案

与 htpasswd 最相似的可能是 MemoryRealm . 我自己找不到如何使用它的简单示例,所以我将在此处发布一个简单的示例代码:

  1. 在tomcat-users.xml中设置角色、用户名和密码

  2. 您的 web.xml 应该包含如下内容:

       <security-constraint>
         <web-resource-collection>
          <web-resource-name> 
            My Protected WebSite 
          </web-resource-name>
          <url-pattern> /* </url-pattern>
          <http-method> GET </http-method>
          <http-method> POST </http-method>
        </web-resource-collection>
        <auth-constraint>
        <!-- the same like in your tomcat-users.conf file -->
          <role-name> test </role-name>
        </auth-constraint>
      </security-constraint>
       <login-config>
        <auth-method> BASIC </auth-method>
        <realm-name>  Basic Authentication </realm-name>
      </login-config>
      <security-role>
        <description> Test role </description>
        <role-name> test </role-name>
      </security-role>
    
  3. 将此添加到您的 server.xml 文件中:

    <Realm className="org.apache.catalina.realm.MemoryRealm"></Realm>
    

关于security - 如何在Tomcat中使用htpasswd保护?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/593952/

相关文章:

.htaccess - 如何通过htaccess保护特定url

authentication - AuthUserFile 不工作,可能是什么问题?

git - 用于 git 标记验证和 git checkout 的组合命令?

java - Tomcat的性能压力测试

apache-flex - 使用 BlazeDS 将 Flex 应用程序连接到 Tomcat + Spring 3.0 Framework

tomcat - 在 Windows 中使用 Tomcat 运行 MyFaces 项目的问题 (java.lang.LinkageError)

.htaccess - 如何自动访问受 htpasswd 保护的页面

cocoa - 如何以编程方式允许访问我的应用程序的钥匙串(keychain)?

security - 在没有证书的情况下使用 SSL

php - Ajax 登录和 javascript cookie,这安全吗?