公共类RequestWrapper扩展HttpServletRequestWrapper{

问题描述 投票:0回答:1
private final Map<String, String> customHeaders; public RequestWrapper(HttpServletRequest request) { super(request); this.customHeaders = new HashMap<String, String>(); } public void setHeader(String name, String value) { this.customHeaders.put(name, value); } @Override public String getHeader(String name) { return !StringUtils.isEmpty(customHeaders.get(name)) ? customHeaders.get(name) : ((HttpServletRequest) getRequest()).getHeader(name); } @Override public Enumeration<String> getHeaderNames() { Set<String> set = new HashSet<String>(customHeaders.keySet()); Enumeration<String> e = ((HttpServletRequest) getRequest()).getHeaderNames(); while (e.hasMoreElements()) { set.add(e.nextElement()); } return Collections.enumeration(set); }

}

公共类RequestFilter扩展了OncePerRequestFilter{

@ Override

protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException
{

        RequestWrapper req = new RequestWrapper(request);
        req.setHeader("Uuid", UUID.randomUUID().toString());
        filterChain.doFilter(req, response);
    }
}

}

我在HttpHeader的这里没有得到Uuid或自定义标头,为什么?

@PostMapping(value = "/v0.2/Student/add") public ResponseEntity<ResponseBody<PlatformCardResponse>> addStudent(HttpServletRequest request, @RequestHeader HttpHeaders httpHeaders, @RequestBody Student s) {

我如何使其能够接收HttpHeader中的标头。或任何其他方法,我们必须在httpServletRequest中修改请求标头吗????

我没有获得我使用包装器设置的自定义标头,以在控制器的HttpHeader内部请求。 RequestWrapper:-公共类RequestWrapper扩展了HttpServletRequestWrapper {private final Map&...

spring servlets filter http-headers interceptor
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.