【Azure 应用服务】App Service中,为Java应用配置自定义错误页面,禁用DELETE, PUT方法

简介: 【Azure 应用服务】App Service中,为Java应用配置自定义错误页面,禁用DELETE, PUT方法

问题定义

使用Azure应用服务(App Service),部署Java应用,使用Tomcat容器,如何自定义错误页面呢?同时禁用DELETE, PUT方法

 

解决办法

如何自定义错误页面呢?需要在 Java 的 web.xml 进行配置 error-page,具体内容如下:

<?xml version="1.0" encoding="utf-8" ?>
<web-app
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1" metadata-complete="true">
<display-name>Welcome to Tomcat</display-name>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<description>Welcome to Tomcat</description>
<error-page>
    <error-code>404</error-code>
    <location>/404.html</location>
</error-page>
<error-page>
    <error-code>403</error-code>
    <location>/403.html</location>
</error-page>
<error-page>
    <error-code>500</error-code>
    <location>/500.html</location>
</error-page>
</web-app>

 

禁用DELETE, PUT方法?Tomcat是默认禁用PUT和DELETE方法的,当使用PUT和DELETE的请求会返回405(Method not allowed)。同时也可以在项目中的web.xml中配置security-constraint内容。如:

<security-constraint>
    <web-resource-collection>
        <web-resource-name>restricted methods</web-resource-name>
        <url-pattern>/*</url-pattern>
        <http-method>GET</http-method>
        <http-method>POST</http-method>
        <http-method>TRACE</http-method>
        <http-method>OPTIONS</http-method>
    </web-resource-collection>
    <auth-constraint />
</security-constraint>
####排除Delete和PUT方法

元素说明

  • security-constraint:允许不通过编程就可以限制对应用某个资源的访问
  • web-resource-collection:标识需要限制访问的资源子集。可以定义URL模式和HTTP方法。如果不存在HTTP方法,就将安全约束应用于所有的方法
  • web-resource-name:是与受保护资源相关联的名称
  • http-method:可被赋予一个HTTP方法,比如GET和POST
  • auth-constraint:用于指定可以访问该资源集合的用户角色。如果没有指定auth-constraint元素,就将安全约束应用于所有角色

 

另一种思路:基于Java的应用部署在App Service in Windows上,使用的是IIS转发请求到Tomcat中,所以也可以在IIS的web.config中配置禁止PUT, DELETE等HTTP方法的访问。如下图配置后,使用PUT和DELETE的请求会返回502(Bad Gateway)。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
    </handlers>
    <httpPlatform processPath="%HOME%\site\wwwroot\bin\tomcat\bin\startup.bat"  arguments="-Djava.net.preferIPv4Stack=true -Dserver.port=%HTTP_PLATFORM_PORT% -jar &quot;%HOME%\site\wwwroot\springbootsample-1.0.0.jar&quot;">
      <environmentVariables>
        <environmentVariable name="CATALINA_OPTS" value="-Dport.http=%HTTP_PLATFORM_PORT%" />
        <environmentVariable name="CATALINA_HOME" value="%HOME%\site\wwwroot\bin\tomcat" />
        <environmentVariable name="JRE_HOME" value="D:\home\site\wwwroot\bin\java\jdk1.6.0_45\jre6" />  
        <environmentVariable name="JAVA_OPTS" value="-Djava.net.preferIPv4Stack=true" />
      </environmentVariables>
    </httpPlatform>
    
    <security>
        <requestFiltering>
            <verbs allowUnlisted="false">
                <add verb="GET" allowed="true"/>
            </verbs>
        </requestFiltering>
    </security>
  </system.webServer>
</configuration>

 

附录

如在配置自定义错误页面时,遇见没有工作的情况,可以考虑是否时web.xml和error页面放置的路径不正确而引起的。

如:web.xml放在D:\home\site\wwwroot\webapps\ROOT\WEB-INF路径下,错误页面是放在D:\home\site\wwwroot\webapps\ROOT路径下的

 

参考资料

web.xml中<security-constraint>安全认证标签说明: https://www.cnblogs.com/xiohao/p/10935004.html

Web 应用如何自定义错误页面: https://docs.azure.cn/zh-cn/articles/azure-operations-guide/app-service-web/aog-app-service-web-howto-customize-error-page

 

相关文章
|
20天前
|
安全 Java 应用服务中间件
【Azure 应用服务】App Service 默认页面暴露Tomcat版本信息,存在安全风险
【Azure 应用服务】App Service 默认页面暴露Tomcat版本信息,存在安全风险
|
20天前
|
应用服务中间件 Linux 网络安全
【Azure 应用服务】App Service for Linux 环境中为Tomcat页面修改默认的Azure 404页面
【Azure 应用服务】App Service for Linux 环境中为Tomcat页面修改默认的Azure 404页面
|
20天前
|
Java 应用服务中间件 Windows
【App Service for Windows】为 App Service 配置自定义 Tomcat 环境
【App Service for Windows】为 App Service 配置自定义 Tomcat 环境
|
20天前
|
Docker 容器
【Azure 应用服务】App Service for Container 无法拉取Docker Hub中的镜像替代方案
【Azure 应用服务】App Service for Container 无法拉取Docker Hub中的镜像替代方案
|
20天前
|
API C++
【Azure 应用服务】Azure Function App在部署时候遇见 503 ServiceUnavailable
【Azure 应用服务】Azure Function App在部署时候遇见 503 ServiceUnavailable
|
20天前
|
网络协议
【Azure 应用服务】Azure Data Factory中调用Function App遇见403 - Forbidden
【Azure 应用服务】Azure Data Factory中调用Function App遇见403 - Forbidden
|
20天前
|
SQL Java 数据库连接
【Azure 应用服务】Java ODBC代码中,启用 Managed Identity 登录 SQL Server 报错 Managed Identity authentication is not available
【Azure 应用服务】Java ODBC代码中,启用 Managed Identity 登录 SQL Server 报错 Managed Identity authentication is not available
|
20天前
|
API
【Azure 应用服务】当在Azure App Service的门户上 Log Stream 日志无输出,需要如何操作让其输出Application Logs呢?
【Azure 应用服务】当在Azure App Service的门户上 Log Stream 日志无输出,需要如何操作让其输出Application Logs呢?
|
20天前
|
Kubernetes Linux Docker
【Azure 应用服务】使用Docker Compose创建App Service遇见"Linux Version is too long. It cannot be more than 4000 characters"错误
【Azure 应用服务】使用Docker Compose创建App Service遇见"Linux Version is too long. It cannot be more than 4000 characters"错误
|
20天前
|
数据中心 容器
【Azure 应用服务】在创建App Service时,遇见“No available instances to satisfy this request. App Service is attempting to increase capacity.”错误
【Azure 应用服务】在创建App Service时,遇见“No available instances to satisfy this request. App Service is attempting to increase capacity.”错误