1.添加拦截器

package com.jy.config;import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import yaycrawler.common.interceptor.SignatureSecurityInterceptor;@Configuration
public class WebAppConfig extends WebMvcConfigurerAdapter {/**添加拦截器
  
WebMvcConfigurerAdapter 显示过期,则使用继承至org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport
*/ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new MySignatureSecurityInterceptor()).addPathPatterns("/admin/**","/manager/**"); } }

2、创建监听器

package com.jy.listener;import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.web.context.WebApplicationContext;/*** * 启动worker监听器 */
public class MyRegisterListener implements ApplicationListener<ContextRefreshedEvent> {@Overridepublic void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {WebApplicationContext context = (WebApplicationContext) contextRefreshedEvent.getApplicationContext();WorkerContext.webApplicationContext = context;Userservice userservice = context.getBean(Userservice.class);//做其他操作}
}

3、添加监听器

package com.jy;import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import yaycrawler.worker.listener.WorkerRegisterListener;/*** 启动时,向master注册 */
public class ServletInitializer extends SpringBootServletInitializer {@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder application) {    //如上第二部创建的监听器application.listeners(new MyRegisterListener());return application.sources(Application.class);}}

4、项目启动时初始化操作(例如初始化项目根路径)

package com.jy.context;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;import java.io.File;
import java.io.FileNotFoundException;@Component
public class MyContextAware implements ApplicationContextAware {private Logger logger = LoggerFactory.getLogger(MyContextAware.class);@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {//获取跟目录File path = null;try {path = new File(ResourceUtils.getURL("classpath:").getPath());if(!path.exists()) {path = new File("");}//自行定义的类Global,ROOT为static 变量;项目启动时初始化项目路径 Global.ROOT = path.getAbsolutePath()+File.separator;/*在开发测试模式时,得到的地址为:{项目跟目录}/target/classes/在打包成jar正式发布时,得到的地址为:{发布jar包目录}/*/} catch (FileNotFoundException e) {logger.error("FileNotFoundException",e);}logger.info("Global.ROOT {}",Global.ROOT);}}

转载于:https://www.cnblogs.com/northern-light/p/10498551.html

springboot添加拦截器 监听等设置相关推荐

  1. springboot 添加拦截器之后中文乱码_spring boot 2.x 添加拦截器配置未生效的问题

    背景: 今天有一个需求需要拦截除登录相关请求以外的所有请求,并查看request 中是否包含指定的信息,而自然就想到了使用拦截器就可以轻松实现 编写拦截器,获取请求头信息中的test,并打印出来 @C ...

  2. 玩转springboot:实现springboot自定义拦截器

    一.前言 在javaee中,我们经常使用filter来做拦截器,后来有了springmvc,我们使用HandlerInterceptor进行拦截,springmvc的拦截器查看这篇文章,现在有了spr ...

  3. springboot Interceptor拦截器excludePathPatterns失效

    springboot Interceptor拦截器excludePathPatterns失效 使用jwt和HandlerInterceptorAdapter做登录的拦截放行失效,其实更多时候不是配置文 ...

  4. springboot项目拦截器中获取接口返回数据_Spring Boot自定义Annotation实现接口自动幂...

    在实际的开发项目中,一个对外暴露的接口往往会面临很多次请求,我们来解释一下幂等的概念:任意多次执行所产生的影响均与一次执行的影响相同.按照这个含义,最终的含义就是 对数据库的影响只能是一次性的,不能重 ...

  5. 拦截器 java_在Java后端如何添加拦截器

    (1)InterceptorConfig.java文件内容如下: import org.springframework.web.servlet.config.annotation.WebMvcConf ...

  6. cxf添加拦截器_在CXF API和拦截器中添加Gzip压缩

    cxf添加拦截器 如今,由于我们在响应中发送大量数据,因此必须对API响应执行Gziping. 它节省了网络带宽和交付时间,当然还节省了Internet上的空间. CXF提供了以多种方式使用Gzip压 ...

  7. token拦截器android_vue.js添加拦截器,实现token认证(使用axios)

    什么是token? token是一个用户自定义的任意字符串,目前开发中,token都是在服务端生成并且token的值会保存到服务器后台.只有服务器和客户端知道这个字符串,于是,这个token就成了两者 ...

  8. SpringBoot与拦截器

    自定义拦截器 package com.buba.interceptor;import com.buba.pojo.User; import org.springframework.web.servle ...

  9. SpringBoot配置拦截器导致swagger失效

    问题背景: Springboot 启用拦截器后,Swagger无法访问 原因 拦截器拦截了所有的请求,导致swagger也被拦截,当在进行鉴权的的时候,可能需要传入一些特定的参数,或者请求头信息,这样 ...

最新文章

  1. PHP SSL certificate: unable to get local issuer certificate的解决办法
  2. 7小时44分最佳,睡多睡少都不好,养成习惯也没用
  3. 大数据时代的公共安全治理
  4. 程序员面试金典 - 面试题 04.05. 合法二叉搜索树(中序遍历)
  5. 现代软件工程 2012 北航 项目复审模板
  6. python线程进程协程面试_Python学习经验之谈:关于协程的理解和其相关面试问题...
  7. 为什么用c语言程序中的if语句实现从1加到100最后的结果是负数,用C语言程式计算从1加到100的程式是怎样的?...
  8. java跳_用Java实现跳表
  9. C语言编程比赛WBS
  10. 下面是一个python函数、哪个说法不正确_2017秋python语言程序设计(北京信息科技大学)答案...
  11. git push 报错 pre-receive hook declined
  12. MST703调试群创AT070TN92
  13. SUDA歌单管理软件,支持Spotify\Tidal\QQ\网易云导入导出歌单
  14. 小蓝本 第一本《因式分解技巧》第三章 分组分解 笔记(第三天)
  15. flash player 11.2 64位 linux,Flash Player 11.2新版发布
  16. 贝尔曼-福特算法(Bellman-Ford)
  17. 【camera】【摄像头模组】摄像头模组简单介绍
  18. mysql删除的表格怎么还原_mysql删除的表格怎么恢复数据
  19. 8.时间频度和时间复杂度
  20. 用pygame写了个俄罗斯方块

热门文章

  1. 计算机硬件课题,计算机硬件技术基础免试课题.pptx
  2. alphac测试和bata测试区别_康一康!接口测试与性能测试的区别瞧过来~
  3. python百度贴吧怎么爬取最早的帖子_Python爬虫爬取百度贴吧的帖子
  4. c语言求前缀后缀大全,温故而知新_C语言_前缀++(--)和后缀++(--)
  5. java两个和三个_H2DB和Java,大约两个小时的差异
  6. linux清楚保存的密钥文件夹,在Ubuntu系统中删除存储库和GPG密钥的方法
  7. 在Vue 项目 webpack 打包中关于 背景图片的路径问题
  8. 1. BeeGo 介绍与项目的创建,启动
  9. 在CentOS 6.7部署wordpress博客系统Discuz论坛系统
  10. 从零开始部署基于阿里容器云的微服务(consul+registrator+template)(一)