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

    你可能感兴趣的文章
    UVa 10465 - Homer Simpson
    查看>>
    php九九乘法表加粗,PHP九九乘法表
    查看>>
    PHP二维数组将重复键值合并重组成三维数组
    查看>>
    PHP二维数组转换为一维数组
    查看>>
    PHP二维数组重组
    查看>>
    PHP交换两个变量值
    查看>>
    php代码执行完整流程介绍
    查看>>
    PHP代码格式化工具phpcf常见问题解决方案
    查看>>
    PHP使用3DES算法加密解密字符串
    查看>>
    PHP使用curl multi要注意的问题:每次使用curl multi同时并发多少请求合适
    查看>>
    php使用memcached扩展的一个BUG
    查看>>
    PHP入门part1
    查看>>
    PHP兼容性检查,PHP升级语法检查(PHPCompatibility+PHP_CodeSniffer)
    查看>>
    PHP内核介绍及扩展开发指南—基础知识
    查看>>
    php内核基础说明
    查看>>
    PHP写日志fwrite和file_put_contents的区别与性能
    查看>>
    PHP写计划任务
    查看>>
    PHP出现Notice: unserialize() [function.unserialize]: Error at offset问题的解决方案
    查看>>
    PHP函数
    查看>>
    React input defaultValue不会更新状态怎么办?
    查看>>