package com.example.Maven.Project.intercepter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class CustomWebMvcConfigurer implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(getLoginInterceptor()).addPathPatterns("/api/v1/pri/**");//以"/api/v1/pri/**"结尾的都进行拦截 WebMvcConfigurer.super.addInterceptors(registry); } //这个注释代码是视频未讲的,补充知识点哈 //@Override //public void addInterceptors(InterceptorRegistry registry) { // // registry.addInterceptor(getLoginInterceptor()).addPathPatterns("/api/v1/pri/**","/api/v1/pri/user/**") // .excludePathPatterns("/**/*.html","/**/*.js"); //配置不拦截某些路径; // // // // WebMvcConfigurer.super.addInterceptors(registry); // // //} @Bean public LoginIntercepter getLoginInterceptor(){ return new LoginIntercepter(); } }
package com.example.Maven.Project.intercepter; import com.example.Maven.Project.domain.User; import com.example.Maven.Project.service.impl.UserServiceImpl; import com.example.Maven.Project.util.JsonData; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.util.StringUtils; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.PrintWriter; public class LoginIntercepter implements HandlerInterceptor { private static final ObjectMapper objectMapper = new ObjectMapper(); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("LoginIntercepter preHandle ====="); String token = request.getHeader("token"); if(StringUtils.isEmpty(token)){ token = request.getParameter("token"); } if(!StringUtils.isEmpty(token)){ //判断token是否合法 User user = UserServiceImpl.sessionMap.get(token); if(user!=null){ return true; }else { JsonData jsonData = JsonData.buildError("登录失败,token无效",-2); String jsonStr = objectMapper.writeValueAsString(jsonData); renderJson(response,jsonStr); return false; } }else { JsonData jsonData = JsonData.buildError("未登录",-3); String jsonStr = objectMapper.writeValueAsString(jsonData); renderJson(response,jsonStr); return false; } } private void renderJson(HttpServletResponse response,String json){ response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); try(PrintWriter writer = response.getWriter()){ writer.print(json); }catch (Exception e){ e.printStackTrace(); } } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("LoginIntercepter postHandle ====="); HandlerInterceptor.super.postHandle(request,response,handler,modelAndView); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("LoginIntercepter afterCompletion ====="); HandlerInterceptor.super.afterCompletion(request,response,handler,ex); } }