仅针对特定URL的Tomcat基本身份验证

时间:2019-12-04 08:40:45

标签: jsp tomcat servlets server xampp

我正在使用JSP和servlet。我想在同一服务器上部署两个Web应用程序,但使用不同的URL。

我需要使用不同的用户名和密码为两个Web应用程序设置基本身份验证

我已经在tomcat中配置了以下内容。

在Web.xml文件中

<web-app>
<security-constraint>
    <web-resource-collection>
        <web-resource-name>timesheet</web-resource-name>
        <url-pattern>/timesheet</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>timesheetuser</role-name>
    </auth-constraint>
</security-constraint>
<login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>user password required. Producted by Udhayakumar</realm-name>
</login-config>


<security-constraint>
    <web-resource-collection>
        <web-resource-name>test</web-resource-name>
        <url-pattern>/test</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>testuser</role-name>
    </auth-constraint>
</security-constraint>
<login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>user password required. Producted by Udhayakumar</realm-name>
</login-config>
</web-app>

在tomcat-user.xml文件中

<tomcat-users>
<role rolename="timesheet"/>
<role rolename="test"/>
<user username="timesheetuser" password="test123" roles="timesheet"/>
<user username="testuser" password="test123" roles="test"/>
</tomcat-users>

Tomcat Web应用程序管理器

Tomcat Web Application Manager

如果我在浏览器中打开此URL,而不要求用户名和密码。它是直接打开

  • localhost:8080 / timesheet
  • localhost:8080 / test

我想在时间表和测试应用程序中使用其他用户和密码。上面的方法对我不起作用。 有什么错误吗?

1 个答案:

答案 0 :(得分:0)

在Web.xml中

    <security-constraint>

    <web-resource-collection>
        <web-resource-name>Wildcard means whole app requires authentication</web-resource-name>
        <url-pattern>/*</url-pattern>
        <http-method>GET</http-method>
        <http-method>POST</http-method>
    </web-resource-collection>
    <auth-constraint>
        <role-name>testuser</role-name>
    </auth-constraint>

    <user-data-constraint>
        <transport-guarantee>NONE</transport-guarantee>
    </user-data-constraint>

    </security-constraint>