过滤器
@Component public class AddResponseHeaderFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { String header = response.getHeader("Set-Cookie"); Collection<String> headerNames = response.getHeaderNames(); System.out.println("___________________"); System.out.println(header); System.out.println("___________________"); filterChain.doFilter(request, response); } }
拦截器
public class HttpResponseInterceptorHandler implements HandlerInterceptor { // 实现HandlerInterceptor 或者 继承HandlerInterceptorAdapter都可以,如果想看着简洁就使用后者 @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { response.addHeader("content-test","123"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }