1

我有一个 Tomcat 5.5 服务器,我在上面部署了一个应用程序。我有类似这样的http://tocatServer:8088/MyApp 我想通过输入用户名和密码来限制用户访问此地址,但我需要帮助。我知道我必须在 web.xml 上添加角色,我尝试过但没有成功。如果可能的话,我想发送这样的 URL http://username:password@tocatServer:8088/MyApp 这个 URL 是从 java swing 应用程序发送的,用于从 serlet 获取许可证 谢谢

4

1 回答 1

2

要限制对 Tomcat 中的 webapp(或 webapp URL 中的文件夹)的访问:

webapps/MyApp/WEB-INF/web.xml添加

<security-constraint>
    <web-resource-collection>
        <web-resource-name>
            Entire webapp
        </web-resource-name>
        <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>member</role-name>
    </auth-constraint>
</security-constraint>
<login-config>
    <!-- pay attention: BASIC in insecure, use it only for test, search for a more secure method -->
    <auth-method>BASIC</auth-method>
    <realm-name>Text reported when prompting the user for un and pw</realm-name>
</login-config>

conf/tomcat-users.xml添加

<role rolename="member"/>
<user username="bubi" password="bubi" roles="member"/>

然后重新加载 webapp 并重新启动 Tomcat。

来源:O'Reilly 的 Tomcat 十大配置技巧 - 5. 配置基本认证

关于第二个问题,我不知道如何实现。

于 2011-03-04T13:38:28.443 回答