需要在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. Addition Chains POJ - 2248 (bfs / dfs / 迭代加深)

    An addition chain for n is an integer sequence <a0, a1,a2,...,am=""> with the follow ...

  2. Best Cow Fences POJ - 2018 (二分)

    Farmer John's farm consists of a long row of N (1 <= N <= 100,000)fields. Each field contains ...

  3. HDU 4635 Strongly connected (强连通分量+缩点)

    <题目链接> 题目大意: 给你一张有向图,问在保证该图不能成为强连通图的条件下,最多能够添加几条有向边. 解题分析: 我们从反面思考,在该图是一张有向完全图的情况下,最少删去几条边能够使其 ...

  4. HDU 4612 Warm up (边双连通分量+缩点+树的直径)

    <题目链接> 题目大意:给出一个连通图,问你在这个连通图上加一条边,使该连通图的桥的数量最小,输出最少的桥的数量. 解题分析: 首先,通过Tarjan缩点,将该图缩成一颗树,树上的每个节点 ...

  5. 使用IntelliJ Idea新建SpringBoot项目

    简单给大家介绍一下我来创建SpringBoot项目使用的工具,本人使用IntelliJ Idea来创建项目,利用其中的Spring Initializr工具来快速创建项目. 步骤如下: 菜单栏中选择F ...

  6. VMware6.0-vCenter的安装准备及安装

    由于6.0的VCSA安装需要跳板主机来辅助,而在5.5时时可用用OVA导入模式安装的. 首先安装跳板插件 安装完成后,点击setup链接. 设置 Single Sign-On (SSO),将root和 ...

  7. 关于java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to 实体类

    由于业务逻辑的复杂,有些sql语句写法hql不支持,所以hibernate查询直接用了sql原生查询,由于数据结果根据四个表查询出来,所以无法使用方法.addEntity(XXXXXXX.class) ...

  8. 2554 ACM 杭电 数学

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2554 中文题目,题意易懂.但是本题涉及到很强的数学思维. 思路:看了题意后:我的第一反应是除了 n=1,n ...

  9. python网络编程(十三)

    协程-greenlet版 为了更好使用协程来完成多任务,python中的greenlet模块对其封装,从而使得切换任务变的更加简单 安装方式 使用如下命令安装greenlet模块: sudo pip ...

  10. Making the Grade [POJ3666] [DP]

    题意: 给定一个序列,以最小代价将其变成单调不增或单调不减序列,代价为Σabs(i变化后-i变化前),序列长度<=2000,单个数字<=1e9 输入:(第一行表示序列长度,之后一行一个表示 ...