package com.example.demo.interceptor;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
/**
* @author
* 登录检查
* 1、配置好拦截那些请求
* 2、把这些配置放到容器中
*
*/
public class LoginInterceptor implements HandlerInterceptor {
//目标方法执行之前
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//可以是session,也可是redis中保存的用户信息
HttpSession session = request.getSession();
Object loginUser = session.getAttribute("loginUser");
if(loginUser != null){
//放行
return true;
}
//拦截住,未登录,跳转到登录页
request.setAttribute("msg","请登录");
request.getRequestDispatcher("/").forwa
springboot自定义拦截器之登录拦截器
于 2023-05-17 14:52:32 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)