問題1
springboot注冊攔截器過濾器方法
注冊攔截器:在啟動類中注冊bean
@EnableWebMvc@Configurationstatic class MvcConfigurer implements WebMvcConfigurer {//在攔截器中需要使用這個bean,如果直接在攔截器中注入的話會失敗,所以選擇有參構造的方式傳入 @Autowired CacheService cacheService; @Override public void addInterceptors(InterceptorRegistry registry) { //指定攔截器類 registry.addInterceptor(new AuthorizationInterceptor(cacheService)) //指定該類攔截的url,過濾掉指定的url .addPathPatterns("/gw/**").excludePathPatterns("/**/query_token/**"); }}public class AuthorizationInterceptor extends HandlerInterceptorAdapter{//選擇性的重寫preHandle postHandle afterCompletion afterConcurrentHandlingStarted方法}
或者是
public class AuthorizationInterceptor implements HandlerInterceptor{//重寫preHandle postHandle afterCompletion方法default boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {//請求開始執行之前執行改方法,返回true該請求猜能被繼續執行下去,返回false的話,請求就直接結束 return true;}public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {//該方法的執行時間是在處理器進行處理之后,也就是在Controller的方法調用之后執行,但是它會在DispatcherServlet進行視圖的//渲染之前執行,也就是說在這個方法中你可以對ModelAndView進行操作}public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {//該方法將在整個請求結束之后,也就是在DispatcherServlet 渲染了對應的視圖之后執行。這個方法的主要作用是用于進行資源清理工作的。}}
總結
以上所述是小編給大家介紹的springboot注冊攔截器所遇到的問題,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VeVb武林網網站的支持!
新聞熱點
疑難解答
圖片精選