博客
关于我
拦截器使用 所有请求 都判断一下
阅读量: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/

    你可能感兴趣的文章
    PHP生成及获取JSON文件的方法
    查看>>
    PHP生成唯一不重复的编号
    查看>>
    PHP生成器-动态生成内容的数组
    查看>>
    PHP的ip2long和long2ip升级函数
    查看>>
    PHP的json_encode函数应用到微信接口问题(include \uxxxx will create fail)
    查看>>
    php的web路径获取
    查看>>
    php的一些小笔记--字符串
    查看>>
    php的几种运行模式CLI、CGI、FastCGI、mod_php
    查看>>
    php的四大特性八大优势
    查看>>
    RabbitMQ
    查看>>
    PHP的威胁函数与PHP代码审计实战
    查看>>
    PHP的引用举例
    查看>>
    PHP相关代码
    查看>>
    RabbitMQ
    查看>>
    php知识点记录
    查看>>
    PHP类数组式访问(ArrayAccess接口)
    查看>>
    PHP系列:浅谈PHP中isset()和empty() 函数的区别
    查看>>
    PHP索引数组unset的坑-array_values解决方案
    查看>>
    PHP索引数组排序方法整理(冒泡、选择、插入、快速)
    查看>>
    PHP线程安全和非线程安全
    查看>>