返回值类型 | 方法声明 | 描述 |
---|---|---|
boolean | preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) | 该方法在控制器处理请求方法前执行,其返回值表示是否中断后续操作,返回 true 表示继续向下执行,返回 false 表示中断后续操作。 |
void | postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) | 该方法在控制器处理请求方法调用之后、解析视图之前执行,可以通过此方法对请求域中的模型和视图做进一步修改。 |
void | afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) | 该方法在视图渲染结束后执行,可以通过此方法实现资源清理、记录日志信息等工作。 |
package net.biancheng.www.componet; import lombok.extern.slf4j.Slf4j; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @Slf4j public class LoginInterceptor implements HandlerInterceptor { /** * 目标方法执行前 * * @param request * @param response * @param handler * @return * @throws Exception */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { Object loginUser = request.getSession().getAttribute("loginUser"); if (loginUser == null) { //未登录,返回登陆页 request.setAttribute("msg", "您没有权限进行此操作,请先登陆!"); request.getRequestDispatcher("/index.html").forward(request, response); return false; } else { //放行 return true; } } /** * 目标方法执行后 * * @param request * @param response * @param handler * @param modelAndView * @throws Exception */ @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { log.info("postHandle执行{}", modelAndView); } /** * 页面渲染后 * * @param request * @param response * @param handler * @param ex * @throws Exception */ @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { log.info("afterCompletion执行异常{}", ex); } }
@Configuration public class MyMvcConfig implements WebMvcConfigurer { ...... @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new LoginInterceptor()); } }
@Slf4j @Configuration public class MyMvcConfig implements WebMvcConfigurer { ...... @Override public void addInterceptors(InterceptorRegistry registry) { log.info("注册拦截器"); registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**") //拦截所有请求,包括静态资源文件 .excludePathPatterns("/", "/login", "/index.html", "/user/login", "/css/**", "/images/**", "/js/**", "/fonts/**"); //放行登录页,登陆操作,静态资源 } }
至此,拦截器的基本功能已经完成,接下来,我们先实现 spring-boot-adminex 的登陆功能,为验证登陆拦截做准备。
图1:main 页面
package net.biancheng.www.controller; import lombok.extern.slf4j.Slf4j; import net.biancheng.www.bean.User; import org.springframework.stereotype.Controller; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpSession; import java.util.Map; @Slf4j @Controller public class LoginController { @RequestMapping("/user/login") public String doLogin(User user, Map<String, Object> map, HttpSession session) { if (user != null && StringUtils.hasText(user.getUsername()) && "123456".equals(user.getPassword())) { session.setAttribute("loginUser", user); log.info("登陆成功,用户名:" + user.getUsername()); //防止重复提交使用重定向 return "redirect:/main.html"; } else { map.put("msg", "用户名或密码错误"); log.error("登陆失败"); return "login"; } } /* @RequestMapping("/main.html") public String mainPage(){ return "main"; }*/ }
@Configuration public class MyMvcConfig implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { //当访问 “/” 或 “/index.html” 时,都直接跳转到登陆页面 registry.addViewController("/").setViewName("login"); registry.addViewController("/index.html").setViewName("login"); //添加视图映射 main.html 指向 dashboard.html registry.addViewController("/main.html").setViewName("main"); } ...... }
4. 在 login.html 适当位置添加以下代码,显示错误信息。
<p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>
图1:登陆被拦截
图2:登陆失败
本文链接:http://task.lmcjl.com/news/17485.html