spring拦截器中使用spring的自动注入
需要在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的自动注入的更多相关文章
- Spring拦截器中通过request获取到该请求对应Controller中的method对象
背景:项目使用Spring 3.1.0.RELEASE,从dao到Controller层全部是基于注解配置.我的需求是想在自定义的Spring拦截器中通过request获取到该请求对应于Control ...
- spring拦截器中修改响应消息头
问题描述 前后端分离的项目,前端使用Vue,后端使用Spring MVC. 显然,需要解决浏览器跨域访问数据限制的问题,在此使用CROS协议解决. 由于该项目我在中期加入的,主要负责集成shiro框架 ...
- 如何在Struts2的拦截器中调用Spring容器
第一种: 通常用ApplicationContext来调用Spring配置文件中的一些Bean,所以首先创建Spring上下文容器. ApplicationContext ac = (Applicat ...
- 在spring拦截器中response输出html标签到页面
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object ...
- spring boot拦截器中获取request post请求中的参数
最近有一个需要从拦截器中获取post请求的参数的需求,这里记录一下处理过程中出现的问题. 首先想到的就是request.getParameter(String )方法,但是这个方法只能在get请求中取 ...
- 【spring boot】在自定义拦截器中从request中获取json字符串
又这样的需求,需要在自定义的拦截器中获取request中的数据,想获取到的是JSON字符串 那需要在拦截器中写这样一个方法 public static String getOpenApiRequest ...
- spring拦截器的定义
(一).拦截器的定义 1.为什么需要拦截器:在做身份认证或者是进行日志的记录时,我们需要通过拦截器达到我们的目的 2.什么事拦截器:在AOP(Aspect-Oriented Programming)中 ...
- Spring拦截器总结
本文是对慕课网上"搞定SSM开发"路径的系列课程的总结,详细的项目文档和课程总结放在github上了.点击查看 Spring过滤器WebFilter可以配置中文过滤 拦截器实现步骤 ...
- spring拦截器-过滤器的区别
1. 理解 拦截器 :是在面向切面编程的时候,在你的 service 或者一个方法前调用一个方法,或者在方法后调用一个方法:比如动态代理就是拦截器的简单实现,在你调用方法前打印出字符串(或者做其它业 ...
随机推荐
- JavaEE 之 文件上传
1.文件上传 a.配置mySpring-servlet.xml <bean id="multipartResolver" class="org.springfram ...
- android studio打可执行jar包
android studio可以通过library工程打出jar包 解压会看到META-INF/MANIFEST.MF文件的打开如下: Manifest-Version: 1.0 增加一行,注意冒号后 ...
- git&github快速掌握
git&github快速掌握 安装git 版本库创建 代码修改并提交 代码回滚 工作区和暂存区 撤销操作 删除操作 更多操作 Windows下安装git https://gitforwindo ...
- spring注解工具类AnnotatedElementUtils和AnnotationUtils
一.前言 spring为开发人员提供了两个搜索注解的工具类,分别是AnnotatedElementUtils和AnnotationUtils.在使用的时候,总是傻傻分不清,什么情况下使用哪一个.于是我 ...
- Ignatius and the Princess III HDU - 1028 -生成函数or完全背包计数
HDU - 1028 step 1:初始化第一个多项式 也就是 由 1的各种方案 组 成 的多项式 初始化系数为 1.临时区 temp初始化 为 0 step 2:遍历后续的n - 1 个 多项式 , ...
- RMQ问题 [luogu 3865]
原题地址 ST表模板题,尝试用单点修改线段树. 原本以为线段树会被卡掉,但是还是险过了.实践证明,快速读入很有用. #include<bits/stdc++.h> using namesp ...
- [POI2012]Tour de Bajtocja
[POI2012]Tour de Bajtocja 题目大意: 给定一个\(n(n\le10^6)\)个点\(m(m\le2\times10^6)\)条边的无向图,问最少删掉多少条边能使得编号小于等于 ...
- Struts2网页面传值两种方式
第一种方式: /** 列表 */ public String list() throws Exception { List<Role> roleList = roleService.fin ...
- IDEA+Maven:cannot download sources
把IDEA的maven换成maven2
- C Windows控制台字符版本俄罗斯方块
//一个可以工作在Windows控制台字符界面下的俄罗斯方块 //工作在非图形模式,无需其他库依赖,单个C文件代码即可运行 //支持最高纪录,并且对于纪录进行了加密 //By wrule 2015年1 ...