需要在spring的拦截器中使用自定义的服务,这要就设计到将服务注入到拦截器中。网上看的情况有两种:

  1、

 @Configuration
public class OptPermissionHandlerInterceptor extends HandlerInterceptorAdapter {
private Logger logger = LoggerFactory.getLogger(OptPermissionHandlerInterceptor.class); @Autowired
private OperatorLogService operatorLogService; //这里使用@Autowired无法注入成功 @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (true) {
return true;
} else {
String result = "当前登录用户无权限!";
response.getOutputStream().write(result.getBytes());
response.setStatus(HttpStatus.OK.value());
return false;
}
} @SuppressWarnings("rawtypes")
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
try {
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
String beanName = handlerMethod.getBean().getClass().toString();
String methodName = handlerMethod.getMethod().getName();
String uri = request.getRequestURI();
String remoteAddr = request.getRemoteAddr();
String sessionId = request.getSession().getId(); OperatorLog optLog = new OperatorLog();
optLog.setBeanName(beanName);
optLog.setMethodName(methodName);
optLog.setRemoteAddr(remoteAddr);
optLog.setSessionId(sessionId);
optLog.setUri(uri); if (operatorLogService == null) {//解决service为null无法注入问题
System.out.println("operatorLogService is null!!!");
BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
operatorLogService = (OperatorLogService) factory.getBean("operatorLogService");
}
operatorLogService.saveOperatorLog(optLog);
}
} catch (Exception e) {
logger.error("", e);
}
} @Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
Exception ex) throws Exception { } }

  2、

 @Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(sessionInterceptor())
.addPathPatterns("/**")
.excludePathPatterns( "/push/**");
super.addInterceptors(registry);
} @Bean
public SessionInterceptor sessionInterceptor() {
return new SessionInterceptor();
}

  第二种的方式中 sessionInterceptor类中也可以使用:@Configuration,总之就是需要让spring进行管理。

spring拦截器中使用spring的自动注入的更多相关文章

  1. Spring拦截器中通过request获取到该请求对应Controller中的method对象

    背景:项目使用Spring 3.1.0.RELEASE,从dao到Controller层全部是基于注解配置.我的需求是想在自定义的Spring拦截器中通过request获取到该请求对应于Control ...

  2. spring拦截器中修改响应消息头

    问题描述 前后端分离的项目,前端使用Vue,后端使用Spring MVC. 显然,需要解决浏览器跨域访问数据限制的问题,在此使用CROS协议解决. 由于该项目我在中期加入的,主要负责集成shiro框架 ...

  3. 如何在Struts2的拦截器中调用Spring容器

    第一种: 通常用ApplicationContext来调用Spring配置文件中的一些Bean,所以首先创建Spring上下文容器. ApplicationContext ac = (Applicat ...

  4. 在spring拦截器中response输出html标签到页面

    @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object ...

  5. spring boot拦截器中获取request post请求中的参数

    最近有一个需要从拦截器中获取post请求的参数的需求,这里记录一下处理过程中出现的问题. 首先想到的就是request.getParameter(String )方法,但是这个方法只能在get请求中取 ...

  6. 【spring boot】在自定义拦截器中从request中获取json字符串

    又这样的需求,需要在自定义的拦截器中获取request中的数据,想获取到的是JSON字符串 那需要在拦截器中写这样一个方法 public static String getOpenApiRequest ...

  7. spring拦截器的定义

    (一).拦截器的定义 1.为什么需要拦截器:在做身份认证或者是进行日志的记录时,我们需要通过拦截器达到我们的目的 2.什么事拦截器:在AOP(Aspect-Oriented Programming)中 ...

  8. Spring拦截器总结

    本文是对慕课网上"搞定SSM开发"路径的系列课程的总结,详细的项目文档和课程总结放在github上了.点击查看 Spring过滤器WebFilter可以配置中文过滤 拦截器实现步骤 ...

  9. spring拦截器-过滤器的区别

    1.  理解 拦截器 :是在面向切面编程的时候,在你的 service 或者一个方法前调用一个方法,或者在方法后调用一个方法:比如动态代理就是拦截器的简单实现,在你调用方法前打印出字符串(或者做其它业 ...

随机推荐

  1. Rendering React components to the document body

    React一个比较好用的功能是其简单的API,一个组件可以简单到一个return了组件结构的render函数.除了一个简单的函数之外,我们还有了一段有用且可复用的代码片段. 问题 不过有时候可能会受到 ...

  2. 读取XML文件内容

    myeclipse中类的格式 上面中的RunMain.java为程序执行的入口,JdbcUtil.java为实体类,XmlDocumentUtil.java执行解释xml文件与获取里面的属性,程序所需 ...

  3. 平衡二叉树的java实现

    转载请注明出处! 一.概念 平衡二叉树是一种特殊的二叉搜索树,关于二叉搜索树,请查看上一篇博客二叉搜索树的java实现,那它有什么特别的地方呢,了解二叉搜索树的基本都清楚,在按顺序向插入二叉搜索树中插 ...

  4. jmeter使用Beanshell预处理器从指定列表中获取随机值

    变量mynation从列表{"china", "US", "UK"}中随机取值 String[] nation = new String[] ...

  5. VMware5.5-添加数据中心,集群及主机

    首先介绍下硬盘的三种设置格式,这个要搞清楚 虚机硬盘的三种格式 厚置备延迟置零 厚置备,分配10g空间,虚拟机没开机时就分配了: 延迟置零,没开机之前只分配空间,里面的数据不抹零,当开机写入数据时再抹 ...

  6. javascript 伪数组和转化为标准数组

    1: 什么是伪数组 伪数组是一个含有length属性的json对象, 它是按照索引的方式存储数据, 它并不具有数组的一些方法,只能能通过Array.prototype.slice转换为真正的数组,并且 ...

  7. Leetcode 记录(1~100)

    5.回文串 几种方法: 暴力:枚举每一个字串,判断是否为回文串,复杂度O(n^3),暴力月莫不可取 dp:区间dp思想,O(n^2) 中心扩展:找每一个字符,然后往两边扩展,O(n^2) manach ...

  8. [USACO18DEC]The Cow Gathering

    Description: 给定一棵树,每次删去叶子,有m个限制,分别为(a,b)表示a需要比b先删,为每个点能否成为最后被删的点 Hint: \(n,m \le 10^5\) Solution: 手模 ...

  9. 种花 [JZOJ4726] [可撤销贪心]

    Description 经过三十多个小时的长途跋涉,小Z和小D终于到了NOI现场——南山南中学.一进校园,小D就被花所吸引了(不要问我为什么),遍和一旁的种花园丁交(J)流(L)了起来. 他发现花的摆 ...

  10. PAT基础6-1

    6-1 简单输出整数 (10 分) 本题要求实现一个函数,对给定的正整数N,打印从1到N的全部正整数. 函数接口定义: void PrintN ( int N ); 其中N是用户传入的参数.该函数必须 ...