简体   繁体   English

在拦截器 spring 引导上添加自定义 header

[英]Add custom header on interceptor spring boot

I have an interceptor I need to add a custom header which is required for other parts of the code我有一个拦截器,我需要添加一个自定义 header,这是代码的其他部分所必需的

public class KeyTaskInterceptor implements HandlerInterceptor {

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {

    if (// a custom condition) {
        request.  // set custom header key ... `KeyCode`
    }
    return true;
} 

The problem is the frontend doesn't send this custom header named " KeyCode " and I can't change the implementation for the controllers that expect this header so I have to find a way to add a custom header on request on preHandle method before sending the request to the controller. The problem is the frontend doesn't send this custom header named " KeyCode " and I can't change the implementation for the controllers that expect this header so I have to find a way to add a custom header on request on preHandle method before sending对 controller 的请求。 Can someone help me, please?有人能帮助我吗?

HttpServletRequest object is read-only and you cannot modify its headers in the HandlerInterceptor . HttpServletRequest object 是只读的,您不能在HandlerInterceptor中修改其标头。 The only thing that you can do with it - is to set attributes and read them later in your controller.您唯一能做的就是设置属性并稍后在 controller 中读取它们。

But as in your case you can't change the implementation of the controllers to read attributes, you need actually modify request headers.但是在您的情况下,您无法更改控制器的实现以读取属性,您实际上需要修改请求标头。

There is a way of how to do it - is to use a Filter in which you will substitute the incoming request object with your own request wrapper implementation.有一种方法可以做到这一点 - 是使用Filter ,您将在其中将传入的请求 object 替换为您自己的请求包装器实现。 By using a request wrapper, you can modify its headers list as you need.通过使用请求包装器,您可以根据需要修改其标头列表。

There is a good tutorial that explains how to do it.一个很好的教程解释了如何做到这一点。

Below is an example based on this tutorial, slightly adapted for your use case:以下是基于本教程的示例,略微适合您的用例:

@Component
public class CustomHeaderFilter implements Filter {
  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
      throws IOException, ServletException {
    MutableHttpServletRequest mutableRequest = new MutableHttpServletRequest((HttpServletRequest) request);
    mutableRequest.putHeader("KeyCode", "custom value");
    chain.doFilter(mutableRequest, response);
  }
}

And the implementation of the MutableHttpServletRequest from the tutorial :以及教程MutableHttpServletRequest的实现:

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM