我有一个 Tomcat 5.5 服务器,我在上面部署了一个应用程序。 我有这样的东西http://tocatServer:8088/MyApp 我想通过输入用户名和密码来限制用户对该地址的访问,但我需要帮助。我知道我必须在 web.xml 上添加角色,我试过了但没有成功。 如果可能的话,我想像这样发送 URL http://username:password@tocatServer:8088/MyApp 此 url 是从 java swing 应用程序发送的,用于从 serlet 获取许可证 谢谢
最佳答案
在 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"/>
然后重新加载 web 应用程序,也可能重新启动 Tomcat。
来源:O'Reilly's Top Ten Tomcat Configuration Tips - 5. Configuring Basic Authentication
关于第二个问题,我不知道怎么实现。
关于security - 在 Tomcat 5.5 应用程序上配置用户名和密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4163414/