自定义注解获取当前登录信息
登录注解
/** * @author zhourui * @date 2023/4/18 9:39 */ import java.lang.annotation.*; /** * @author zhourui * @date 2023/4/17 14:42 */ @Documented @Target(ElementType.METHOD) @Retention(value = RetentionPolicy.RUNTIME) public @interface ReLogin { }
获取用户信息注解
package com.zjfc.sys.oauth2; import java.lang.annotation.*; /** * @author zhourui * @date 2023/4/17 14:42 */ @Documented @Target({ElementType.PARAMETER}) @Retention(value = RetentionPolicy.RUNTIME) public @interface ReUser { String key() default "ReUser"; }
登录解析器
/** * 登录解析器 * * @author zhourui * @date 2023/4/17 14:39 */ @Component public class ReLoginResolver implements HandlerMethodArgumentResolver { @Override public boolean supportsParameter(MethodParameter parameter) { if (parameter.hasParameterAnnotation(ReUser.class)) { return true; } return false; } @Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest request, WebDataBinderFactory webDataBinderFactory) throws MissingServletRequestPartException { ReUser reUser = parameter.getParameterAnnotation(ReUser.class); SysUserEntity user = (SysUserEntity) request.getAttribute(Objects.requireNonNull(reUser) .key(), RequestAttributes.SCOPE_REQUEST); if (user != null) { return user; } throw new MissingServletRequestPartException("userBean is null"); } }
拦截器配置
/** * 拦截器配置 * * @author zhourui * @date 2023/4/17 14:42 */ @Log4j2 @Component public class ReHandlerInterceptor implements HandlerInterceptor { @Autowired private ShiroService shiroService; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { HandlerMethod handlerMethod = (HandlerMethod) handler; ReLogin reLogin = AnnotationUtils.findAnnotation(handlerMethod.getMethod(), ReLogin.class); if (reLogin != null) { // 获取令牌信息 String token = request.getHeader("token"); log.info("当前传递的token为: {}", token); if (StrUtil.isBlank(token)) { throw new RRException("提示: 请求方法未传token!"); } // 查询用户信息 SysUserTokenEntity sysUser = shiroService.queryByToken(token); if (sysUser == null) { throw new RRException("提示: 令牌无效,无效token!"); } SysUserEntity user = shiroService.queryUser(sysUser.getUserId()); log.info("用户信息: {}", JSON.toJSONString(user)); Optional.ofNullable(user).ifPresent(item -> request.setAttribute("ReUser", item)); } return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) { response.addHeader("x-frame-options", "SAMEORIGIN"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { } }
坑点:所写的拦截器不能注入bean
所以通过 WebMvcConfigurer注入
** * @author zhourui * @date 2023/4/17 14:40 */ @Configuration public class ReWebConfig implements WebMvcConfigurer { /** * 实例化拦截器 * * @return */ @Bean public ReHandlerInterceptor reHandlerInterceptor() { return new ReHandlerInterceptor(); } /** * 实例化解析器 * * @return */ @Bean public ReLoginResolver ReWebResolver() { return new ReLoginResolver(); } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(reHandlerInterceptor()); } @Override public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) { resolvers.add(new ReLoginResolver()); } }
使用: