【Azure 應用服務】App Service中,爲Java應用配置自定義錯誤頁面,禁用DELETE, PUT方法

問題定義

使用Azure應用服務(App Service),部署Java應用,使用Tomcat容器,如何自定義錯誤頁面呢?同時禁用DELETE, PUT方法html

 

解決辦法

如何自定義錯誤頁面呢?須要在 Java 的 web.xml 進行配置 error-page,具體內容以下:java

<?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內容。如:web

<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方法

元素說明spring

  • 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頁面放置的路徑不正確而引發的。tomcat

如:web.xml放在D:\home\site\wwwroot\webapps\ROOT\WEB-INF路徑下,錯誤頁面是放在D:\home\site\wwwroot\webapps\ROOT路徑下的安全

 

參考資料

web.xml中<security-constraint>安全認證標籤說明: http://www.javashuo.com/article/p-gmcpskci-ea.htmlspringboot

Web 應用如何自定義錯誤頁面: https://docs.azure.cn/zh-cn/articles/azure-operations-guide/app-service-web/aog-app-service-web-howto-customize-error-pageapp

相關文章
相關標籤/搜索