常见web中间件 HTTP 限制请求方法 配置

前言

本文针对 IIS Tomcat Nginx OpenResty Weblogic WebSphere httpd

Nginx、OpenResty配置

OpenResty 是一个 以 Nginx 为核心的 Web 开发平台,其配置与Nginx相同。方式如下
配置nginx.conflocation 节点添加判断语句

server
{
	location /
	{
		proxy_pass http://xxxx;
		#白名单:仅允许 GETPOST
		if ($request_method !~ ^(GET|POST)$ ) {
			return 403;
		}
		# 黑名单:禁用 PUTDELETETRACEOPTIONS
		if ($request_method ~ ^(PUT|DELETE|TRACE|OPTIONS)$ ) {
			return 403;
		}
	}
}

IIS 配置

法(1):配置相关web.config<configuration>节点下添加以下security代码:

<system.webServer>
<!-- 省略其他配置,在最后添加以下配置即可-->
	<security>
		<requestFiltering>
			<verbs allowUnlisted="false">
				<add verb="GET" allowed="true"/>
				<add verb="POST" allowed="true"/>
				<add verb="HEAD" allowed="true"/>
			</verbs>
		</requestFiltering>
	</security>
</system.webServer>

以上代码只允许开启GET、POST和HEAD方法。
allowUnlisted="false"含义:拒绝未列出的谓词。
若想禁用某方法,allowed设置为false即可

<add verb="DEBUG" allowed="false" />

方法(2):找到要设置的项目 “请求筛选”–> “HTTP谓词”
设置允许谓词,并将允许未列出的谓词的选框的取消,图示如下:
此处设置将同步修改web.config。效果一致。在这里插入图片描述

Tomcat、JBOSS配置

修改 web.xml,在<session-config></session-config>节点后面新增<security-constraint>配置:
JBoss是在Tomcat的基础上,对其进行本地化,将Tomcat 以内嵌的方式集成到 JBoss 中,因此,设置方式可借鉴Tomcat方式
在应用的web.xml中配置如下信息:
通过 <auth-constraint/>进行限制1

白名单

<!-- ----白名单start---- -->
<security-constraint>
    <web-resource-collection>
<web-resource-name>WhiteList</web-resource-name>
        <url-pattern>/*</url-pattern>
        <http-method>GET</http-method>
        <http-method>POST</http-method>
    </web-resource-collection>
</security-constraint>
<!-- 限制其他方法访问 -->
<security-constraint>
    <web-resource-collection>
        <web-resource-name>RestrictedMethods</web-resource-name>
        <url-pattern>/*</url-pattern>
    </web-resource-collection>
   <auth-constraint/>
</security-constraint>
<!-- ----白名单end---- -->

黑名单

<!-- ----黑名单start---- -->
<security-constraint>
		<web-resource-collection>
			<web-resource-name>BlackList</web-resource-name>
			<url-pattern>/*</url-pattern>
			<http-method>PUT</http-method>
			<http-method>DELETE</http-method>
			<http-method>TRACE</http-method>
			<http-method>OPTIONS</http-method>
		</web-resource-collection>
		<auth-constraint/>
 </security-constraint> 
 <!-- ----黑名单end---- -->

WebLogic配置

配置参考官网2
白名单

<security-constraint>  
	<web-resource-collection>  
		<web-resource-name>whitelist</web-resource-name>  
		<url-pattern>/*</url-pattern>  
		<http-method>POST</http-method>  
		<http-method>GET</http-method>  
		<http-method>HEAD</http-method>  
	</web-resource-collection>  
</security-constraint>  
<security-constraint>  
	<web-resource-collection>  
		<web-resource-name>blacklist</web-resource-name>  
		<url-pattern>/*</url-pattern>  
	</web-resource-collection>  
	<auth-constraint></auth-constraint>  
</security-constraint>  

<login-config>
	<auth-method>BASIC</auth-method>
</login-config>

黑名单

<security-constraint>  
	<web-resource-collection>  
		<web-resource-name>blacklist</web-resource-name>  
		<url-pattern>/*</url-pattern>  
		<http-method>PUT</http-method>
		<http-method>DELETE</http-method>
		<http-method>TRACE</http-method>
		<http-method>OPTIONS</http-method>
	</web-resource-collection>  
	<auth-constraint></auth-constraint>  
</security-constraint>  

<login-config>
	<auth-method>BASIC</auth-method>
</login-config>

Apache httpd配置

在.htaccess文件中添加如下代码过滤OPTIONS、TRACE请求

RewriteEngine on
RewriteCond %{REQUEST_METHOD} ^(OPTIONS|TRACE|TRACK)
RewriteRule .* - [F]

  1. 参考:http://wiki.metawerx.net/wiki/Web.xml.AuthConstraint ↩︎

  2. 参考:https://docs.oracle.com/cd/E28280_01/web.1111/e13712/web_xml.htm#WBAPP549 ↩︎

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值