**服务器禁止不需要的 HTTP 方法## ** 目前项目进行交付时,测试方给到一个安全检测报告。其中有一个问题是:启用了不安全的“OPTIONS”HTTP 方法。给到的建议是禁用 WebDAV,或者禁止不需要的 HTTP 方法。由于测试服务器是Tomcat,而线上服务器为websphere,在网上找了许多资源,始终都是Tomcat的解决办法。现在记录一下websphere解决问题思路,给使用websphere的同学们提供一个参考。 首先是Tomcat的解决办法: 1.将web.xml协议改为
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
2.在web.xml中加入以下代码
<security-constraint>
<web-resource-collection>
<url-pattern>/*</url-pattern>
<http-method>PUT</http-method>
<http-method>DELETE</http-method>
<http-method>HEAD</http-method>
<http-method>OPTIONS</http-method>
<http-method>TRACE</http-method>
</web-resource-collection>
<auth-constraint></auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
重启服务器,postman用OPTIONS请求发现allow中已经没有上述代码中的请求方法。 tips:踩坑过程中,我只加入了第二步中的代码,亲测有效。
然后是websphere的解决办法: 解决思路是利用过滤器将我们不需要的,也就是检测出来不安全的请求方法过滤掉 1.创建一个自定义过滤器
package com.dxl.project.filter;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class MethodsFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(
HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
if ("GET".equals(request.getMethod())||"POST".equals(request.getMethod())) {
filterChain.doFilter(request, response);
}
}
}
2.在web.xml中配置过滤器,我将这个过滤器放在了所有过滤器的最上面
<!-- http请求方法过滤器 -->
<filter>
<filter-name>methodsFilter</filter-name>
<filter-class>com.dxl.project.filter.MethodsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>methodsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
重启服务器,postman用OPTIONS请求,发现Headers中并无allow。 在查找资料的过程中我发现有用weblogic的小伙伴也有遇到相同的问题,或许能提供一种思路。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/1993.html