如何使用SpringBoot拦截器实现登录拦截
发表于:2025-11-19 作者:千家信息网编辑
千家信息网最后更新 2025年11月19日,小编给大家分享一下如何使用SpringBoot拦截器实现登录拦截,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!可以对URL
千家信息网最后更新 2025年11月19日如何使用SpringBoot拦截器实现登录拦截
小编给大家分享一下如何使用SpringBoot拦截器实现登录拦截,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
可以对URL路径进行拦截,可以用于权限验证、解决乱码、操作日志记录、性能监控、异常处理等
实现代码
新建 interceptor包

添加拦截器代码
package com.qcby.interceptor; import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.method.HandlerMethod;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession; public class LoginInterceptor implements HandlerInterceptor { @Autowired private HttpSession httpSession; //Controller逻辑执行之前 @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("preHandle...."); String uri = request.getRequestURI(); System.out.println("当前路径"+uri); /** * HandlerMethod=>Controller中标注@RequestMapping的方法 * 需要配置静态资源不拦截时,添加这块逻辑 => 前后端分离项目 */ if (!(handler instanceof HandlerMethod)) { return true; } if (httpSession.getAttribute("username") == null) { // 未登录跳转到登录界面 throw new RuntimeException("no login!"); } else { return true; } } //Controller逻辑执行完毕但是视图解析器还未进行解析之前 @Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { System.out.println("postHandle...."); } //Controller逻辑和视图解析器执行完毕 @Override public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { System.out.println("afterCompletion...."); }}注册,配置拦截路径和排除登录需访问路径

package com.qcby.config; import com.qcby.interceptor.LoginInterceptor;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; @Configurationpublic class WebMvcConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(loginInterceptor()) .addPathPatterns("/**") // 那些路径不拦截 .excludePathPatterns("/user/login","/error"); } @Bean public LoginInterceptor loginInterceptor(){ return new LoginInterceptor(); }}实现类
@RestController@RequestMapping("user")public class UserController {@Autowired private UserService userService; @Autowired private HttpSession session; @ApiOperation("用户登录接口") @RequestMapping(value="login",method = {RequestMethod.GET,RequestMethod.POST}) public Maplogin(User user){ Map map=new HashMap<>(); map.put("code",0); if(StringUtils.isEmpty(user.getUsername())||StringUtils.isEmpty(user.getPassword())){ map.put("msg","用户或密码为空!"); return map; } QueryWrapper queryWrapper=new QueryWrapper<>(); queryWrapper.eq("username",user.getUsername()) .eq("password",user.getPassword()); User user1=userService.getOne(queryWrapper); if(user1!=null){ map.put("cod",1); map.put("data",user1); session.setAttribute("username",user1.getUsername()); }else { map.put("msg","用户名或密码错误!"); } return map; }} 当我们未登录时我们不能进入拦截的页面
登录
登录之后我们就能进入hello方法了
以上是"如何使用SpringBoot拦截器实现登录拦截"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
登录
路径
逻辑
拦截器
用户
篇文章
代码
内容
密码
方法
视图
配置
不怎么
乱码
大部分
性能
接口
日志
更多
权限
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
放映服务器
跳绳软件开发
软件开发是本科还是专科专业
可以从ei数据库下载论文吗
安徽证券软件开发公司
Windows原生软件开发
杨浦区网络技术服务咨询
区块链缓慢的数据库
传奇内置挂数据库
独立服务器的外贸管理软件
软件开发部门工作口号
c语言ado连接数据库
服务器运算速度慢
合肥宝德网络技术公司
火影一个服务器多少人满
plsql 复制数据库
深圳科技互联网公司
游戏服务器上线标准
软件开发做哪些事情
单机软件开发时间
数据库连接成功表不存在
珠海服务器公司
临沂微企邦网络技术服务
哪家网络平台服务器比较流畅
汽车软件开发 标准
网络安全策略提供哪些信息
网络技术毕业生对学校的建议
bmp 数据库图片
水果电商数据库的案例
开阳县网络安全