public class MyInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { HttpSession session = request.getSession(); String username = (String) session.getAttribute("username"); if (username != null){ System.out.println(username); return true; } request.getRequestDispatcher("/toLogin").forward(request,response); return false; }2.写一个配置类,把自己写的拦截器,在配置类加上,如何需要拦截的路径和放行的路径
@Configuration public class MyConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new MyInterceptor()) .addPathPatterns("/**") //拦截的路径 .excludePathPatterns("/toLogin","/login"); //放行的路径 } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)