在网关级别的dofilter中添加请求的自定义标头

时间:2018-12-24 09:15:01

标签: java spring-boot netflix-zuul api-gateway

我正在基于请求uri实现api网关。我需要在chain.doFilter(request,response)中添加其他标题。

我的过滤器是jwt过滤器,如下所示

public class JwtAuthenticationFilter extends OncePerRequestFilter{
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
        throws ServletException, IOException {
        CustomHttpServletRequest cRequest = new CustomHttpServletRequest(request);

        /* authenticate user for valid token if valid then add the following header*/

        if(request.getRequestURI().equals("/testService/updateHeader")) {
              cRequest.putHeader("test","Test-header");
        }


        filterChain.doFilter(cRequest, response);
    }
}

基于reference实现:

在我的testservice中,尝试在标题中获取标题,我无法获取标题名称

1 个答案:

答案 0 :(得分:0)

我想问题是在filterChain.doFilter(request,response)中,您使用的是原始请求,而不是使用自定义侦听器的新请求。更改

filterChain.doFilter(request, response);

filterChain.doFilter(cRequest, response);