gpt4 book ai didi

spring - 通过过滤器向请求添加自定义 header

转载 作者:行者123 更新时间:2023-12-04 14:39:10 28 4
gpt4 key购买 nike

我有一个扩展 Filter 的类类,它看起来像:

@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CorsFilter implements Filter {

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
System.out.println("--------------------------------------");

HttpServletRequest request = (HttpServletRequest) req;

req.setAttribute("test", "test");


final HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Allow-Headers", "Authorization, Content-Type");
response.setHeader("Access-Control-Max-Age", "3600");
if ("OPTIONS".equalsIgnoreCase(((HttpServletRequest) req).getMethod())) {
response.setStatus(HttpServletResponse.SC_OK);
} else {
chain.doFilter(req, res);
}
}

问题是我可以添加自定义 headerrequest ?我经历了所有可用的方法 requestreq并且找不到任何方法。但在 debug模式我发现里面 CoyoteRequestheaders的列表.我如何在那里添加我的自定义 header ?

enter image description here

最佳答案

您不能在 HttpServletRequest 中设置标题对象,但您可以使用包装类。

看看这个指南:http://wilddiary.com/adding-custom-headers-java-httpservletrequest/

以防万一链接失效:

final class MutableHttpServletRequest extends HttpServletRequestWrapper {
// holds custom header and value mapping
private final Map<String, String> customHeaders;

public MutableHttpServletRequest(HttpServletRequest request){
super(request);
this.customHeaders = new HashMap<String, String>();
}

public void putHeader(String name, String value){
this.customHeaders.put(name, value);
}

public String getHeader(String name) {
// check the custom headers first
String headerValue = customHeaders.get(name);

if (headerValue != null){
return headerValue;
}
// else return from into the original wrapped object
return ((HttpServletRequest) getRequest()).getHeader(name);
}

public Enumeration<String> getHeaderNames() {
// create a set of the custom header names
Set<String> set = new HashSet<String>(customHeaders.keySet());

// now add the headers from the wrapped request object
@SuppressWarnings("unchecked")
Enumeration<String> e = ((HttpServletRequest) getRequest()).getHeaderNames();
while (e.hasMoreElements()) {
// add the names of the request headers into the list
String n = e.nextElement();
set.add(n);
}

// create an enumeration from the set and return
return Collections.enumeration(set);
}
}

用法:
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
MutableHttpServletRequest mutableRequest = new MutableHttpServletRequest(req);
...
mutableRequest.putHeader("x-custom-header", "custom value");
chain.doFilter(mutableRequest, response);
}

关于spring - 通过过滤器向请求添加自定义 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48240291/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com