当前位置: 首页 > news >正文

用户访问认证

注解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Login {
}

自定义拦截器

@Component
public class AuthInterceptor implements HandlerInterceptor {@ResourceJwtUtils jwtUtils;@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {// 判断是否有@Login注解,没有就放行Login annotation;if(handler instanceof HandlerMethod){annotation = ((HandlerMethod) handler).getMethodAnnotation(Login.class);}else{return true;}if(annotation == null){return true;}// 获取用户凭证tokenString token = request.getHeader("token");// 判断token是否合法if(!StringUtils.hasText(token)){throw new HubException("请先登录");}Claims claim = jwtUtils.getClaimByToken(token);if(claim == null || jwtUtils.isTokenExpired(claim.getExpiration())){throw new HubException("请先登录");}// 把用户信息存到sessionrequest.getSession().setAttribute(Const.USER_KEY, Long.parseLong(claim.getSubject()));return true;}
}

MvcConfig

@Configuration
public class MvcConfig implements WebMvcConfigurer {@ResourceAuthInterceptor authInterceptor;@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(authInterceptor).addPathPatterns("/app/**");}
}

测试

@RestController
@RequestMapping("/app")
public class UserController {@Login@GetMapping("/userInfo")public Result getUserInfo(){return Result.success();}@GetMapping("/userInfo2")public Result getUserInfo2(){return Result.success();}
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

http://www.lryc.cn/news/259462.html

相关文章:

  • 前端知识(七)———HTTPS:保护网络通信安全的关键
  • element-ui按钮el-button,点击之后恢复之前的颜色
  • Excel: Python 如何干掉 VBA 系列 乙
  • 算法笔记—链表、队列和栈
  • MySQL中的时间函数整理汇总
  • stu06-VSCode里的常用快捷键
  • Bypass open_basedir
  • 【数据库设计和SQL基础语法】--查询数据--过滤
  • 关于git clone速度极慢的解决方法
  • 软件设计不是CRUD(8):低耦合模块设计实战——组织机构模块(下)
  • docker-compose Install gitea
  • 【Pytorch】学习记录分享1——Tensor张量初始化与基本操作
  • Python数据科学视频讲解:Python的数据运算符
  • 参数学习——糖果问题(人工智能期末复习)
  • 【深度学习】注意力机制(六)
  • 螺旋矩阵算法(leetcode第59题)
  • SQL Server 服务启动报错:错误1069:由于登录失败而无法启动服务
  • “ABCD“[(int)qrand() % 4]作用
  • Vue2面试题:说一下组件通信有哪些方式?
  • C# 两个日期比较大小
  • 路由基本原理
  • 配置本地端口镜像示例
  • 使用FluentAvalonia组件库快速完成Avalonia前端开发
  • JAVA实体类集合该如何去重?
  • 修改Element UI可清空Input的样式
  • Java常用注解
  • golang实现同步阻塞、同步非阻塞、异步非阻塞IO模型
  • java SSM教师工作量管理系统myeclipse开发mysql数据库springMVC模式java编程计算机网页设计
  • 大数据技术之Hive(超级详细)
  • NVMe over Fabrics with SPDK with iRDMA总结 - 1