要禁止访问某个页面,可以在Tomcat的web.xml文件中添加一个安全约束(security constraint)。

  1. 找到Tomcat安装目录下的conf/web.xml文件,然后在其中添加以下代码:
<security-constraint>
    <web-resource-collection>
        <web-resource-name>Restricted Area</web-resource-name>
        <url-pattern>/restricted/*</url-pattern>
    </web-resource-collection>
    <auth-constraint/>
</security-constraint>

这个配置将禁止访问以“/restricted/”开头的所有URL。

  1. 接下来,需要在Tomcat的conf/tomcat-users.xml文件中添加一个用户,以便进行认证。例如:
<role rolename="admin"/>
<user username="admin" password="123456" roles="admin"/>

这个配置将创建一个名为“admin”的用户,密码为“123456”,角色为“admin”。

  1. 最后,在web.xml文件中添加一个安全约束,以确保只有拥有“admin”角色的用户才能访问受限页面:
<security-constraint>
    <web-resource-collection>
        <web-resource-name>Restricted Area</web-resource-name>
        <url-pattern>/restricted/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>admin</role-name>
    </auth-constraint>
</security-constraint>

保存并重启Tomcat,现在只有拥有“admin”角色的用户才能访问受限页面。

tomcat配置禁止访问某个页面

原文地址: https://www.cveoy.top/t/topic/zcG 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录