• 自定义注解实现token拦截放行


    首先自定义一个注解

    1. import java.lang.annotation.ElementType;
    2. import java.lang.annotation.Retention;
    3. import java.lang.annotation.RetentionPolicy;
    4. import java.lang.annotation.Target;
    5. /**
    6. * @Description: token鉴权放行注解
    7. * @Author: cll
    8. * @CreateTime: 2023-10-24 20:03
    9. */
    10. @Retention(RetentionPolicy.RUNTIME)
    11. @Target({ElementType.TYPE,ElementType.METHOD})
    12. public @interface NoAuth {
    13. }

    然后自定义拦截器

    1. import cn.hutool.core.util.NumberUtil;
    2. import cn.hutool.core.util.ObjectUtil;
    3. import cn.hutool.core.util.StrUtil;
    4. import com.alibaba.fastjson.JSON;
    5. import lombok.extern.slf4j.Slf4j;
    6. import org.apache.commons.lang3.StringUtils;
    7. import org.springframework.web.method.HandlerMethod;
    8. import org.springframework.web.servlet.HandlerInterceptor;
    9. import javax.servlet.http.HttpServletRequest;
    10. import javax.servlet.http.HttpServletResponse;
    11. /**
    12. * @desc: 登录拦截器
    13. */
    14. @Slf4j
    15. public class LoginInterceptor implements HandlerInterceptor {
    16. @Override
    17. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    18. if (HandlerMethod.class.equals(handler.getClass())) {
    19. HandlerMethod handlerMethod = (HandlerMethod) handler;
    20. NoAuth noAuth = handlerMethod.getMethod().getAnnotation(NoAuth.class);
    21. if (noAuth == null) {
    22. String authorization = request.getHeader("Authorization");
    23. log.info("登录拦截器authorization:{}", authorization);
    24. if (StringUtils.isBlank(authorization)) {
    25. log.info("登录拦截器,无权限访问,requestURL:{}", request.getRequestURL());
    26. throw new ApiException("无权限访问");
    27. }
    28. if (StringUtils.isNotBlank(authorization)) {
    29. //todo 添加token校验逻辑
    30. }
    31. }
    32. }
    33. return true;
    34. }
    35. }

    然后注册拦截器

    1. import org.springframework.context.annotation.Configuration;
    2. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    3. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    4. /**
    5. * @desc:
    6. * @author:
    7. * @date:
    8. */
    9. @Configuration
    10. public class MVCConfig implements WebMvcConfigurer {
    11. @Override
    12. public void addInterceptors(InterceptorRegistry registry) {
    13. registry.addInterceptor(new LoginInterceptor())
    14. // 拦截路径
    15. .addPathPatterns("/**")
    16. // 排除swagger
    17. .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/api-docs", "/swagger-ui.html/**", "/doc.html", "/favicon.ico")
    18. ;
    19. }
    20. }

    自定义注解使用

    1. @RestController
    2. @RequestMapping("/test")
    3. public class TestController {
    4. @NoAuth
    5. @PostMapping("test")
    6. public String test(){
    7. return "11111111";
    8. }
    9. }

  • 相关阅读:
    【入门篇】1.4 redis 客户端 之 Lettuce 详解
    Vue过渡和动画效果展示(案例、GIF动图演示、附源码)
    Flutter Image组件如何处理图片加载过程中的错误?
    Objective-C编写程序
    ffmpeg 中av_rescale_rnd 的含义
    【MyBatis】MyBatis基础内容介绍
    [RK3568 Android11]Wifi 连接过程
    mysql root允许远程连接
    详细介绍Unlink的原理及分析
    基于粒子群算法的排课系统,基于PSO的排课系统,粒子群算法原理,粒子群算法流程
  • 原文地址:https://blog.csdn.net/weixin_42037525/article/details/134026513