Spring MVC拦截器实现,记录访问请求日志
1.创建拦截器类并实现HandlerInterceptor拦截器
package com.jxdinfo.hussar.sys.interceptor;
import com.jxdinfo.hussar.base.entity.UserInfo;
import com.jxdinfo.hussar.common.util.UserUtils;
import com.jxdinfo.hussar.core.util.IdGenerator;
import com.jxdinfo.hussar.sys.dto.OperateLog;
import com.jxdinfo.hussar.sys.mapper.OperateLogMapper;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executor;
public class RequestInterceptor implements HandlerInterceptor {
//环境
private Environment env;
private Executor executor;
private OperateLogMapper operateLogMapper;
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public RequestInterceptor(Environment env,OperateLogMapper operateLogMapper) {
this.env = env;
this.operateLogMapper = operateLogMapper;
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
try {
if (handler instanceof ResourceHttpRequestHandler) {
// 处理静态资源的请求
return true;
}
//其他请求
UserInfo user = UserUtils.getUser();
OperateLog log = new OperateLog();
log.setId(IdGenerator.getId());
log.setRequestUri(request.getRequestURI());
log.setIp(InetAddress.getLocalHost().getHostAddress());
log.setLoginName(user.getLoginName());
log.setUserId(user.getId());
log.setUserName(user.getUserName());
log.setStartTime(dateFormat.format(new Date()));
operateLogMapper.insertLog(log);
} catch (Exception e) {
e.printStackTrace();
} finally {
return true;
}
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
Exception ex) throws Exception {
try {
if (handler instanceof ResourceHttpRequestHandler) {
// 处理静态资源的请求
} else {
// 处理其他请求
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
2.注册拦截器(需实现WebMvcConfigurer)
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import javax.annotation.Resource;
import java.util.concurrent.Executor;
@Configuration
public class AppConfig implements WebMvcConfigurer {
@Autowired
private Environment env;
@Autowired
private JdbcTemplate jdbcTemplate;
@Resource(name = "requestLogThreadPool")
private Executor taskExecutor;
@Value("${requestLog.onState:false}")
private Boolean requestLogOnState;
@Override
public void addInterceptors(InterceptorRegistry registry) {
//根据配置文件中的参数决定是否开启日志记录功能
if (requestLogOnState) {
InterceptorRegistration interceptorRegistration = registry.addInterceptor(new RequestInterceptor(env, jdbcTemplate, taskExecutor));
interceptorRegistration.addPathPatterns("/**");
interceptorRegistration.excludePathPatterns("/static/**");
}
}
}
标签:拦截器,log,Spring,springframework,MVC,private,import,org,servlet
From: https://www.cnblogs.com/huoxiaoguo/p/18155644