首页 > 其他分享 >【SpringBoot】自定义注解+拦截

【SpringBoot】自定义注解+拦截

时间:2023-03-21 11:25:15浏览次数:35  
标签:AuthUser SpringBoot 自定义 handler 注解 HandlerMethod ElementType String

 

创建一个注解,用来校验身份

@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface AuthUser {

//    int user();
    // 管理员
//    String User_ADMIN = "101";
//    // 普通用近乎
//    String User_STANDARD = "102";
//    // 被封禁
//    String User_LOCK = "500";


}

@Target({ElementType.TYPE,ElementType.METHOD})  让注解可用在类或方法上

 

 

 

拦截器中捕获注解【这里用webmvc的】HandlerInterceptor接口    preHandle  拦截请求

 

private boolean checkAdmin(HttpServletRequest request, Object handler,String token)  {
    // 如果不是方法
    if (!(handler instanceof HandlerMethod)){
        return true;
    }

    // 获取注解
    AuthUser pl = ((HandlerMethod)handler).getMethodAnnotation(AuthUser.class);//写在方法上的
    AuthUser annotation = ((HandlerMethod) handler).getMethod().getDeclaringClass().getAnnotation(AuthUser.class);//写在类上的

    if(annotation != null){
        System.out.println("获取到写在类上注解了");
    }


    if(pl != null){
        System.out.println("获取到写在方法上的注解了");
    }
    
    
        return true;
}

 

标签:AuthUser,SpringBoot,自定义,handler,注解,HandlerMethod,ElementType,String
From: https://www.cnblogs.com/Hello233/p/17239300.html

相关文章

  • SpringBoot——包扫描@ComponentScan源码分析
    摘要博文参考(1)选择SpringCloud作为微服务架构的原因(2)SpringBoot和SpirngCloud,请你谈谈对他们的理解(3)什么是服务熔断?什么是服务降级?(4)微服务的优缺点分别是什么?(5)你所知道......
  • SpringBoot——springboot自动配置原理
    摘要 主要是介绍的Springboot的底层原理。web.xml配置Spring环境<!--Spring监听器--><1istener>org.springframework.web.context.ContextLoaderListener</1istener-......
  • SpringBoot——spring quarter原理和应用
    摘要1、JDK定时器timer使用及原理分析2、定时任务线程池解析3、定时任务框架-quarter小顶堆......
  • 5-springboot集成热部署的方式
    热部署是指当我们修改代码后,服务能自动重启加载新修改的内容,这样大大提高了我们开发的效率;Springboot热部署通过添加一个插件实现;插件为:spring-boot-devtools,在Maven中......
  • 4-springboot集成mybatis
    1.pom.xml中添加mybatis依赖2.application.properties中添加数据源配置3.反向生成dao的代码1.<!--mybatis-spring-boot-starter--><dependency>    <groupId>org......
  • springboot 接入 ChatGPT
    介绍lucy-chat是接入OpenAI-ChatGPT大模型人工智能的Java解决方案,大模型人工智能的发展是不可阻挡的趋势,我们环境无法创造工具,但是也要更好的使用工具,该包简化了接......
  • 12_SpringBoot_整合Thymeleaf_掌握
       Thymeleaf的主要目标是将优雅的自然模板带到开发工作流程中,并将HTML在浏览器中正确显示,并且可以作为静态原型,让开发团队能更容易地协作。Thymeleaf能够处理HTML,XML,Ja......
  • 12_SpringBoot_整合Thymeleaf_掌握
       Thymeleaf的主要目标是将优雅的自然模板带到开发工作流程中,并将HTML在浏览器中正确显示,并且可以作为静态原型,让开发团队能更容易地协作。Thymeleaf能够处理HTML,XML,Ja......
  • 15_SpringBoot_模板引擎总结_了解
    jsp优点:1、功能强大,可以写java代码2、支持jsp标签(jsptag)3、支持表达式语言(el)4、官方标准,用户群广,丰富的第三方jsp标签库缺点:性能问题。不支持前后端分离freemarker......
  • 15_SpringBoot_模板引擎总结_了解
    jsp优点:1、功能强大,可以写java代码2、支持jsp标签(jsptag)3、支持表达式语言(el)4、官方标准,用户群广,丰富的第三方jsp标签库缺点:性能问题。不支持前后端分离freemarker......