zoukankan      html  css  js  c++  java
  • 【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%sitewwwrootin	omcatinstartup.bat"  arguments="-Djava.net.preferIPv4Stack=true -Dserver.port=%HTTP_PLATFORM_PORT% -jar &quot;%HOME%sitewwwrootspringbootsample-1.0.0.jar&quot;">
          <environmentVariables>
            <environmentVariable name="CATALINA_OPTS" value="-Dport.http=%HTTP_PLATFORM_PORT%" />
            <environmentVariable name="CATALINA_HOME" value="%HOME%sitewwwrootin	omcat" />
            <environmentVariable name="JRE_HOME" value="D:homesitewwwrootinjavajdk1.6.0_45jre6" />  
            <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:homesitewwwrootwebappsROOTWEB-INF路径下,错误页面是放在D:homesitewwwrootwebappsROOT路径下的

    参考资料

    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

  • 相关阅读:
    oracle序列
    json对象操作
    web项目中常出现的异常
    Builder设计模式
    getParameter("name")和 getParamterValues("name")的区别
    多条件查询生成sql语句
    安装pl/sql
    修复google浏览器
    创建JAVA项目的几个选项
    类的初始化顺序
  • 原文地址:https://www.cnblogs.com/lulight/p/14327072.html
Copyright © 2011-2022 走看看