博客
关于我
拦截器使用 所有请求 都判断一下
阅读量:316 次
发布时间:2019-03-04

本文共 1018 字,大约阅读时间需要 3 分钟。

@Componentpublic class WendaWebConfiguration extends WebMvcConfigurerAdapter {    @Autowired    private LoginFilter loginFilter;    @Override    public void addInterceptors(InterceptorRegistry registry) {        registry.addInterceptor(loginFilter);        super.addInterceptors(registry);    }}
@Componentpublic class LoginFilter extends HandlerInterceptorAdapter {    @Override    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {        System.out.println("我是拦截器");        return true;    }}

以上代码片段展示了两个技术类的实现:

  • WendaWebConfiguration类继承自WebMvcConfigurerAdapter,用于配置Spring Boot的Web应用程序的全局拦截器

  • LoginFilter类实现了HandlerInterceptorAdapter接口,作为请求处理的拦截器

  • 代码中使用了@Component注解标记组件,使用了@Autowired注解实现自动注入功能。WendaWebConfiguration类中添加了拦截器的实现,通过registry.addInterceptor(loginFilter)将LoginFilter拦截器注册到拦截器注册表中,同时调用了super.addInterceptors(registry)以继承父类的拦截器配置。LoginFilter类实现了preHandle方法,打印了一条日志信息并返回true,表示该拦截器对请求进行了处理。

    代码结构清晰,注解使用规范,类之间继承关系明确,符合Spring Boot框架的开发规范。

    转载地址:http://pijq.baihongyu.com/

    你可能感兴趣的文章
    Prometheus Alertmanager 告警配置详解
    查看>>
    Prometheus Grafana 展示平台
    查看>>
    Prometheus pushgateway使用详解
    查看>>
    Prometheus 云原生 - Prometheus 数据模型、Metrics 指标类型、Exporter 相关
    查看>>
    Prometheus 云原生 - 基于 file_sd、http_sd 实现 Service Discovery
    查看>>
    Prometheus 云原生 - 微服务监控报警系统 (Promethus、Grafana、Node_Exporter)部署、简单使用
    查看>>
    Prometheus 云原生 - 监控 Linux、MySQL、Redis、RabbitMQ、Docker、SpringBoot 3.x
    查看>>
    Prometheus 介绍
    查看>>
    Prometheus 安全配置详解
    查看>>
    prometheus 安装node_exporter, node_exporter 安装最新版 普罗米修思安装监控服务器client
    查看>>
    Prometheus 安装部署实战
    查看>>
    prometheus 持久化存储方案
    查看>>
    Prometheus 监控系统企业级实战
    查看>>
    Prometheus 监控系统简介
    查看>>
    Prometheus 配置详解
    查看>>
    Prometheus 采集器使用详解
    查看>>
    Prometheus 黑盒监控实战
    查看>>
    prometheus+alertmanager+grafana监控部署教程
    查看>>
    Prometheus+Grafana构建智能化Kubernetes监控系统实战
    查看>>
    Prometheus+SpringBoot应用监控全过程详解
    查看>>