前言
本文针对 IIS Tomcat Nginx OpenResty Weblogic WebSphere httpd
Nginx、OpenResty配置
OpenResty 是一个 以 Nginx 为核心的 Web 开发平台,其配置与Nginx相同。方式如下
配置nginx.conf
在location
节点添加判断语句
server
{
location /
{
proxy_pass http://xxxx;
#白名单:仅允许 GET、POST
if ($request_method !~ ^(GET|POST)$ ) {
return 403;
}
# 黑名单:禁用 PUT、DELETE、TRACE、OPTIONS
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]