如何在不更改 tomcat-users.xml 的情况下为静态 tomcat webapps 提供基本的 http 身份验证? [英] How do I provide basic http authentication for static tomcat webapps without changing tomcat-users.xml?
问题描述
我可以访问 tomcat 管理器并且可以上传战争文件.这些战争之一是静态 Web 项目(压缩的 html + 媒体文件,重命名为 *.war).我想在这场战争中添加一个 Web-INF/web.xml 文件,以使用基本的 http 身份验证来保护内容.
I have access to the tomcat manager and can upload war-files. One of these wars is a static web project (zipped html + media files, renamed to *.war). I want add a Web-INF/web.xml file to this war to protect the content with basic http auth.
我知道如何通过在 tomcat-users.xml
中添加全局用户和分配角色来做到这一点,但我想在我的 war 文件中定义所有用户名和密码.
I know how to do this by adding global users and assigning roles in the tomcat-users.xml
, but I want to have all usernames and passwords defined in my war-file.
- 这可以在不接触 tomcat 的
tomcat-users.xml
的情况下完成吗? - 如果是,我该如何在我的静态项目的
web.xml
中指定它?
- Can this be done without touching the tomcat's
tomcat-users.xml
? - And if yes, how do I specify this in my static project's
web.xml
?
谢谢,尤文
推荐答案
我在这里找到了解决方案:http://wiki.metawerx.net/wiki/SecuringYourSiteWithContainerManagedSecurity
I found a solution here: http://wiki.metawerx.net/wiki/SecuringYourSiteWithContainerManagedSecurity
该页面描述了如何定义您自己的META-INF/context.xml
指向您自己的WEB-INF/users.xml
.不幸的是,users.xml 文件的链接必须是绝对链接,我不想对配置文件中的操作系统/文件系统路径做出任何假设.
The page describes how to define your own META-INF/context.xml
pointing to your own WEB-INF/users.xml
. Unfortunately, the link to the users.xml file has to be absolute, and I do not want to make any assumptions on the OS/filesystem paths in my config files.
这是我当前的WEB-INF/web.xml
:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd"
version="2.5">
<display-name>SuperCoolTool</display-name>
<description>What an awesome app!</description>
<security-role>
<role-name>manager</role-name>
</security-role>
<security-role>
<role-name>keyuser</role-name>
</security-role>
<security-constraint>
<web-resource-collection>
<web-resource-name>
Entire Application
</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>keyuser</role-name>
<role-name>manager</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>Evaluation Area</realm-name>
</login-config>
</web-app>
匹配的 META-INF/context.xml
如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Realm className="org.apache.catalina.realm.MemoryRealm"
pathname="[PATH-TO-YOUR-WEBAPP]/WEB-INF/users.xml"/>
</Context>
这篇关于如何在不更改 tomcat-users.xml 的情况下为静态 tomcat webapps 提供基本的 http 身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!