我正在使用基于注释的配置应用程序与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响应时,我无法看到从拦截器添加的标头值。 有人可以帮我解决问题并说明如何为每个请求从拦截器添加标头吗?