Spring boot ServletRequest 修改header

背景

因爲環境升級,須要從新設置header中的 一個值,暫設定爲 org . http 請求時加入在header中加入org,但在filter中,會經過驗證,生成新的org,須要覆蓋原來header中的org.java

修改 filter 。代碼是在網上找的一部分,根據本身的須要進行調整了。

@Component
@Slf4j
@WebFilter(urlPatterns = { "/" }, filterName = "authorFilter")
public class DemoFilter implements Filter {
    @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
                                   FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        req.setAttribute("hdd","tttt");
        HeaderMapRequestWrapper requestWrapper = new HeaderMapRequestWrapper(req);
        requestWrapper.addHeader("realm","test");
        log.info("header-->{}",getHeadKeyAndValue(req));
        filterChain.doFilter(requestWrapper, servletResponse);

    }

    private Map<String, String> getHeadKeyAndValue(HttpServletRequest httpRequest) {
        Map<String, String> header = new HashMap<>();
        Enumeration<String> headerNames = httpRequest.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String nextElement = headerNames.nextElement();
            header.put(nextElement, httpRequest.getHeader(nextElement));
        }
        return header;
    }
}

@Slf4j
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<>();

    /**
     * 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) {
        log.info("getHeader --->{}",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) {
        log.info("getHeaders --->>>>>>{}",name);
        List<String> values = Collections.list(super.getHeaders(name));
        log.info("getHeaders --->>>>>>{}",values);
        if (headerMap.containsKey(name)) {
            log.info("getHeaders --->{}",headerMap.get(name));
            values = Arrays.asList(headerMap.get(name));
        }
        return Collections.enumeration(values);
    }
}

通過測試,在header解析時,是經過getHeaders方法,這個地方原來的時候是經過,values直接添加新的header,組成了一個 header的數組,而不是覆蓋。數組

相關文章
相關標籤/搜索