【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-pa 3ff8 ge> <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 "%HOME%\site\wwwroot\springbootsample-1.0.0.jar""> <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
- 【Azure 应用服务】在Azure App Service for Windows 中部署Java/NodeJS/Python项目时,web.config的配置模板内容
- tomcat的安全配置(禁用http方法,部署多个应用,启用从安全cookie,指定错误页面和显示信息)
- 【Azure App Service For Container】创建ASP.NET Core Blazor项目并打包为Linux镜像发布到Azure应用服务
- java配置错误页面方法
- 【应用服务 App Service】在Azure Web App的部署文件中,是否可以限制某些文件无法被访问?(如json)
- nginx配置禁用除GET|POST|PUT|DELETE之外的方法。
- 如果浏览的页面中应用了java脚本禁用了鼠标右键,解决的方法
- 自定义应用程序配置文件(app.config)_c#应用
- Android 用axis2 webService产生java.lang.ClassCastException: org.ksoap2.serialization.SoapPrimitive错误的解决方法
- ios 应用中直接跳转到指定app页面 或者 app 评价页面 的方法
- asp.net自定义错误处理页面的几种方法
- 01-自定义错误页面(jsp的基本应用)
- Microsoft Azure Remoteapp使用自定义镜像创建桌面服务
- 配置安装Apache主服务发生错误:"(OS 5)拒绝访问。 : AH00369: Failed to open the Windows service manager, perh······ "
- NGINX下配置404错误页面的方法
- WCF Ria Service“操作失败,指定的命名链接在配置中找不到”错误解决方法
- WCF 项目应用连载[4] - 自定义配置 扩展ServiceHost - LServiceHost
- android TimerTask 的简单应用,以及java.lang.IllegalStateException: TimerTask is scheduled already错误的解决方法
- JSP JAVA 自定义 错误页面(404,505,500)
- Android(java)学习笔记228:服务(service)之绑定服务调用服务里面的方法