springboot攔截器 說明:和springmvc一樣去實現HandlerInterceptor介面 import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerIn ...
- springboot攔截器
import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 〈一句話功能簡述〉<br> * TODO(參數攔截器1) * * @author sunhan * @version 1.0.0 * @since 2020/3/7 10:06 PM */ @Component public class ParameterInterceptor1 implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("進入了攔截器-----11111"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { } }View Code
把自定義攔截器註冊到WebMvcConfigurer配置介面中:
重寫addInterceptors方法,添加自定義攔截器的order,pathPatterns
import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** * 〈一句話功能簡述〉<br> * TODO() * * @author sunhan * @version 1.0.0 * @since 2020/3/7 10:25 PM */ @Configuration public class InterceptConfiguration implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new ParameterInterceptor1()).addPathPatterns("/get*"); registry.addInterceptor(new ParameterInterceptor2()).addPathPatterns("/save*"); } }View Code
- springboot過濾器
說明:註解方式和註冊方式
1、註解方式:
import org.springframework.core.annotation.Order; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; /** * 〈一句話功能簡述〉<br> * TODO() * * @author sunhan * @version 1.0.0 * @since 2020/3/6 5:28 PM */ @WebFilter(filterName = "requestParamFilter",urlPatterns = "/*") @Order(3) public class RequestParamFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("sunhan---請求參數過濾器!---test1"); filterChain.doFilter(servletRequest,servletResponse); } @Override public void destroy() { } }View Code
註意:
1.1、註解方式需要@WebFilter和@Order放在Filter實現類上。
1.2、註解方式需要@ServletComponentScan放在SpringBootApplication啟動類上
2、註冊方式:
import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import java.io.IOException; public class MyFilter implements Filter { @Override public void init(FilterConfig arg0) throws ServletException { } @Override public void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) srequest; System.out.println("this is MyFilter,url :"+request.getRequestURI()); filterChain.doFilter(srequest, sresponse); } @Override public void destroy() { } }View Code
註意:@Bean放在一個方法中,然後通過springboot的FilterRegistrationBean註冊到過濾鏈中
import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class WebConfiguration { @Bean public FilterRegistrationBean testFilterRegistration() { FilterRegistrationBean registration = new FilterRegistrationBean(); registration.setFilter(new MyFilter()); registration.addUrlPatterns("/*"); registration.setName("MyFilter"); registration.setOrder(6); return registration; } @Bean public FilterRegistrationBean test2FilterRegistration() { FilterRegistrationBean registration = new FilterRegistrationBean(); registration.setFilter(new MyFilter2()); registration.addUrlPatterns("/*"); registration.setName("MyFilter2"); registration.setOrder(1); return registration; } }View Code