Spring Rest API拦截器在每个/每个请求上添加响应头

时间:2017-04-22 06:07:10

标签: rest spring-mvc interceptor spring-4 response-headers

我正在使用基于Spring 4 REST API注释的配置应用程序。一旦用户通过JWT进行身份验证,我想在每个/每个请求上添加响应头。我创建了拦截器,如下所示:

public class AuthenticateInterceptor implements HandlerInterceptor {

@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object, Exception arg3)
        throws Exception {
    response.addHeader("afterCompletion", "afterCompletion header");
    response.setHeader("afterCompletion", "afterCompletion header");
    System.out.println("************** afterCompletion **************");
}

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object,
        ModelAndView modelAndView) throws Exception {
    response.addHeader("postHandle", "postHandle header");

    System.out.println("************** postHandle **************");
}

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
    System.out.println("************** preHandle **************");

    return true;
  }
}

我的拦截器配置如下:

@Configuration
public class AdapterConfig extends WebMvcConfigurerAdapter {

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new AuthenticateInterceptor());
  }

}

当我收到JSON响应时,我无法看到从拦截器添加的添加的标头值。任何人都可以帮助我解决问题,如何为拦截器添加每个/每个请求的标头。

3 个答案:

答案 0 :(得分:0)

如果它可以帮助你,我就像这样管理它:https://stackoverflow.com/a/49431665/4939245
看第二点

您可以将每个调用的响应头放在应用程序中(这是基于Spring注释的):

@Component
public class Filter extends OncePerRequestFilter {
....
 @Override
 protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
        throws ServletException, IOException {
    //response.addHeader("Access-Control-Allow-Origin", "*");
    //response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
    response.setHeader("Cache-Control", "no-store"); // HTTP 1.1.
    response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
    response.setHeader("Expires", "0"); // Proxies.
    filterChain.doFilter(request, response);
 }
}

我希望我能提供帮助!

答案 1 :(得分:0)

您几乎都在做同样的事情,但是想给您带来实现目标的便利方法。请进行以下更改。

1)在AuthenticateInterceptor类上添加@Component批注。并且包含此类的软件包应该在软件包扫描列表中。

@Component
public class AuthenticateInterceptor implements HandlerInterceptor {
 ...
}

2)自动连接并注入AuthenticateInterceptor实例,如下所示。

@Configuration
public class AdapterConfig extends WebMvcConfigurerAdapter {

@Autowired
private AuthenticateInterceptor authenticateInterceptor;

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(authenticateInterceptor);
  }

}

答案 2 :(得分:0)

我在拦截器方面没有成功,而是使用过滤器或 WebFilter 完美地工作:

@Component
public class ResponseHeaderWebFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        httpServletResponse.setHeader("CustomHeaderName", "SomeValue");
        chain.doFilter(request, response);
    }
}

如果您使用的是 webflux 反应式组件,则:

@Component
public class ResponseHeaderWebFilter implements WebFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        exchange.getResponse()
          .getHeaders()
          .add("CustomHeaderName", "SomeValue");
        return chain.filter(exchange);
    }
}