Commit 43d5f7ef authored by wangsong's avatar wangsong

add log

parent 6022f8e5
package com.chineseall.eden.authcenter.agent.interceptor;
import com.chineseall.eden.authcenter.log.util.RequestResponseHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class AccessInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
RequestResponseHolder.set(httpServletRequest, httpServletResponse);
return true;
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
RequestResponseHolder.remove();
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
}
}
......@@ -14,11 +14,18 @@ public class ApiInterceptorConfig extends WebMvcConfigurerAdapter {
@Autowired
ApiInterceptor apiInterceptor;
@Autowired
AccessInterceptor accessInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
//注册TestInterceptor拦截器
InterceptorRegistration accessRegistration = registry.addInterceptor(accessInterceptor);
accessRegistration.addPathPatterns("/**");
InterceptorRegistration registration = registry.addInterceptor(apiInterceptor);
registration.addPathPatterns("/config/**"); //
}
@Override
......
......@@ -94,6 +94,12 @@
<type>jar</type>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.22</version>
</dependency>
</dependencies>
......
package com.chineseall.eden.authcenter.log.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.sh.chineseall.framework.core.util.CollectionUtils;
import com.alibaba.fastjson.JSON;
import com.chineseall.eden.authcenter.log.dao.AuthLogDao;
......@@ -7,6 +8,7 @@ import com.chineseall.eden.authcenter.log.model.AuthLog;
import com.chineseall.eden.authcenter.log.model.AuthLogHourCount;
import com.chineseall.eden.authcenter.log.service.AuthLogService;
import com.chineseall.eden.authcenter.log.util.BusinessLogUtils;
import com.chineseall.eden.authcenter.log.util.RequestResponseHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -28,13 +30,24 @@ public class AuthLogServiceImpl implements AuthLogService {
if(authLog.getId() != null){
authLog.setUpdateTime(new Date());
authLogDao.replace(authLog);
BusinessLogUtils.info(JSON.toJSONString(authLog));
} else{
authLog.setId(UUID.randomUUID().toString());
authLog.setCreateTime(new Date());
authLogDao.insert(authLog);
BusinessLogUtils.info(JSON.toJSONString(authLog));
}
addBusinessLog(authLog);
}
private void addBusinessLog(AuthLog authLog){
RequestResponseHolder holder = RequestResponseHolder.get();
Map<String, Object> dataMap = BeanUtil.beanToMap(authLog);
dataMap.put("user-agent", holder.getRequestUserAgent());
dataMap.put("remote-address", holder.getRequestRemoteAddress());
dataMap.put("referer", holder.getRequestReferer());
dataMap.put("ip", holder.getIpAddress());
dataMap.put("request", holder.getRequestMethod() + " " + holder.getRequestContextPath());
dataMap.put("timestamp", System.currentTimeMillis());
BusinessLogUtils.info(JSON.toJSONString(dataMap));
}
@Override
......
package com.chineseall.eden.authcenter.log.util;
import cn.sh.chineseall.framework.core.repackaged.org.apache.commons.lang3.StringUtils;
import lombok.Getter;
import lombok.Setter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
@Getter
@Setter
public class RequestResponseHolder {
public static final ThreadLocal<RequestResponseHolder> requestResponseLocal = new ThreadLocal<>();
private HttpServletRequest request;
private HttpServletResponse response;
public Map<String, String> getRequestHeaderMap(){
Map<String, String> requestHeaderMap = new HashMap<>();
Enumeration<String> headerNames = getRequest().getHeaderNames();
while (headerNames.hasMoreElements()){
String headerName = headerNames.nextElement();
String value = getRequest().getHeader(headerName);
requestHeaderMap.put(headerName, value);
}
return requestHeaderMap;
}
public String getRequestUserAgent(){
Map<String, String> headerMap = getRequestHeaderMap();
return headerMap.get("user-agent");
}
public String getRequestRemoteAddress(){
return getRequest().getRemoteAddr();
}
public String getRequestReferer(){
Map<String, String> headerMap = getRequestHeaderMap();
return headerMap.get("referer");
}
public String getRequestMethod(){
return getRequest().getMethod();
}
public String getRequestContextPath(){
return getRequest().getContextPath();
}
public String getIpAddress() {
Map<String, String> headerMap = getRequestHeaderMap();
String Xip = headerMap.get("X-Real-IP");
String XFor = headerMap.get("X-Forwarded-For");
if(StringUtils.isNotEmpty(XFor) && !"unKnown".equalsIgnoreCase(XFor)){
//多次反向代理后会有多个ip值,第一个ip才是真实ip
int index = XFor.indexOf(",");
if(index != -1){
return XFor.substring(0,index);
}else{
return XFor;
}
}
XFor = Xip;
if(StringUtils.isNotEmpty(XFor) && !"unKnown".equalsIgnoreCase(XFor)){
return XFor;
}
if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
XFor = request.getHeader("Proxy-Client-IP");
}
if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
XFor = request.getHeader("WL-Proxy-Client-IP");
}
if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
XFor = request.getHeader("HTTP_CLIENT_IP");
}
if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
XFor = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
XFor = request.getRemoteAddr();
}
return XFor;
}
public static void set(HttpServletRequest request, HttpServletResponse response){
RequestResponseHolder holder = new RequestResponseHolder();
holder.setRequest(request);
holder.setResponse(response);
requestResponseLocal.set(holder);
}
public static RequestResponseHolder get(){
return requestResponseLocal.get();
}
public static void remove(){
requestResponseLocal.remove();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment