springboot 实现拦截器
创建一个拦截器
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* hander拦截器
*
* @author liu
*/
public class LoginHandlerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//return true就放行 false就是拦截
//登录之后,应该有用户的Session
Object loginUser = request.getSession().getAttribute("loginUser");
//没有登录`
if (loginUser == null) {
request.setAttribute("msg", "没有权限,请先登录");
request.getRequestDispatcher("/index.html").forward(request, response);
return false;
} else {
return true;
}
}
}
登录时发送一个session,可用于拦截器判断是否登录
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.thymeleaf.util.StringUtils;
import javax.servlet.http.HttpSession;
@Controller
public class loginController {
@RequestMapping("/user/login")
public String login(@RequestParam("UserName") String userName,
@RequestParam("PassWord") String pwd,
Model model,
HttpSession session) {
if (!StringUtils.isEmpty(userName) && "123456".equals(pwd)) {
//登录后发送一个session,可用于拦截器判断是否登录
session.setAttribute("loginUser",userName);
return "redirect:/dashboard";
} else {
model.addAttribute("msg", "用户名或密码错误");
return "index";
}
}
}
config文件配置,添加拦截器
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.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
//拓展引用,重写视图控制器方法
@Override
public void addViewControllers(ViewControllerRegistry registry) {
WebMvcConfigurer.super.addViewControllers(registry);
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
registry.addViewController("/list").setViewName("list");
registry.addViewController("/list.html").setViewName("list");
registry.addViewController("/dashboard").setViewName("dashboard");
registry.addViewController("/dashboard.html").setViewName("dashboard");
}
//自定义的国际化组件就生效了
@Bean
public MyLocaleResolver localeResolver(){
return new MyLocaleResolver();
}
//拦截器,拦截什么,放行什么
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHandlerInterceptor())
.addPathPatterns("/**")
//注意:不要拦截登录请求"/user/login"
.excludePathPatterns("/index.html", "/","/user/login","/index", "/css/*", "/img/**", "/js/**");
}
}
分类:
springBoot
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!