• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

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

JAVA相关 路边两盏灯 2008次浏览 0个评论

问题定义

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

【Azure 应用服务】App Service中,为Java应用配置自定义错误页面,禁用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路径下的

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

 

参考资料

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

 


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明【Azure 应用服务】App Service中,为Java应用配置自定义错误页面,禁用DELETE, PUT方法
喜欢 (0)

您必须 登录 才能发表评论!

加载中……