将 HTTP 标头添加到 servlet 过滤器中的请求
2022-09-01 00:25:20
我正在与现有的servlet集成,该servlet从HTTP标头中提取一些属性。基本上,我正在实现一个无法访问实际请求的接口,它只能访问HTTP标头的k->v映射。
我需要传入一个请求参数。计划是使用 servlet 过滤器从参数转到标头值,但当然 HttpServletRequest 对象没有 addHeader() 方法。
有什么想法吗?
我正在与现有的servlet集成,该servlet从HTTP标头中提取一些属性。基本上,我正在实现一个无法访问实际请求的接口,它只能访问HTTP标头的k->v映射。
我需要传入一个请求参数。计划是使用 servlet 过滤器从参数转到标头值,但当然 HttpServletRequest 对象没有 addHeader() 方法。
有什么想法吗?
扩展 HttpServletRequestWrapper
,覆盖标头 getters 以返回参数:
public class AddParamsToHeader extends HttpServletRequestWrapper {
public AddParamsToHeader(HttpServletRequest request) {
super(request);
}
public String getHeader(String name) {
String header = super.getHeader(name);
return (header != null) ? header : super.getParameter(name); // Note: you can't use getParameterValues() here.
}
public Enumeration getHeaderNames() {
List<String> names = Collections.list(super.getHeaderNames());
names.addAll(Collections.list(super.getParameterNames()));
return Collections.enumeration(names);
}
}
..并用它包装原始请求:
chain.doFilter(new AddParamsToHeader((HttpServletRequest) request), response);
也就是说,我个人认为这是一个坏主意。而是让它直接访问参数或将参数传递给它。
正如 https://stackoverflow.com/users/89391/miku 所指出的,这将是一个完整的ServletFilter示例,它使用也适用于泽西岛的代码来添加remote_addr标头。
package com.bitplan.smartCRM.web;
import java.io.IOException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
/**
*
* @author wf
*
*/
public class RemoteAddrFilter implements Filter {
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HeaderMapRequestWrapper requestWrapper = new HeaderMapRequestWrapper(req);
String remote_addr = request.getRemoteAddr();
requestWrapper.addHeader("remote_addr", remote_addr);
chain.doFilter(requestWrapper, response); // Goes to default servlet.
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
// https://stackoverflow.com/questions/2811769/adding-an-http-header-to-the-request-in-a-servlet-filter
// http://sandeepmore.com/blog/2010/06/12/modifying-http-headers-using-java/
// http://bijubnair.blogspot.de/2008/12/adding-header-information-to-existing.html
/**
* allow adding additional header entries to a request
*
* @author wf
*
*/
public class HeaderMapRequestWrapper extends HttpServletRequestWrapper {
/**
* construct a wrapper for this request
*
* @param request
*/
public HeaderMapRequestWrapper(HttpServletRequest request) {
super(request);
}
private Map<String, String> headerMap = new HashMap<String, String>();
/**
* add a header with given name and value
*
* @param name
* @param value
*/
public void addHeader(String name, String value) {
headerMap.put(name, value);
}
@Override
public String getHeader(String name) {
String headerValue = super.getHeader(name);
if (headerMap.containsKey(name)) {
headerValue = headerMap.get(name);
}
return headerValue;
}
/**
* get the Header names
*/
@Override
public Enumeration<String> getHeaderNames() {
List<String> names = Collections.list(super.getHeaderNames());
for (String name : headerMap.keySet()) {
names.add(name);
}
return Collections.enumeration(names);
}
@Override
public Enumeration<String> getHeaders(String name) {
List<String> values = Collections.list(super.getHeaders(name));
if (headerMap.containsKey(name)) {
values.add(headerMap.get(name));
}
return Collections.enumeration(values);
}
}
}
网页.xml片段:
<!-- first filter adds remote addr header -->
<filter>
<filter-name>remoteAddrfilter</filter-name>
<filter-class>com.bitplan.smartCRM.web.RemoteAddrFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>remoteAddrfilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>