我已经使用 Apache 的 htpasswd 命令创建了一个用户数据库文件。这个文件现在被其他几个应用程序使用,比如 apache 和 subversion。
中的用户是这样创建的:
htpasswd /path/to/users.htpasswd peter
这个用户文件是全局的,而不是每个目录。
如何让 Tomcat 6 使用同一个文件作为安全域?
最佳答案
与 htpasswd 最相似的可能是 MemoryRealm . 我自己找不到如何使用它的简单示例,所以我将在此处发布一个简单的示例代码:
在tomcat-users.xml中设置角色、用户名和密码
您的 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>
将此添加到您的 server.xml 文件中:
<Realm className="org.apache.catalina.realm.MemoryRealm"></Realm>
关于security - 如何在Tomcat中使用htpasswd保护?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/593952/