springboot過濾器和攔截器
阿新 • • 發佈:2021-07-28
過濾器
@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 implementsHandlerInterceptor { // 實現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 { } }