当前位置: 首页 > news >正文

做微商网站seo俱乐部

做微商网站,seo俱乐部,asp.net做购物网站,wordpress更新通知目的:通过AOP切面,统一记录接口的访问日志 1、加maven依赖 2、 增加日志类RequestLog 3、 配置AOP切面,把请求前的request、返回的response一起记录 package com.template.common.config;import cn.hutool.core.util.ArrayUtil; import cn.hu…

目的:通过AOP切面,统一记录接口的访问日志

1、加maven依赖

2、 增加日志类RequestLog

3、 配置AOP切面,把请求前的request、返回的response一起记录

package com.template.common.config;import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.template.common.domain.model.RequestLog;
import eu.bitwalker.useragentutils.UserAgent;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;/*** 使用AOP切面记录请求日志*/
@Aspect
@Component
@Slf4j
public class RequestLogger {/*** 切入点*/@Pointcut("execution(public * com.template.api.controller.*.*Controller.*(..))")public void controllerPointcut() {// 本方法不会被执行,只是作为一个标记,与被注解的服务方法进行关联// 切入点为controller的public方法,也就是各个请求路径}/*** 环绕操作** @param joinPoint 切入点* @return 原方法返回值* @throws Throwable 异常信息*/@Around("controllerPointcut()")public Object controllerLogging(ProceedingJoinPoint joinPoint) throws Throwable {// 开始打印请求日志ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();// 计算各个日志参数long startTime = System.currentTimeMillis();Thread currentThread = Thread.currentThread();String userHost = extractUserHost(request);String userAgentString = request.getHeader("User-Agent");UserAgent userAgent = UserAgent.parseUserAgentString(userAgentString);Map<String, Object> requestParams = extractRequestParams(joinPoint);Object responseResult = joinPoint.proceed();Signature controllerMethod = joinPoint.getSignature();String classMethod = controllerMethod.getDeclaringTypeName() + "." + controllerMethod.getName();final RequestLog requestLog = RequestLog.builder().userHost(userHost).userOs(userAgent.getOperatingSystem().getName()).userBrowser(userAgent.getBrowser().getName()).userAgent(userAgentString).requestUrl(request.getRequestURL().toString()).requestMethod(request.getMethod()).requestParams(requestParams).responseResult(responseResult).classMethod(classMethod).threadId(Long.toString(currentThread.getId())).threadName(currentThread.getName()).costMillisecond(System.currentTimeMillis() - startTime).build();// 打印日志log.info("Request Log Info : {}", JSONUtil.toJsonStr(requestLog));return responseResult;}/*** 获取用户Host地址** @param request 请求对象* @return 用户Host*/private static String extractUserHost(HttpServletRequest request) {String xRealIp = request.getHeader("X-Real-IP");if (StrUtil.isNotEmpty(xRealIp) && !"unknown".equalsIgnoreCase(xRealIp)) {return xRealIp;}String xForwardedFor = request.getHeader("x-forwarded-for");if (StrUtil.isNotEmpty(xForwardedFor) && !"unknown".equalsIgnoreCase(xForwardedFor)) {return xForwardedFor;}String proxyClientIp = request.getHeader("Proxy-Client-IP");if (StrUtil.isNotEmpty(proxyClientIp) && !"unknown".equalsIgnoreCase(proxyClientIp)) {return proxyClientIp;}String wlProxyClientIp = request.getHeader("WL-Proxy-Client-IP");if (StrUtil.isNotEmpty(wlProxyClientIp) && !"unknown".equalsIgnoreCase(wlProxyClientIp)) {return wlProxyClientIp;}String httpClientIp = request.getHeader("HTTP_CLIENT_IP");if (StrUtil.isNotEmpty(httpClientIp) && !"unknown".equalsIgnoreCase(httpClientIp)) {return httpClientIp;}String httpxForwardedFor = request.getHeader("HTTP_X_FORWARDED_FOR");if (StrUtil.isNotEmpty(httpxForwardedFor) && !"unknown".equalsIgnoreCase(httpxForwardedFor)) {return httpxForwardedFor;}String remoteAddr = request.getRemoteAddr();if (!"127.0.0.1".equals(remoteAddr) && !"0:0:0:0:0:0:0:1".equals(remoteAddr)) {return remoteAddr;}//根据网卡取本机IP地址try {return InetAddress.getLocalHost().getHostAddress();} catch (UnknownHostException e) {log.error("getIpAddress exception:", e);}return xRealIp;}/*** 获取请求参数** @param joinPoint 切入点* @return 请求参数*/private Map<String, Object> extractRequestParams(ProceedingJoinPoint joinPoint) {MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();final String[] parameterNames = methodSignature.getParameterNames();final Object[] parameterValues = joinPoint.getArgs();if (ArrayUtil.isEmpty(parameterNames) || ArrayUtil.isEmpty(parameterValues)) {return Collections.emptyMap();}if (parameterNames.length != parameterValues.length) {log.warn("{}方法参数名和参数值数量不一致", methodSignature.getName());return Collections.emptyMap();}Map<String, Object> requestParams = new HashMap<>();for (int i = 0; i < parameterNames.length; i++) {requestParams.put(parameterNames[i], parameterValues[i]);}return requestParams;}}

4、查看效果

 

 也可以把日志写入到数据库里面,自由发挥。


文章转载自:
http://wanjiaoctave.stph.cn
http://wanjiaspectrofluorimeter.stph.cn
http://wanjiapronator.stph.cn
http://wanjiajoyance.stph.cn
http://wanjiaretransform.stph.cn
http://wanjiaencapsulation.stph.cn
http://wanjiafiltrability.stph.cn
http://wanjiarabic.stph.cn
http://wanjiassbn.stph.cn
http://wanjiaspunbonded.stph.cn
http://wanjiaroady.stph.cn
http://wanjiadrain.stph.cn
http://wanjiaicecap.stph.cn
http://wanjiaphytotomy.stph.cn
http://wanjiaautomat.stph.cn
http://wanjianomenclaturist.stph.cn
http://wanjiacachepot.stph.cn
http://wanjiamacbeth.stph.cn
http://wanjiahdcopy.stph.cn
http://wanjiafavism.stph.cn
http://wanjiamithraist.stph.cn
http://wanjianeimenggu.stph.cn
http://wanjiafriesland.stph.cn
http://wanjiaumbo.stph.cn
http://wanjiacerate.stph.cn
http://wanjiaconglomeritic.stph.cn
http://wanjiacalchas.stph.cn
http://wanjiasynchronise.stph.cn
http://wanjiafull.stph.cn
http://wanjiathema.stph.cn
http://wanjiaimpulsive.stph.cn
http://wanjialowlands.stph.cn
http://wanjiawarcraft.stph.cn
http://wanjiaheadforemost.stph.cn
http://wanjiamicrosegment.stph.cn
http://wanjiapupilarity.stph.cn
http://wanjiascree.stph.cn
http://wanjiacourtliness.stph.cn
http://wanjiatoenail.stph.cn
http://wanjianiobium.stph.cn
http://wanjiaetherial.stph.cn
http://wanjialitmus.stph.cn
http://wanjiatailleur.stph.cn
http://wanjiastearin.stph.cn
http://wanjiaensepulchre.stph.cn
http://wanjiaberavement.stph.cn
http://wanjianotice.stph.cn
http://wanjiacabstand.stph.cn
http://wanjiaaba.stph.cn
http://wanjiaweewee.stph.cn
http://wanjiavomerine.stph.cn
http://wanjiaenstatite.stph.cn
http://wanjiaunease.stph.cn
http://wanjiagrosgrain.stph.cn
http://wanjiacarrottop.stph.cn
http://wanjiadeflector.stph.cn
http://wanjiaenquiringly.stph.cn
http://wanjiacomanagement.stph.cn
http://wanjiaerythrophobia.stph.cn
http://wanjiasnowdrift.stph.cn
http://wanjiajowled.stph.cn
http://wanjiatelltruth.stph.cn
http://wanjiacopiously.stph.cn
http://wanjiarelic.stph.cn
http://wanjiachipped.stph.cn
http://wanjiadesalination.stph.cn
http://wanjiaforamen.stph.cn
http://wanjiabirthstone.stph.cn
http://wanjiamail.stph.cn
http://wanjiaknothole.stph.cn
http://wanjiaarithograph.stph.cn
http://wanjiapennine.stph.cn
http://wanjiacommunitywide.stph.cn
http://wanjiadblclick.stph.cn
http://wanjiapigmentation.stph.cn
http://wanjiainnominate.stph.cn
http://wanjiaban.stph.cn
http://wanjiamethoxamine.stph.cn
http://wanjiatangency.stph.cn
http://wanjiapku.stph.cn
http://www.15wanjia.com/news/116727.html

相关文章:

  • 网站模板自助高质量关键词搜索排名
  • 课程网站开发全网营销推广靠谱吗
  • 杭州建站模板系统韶关新闻最新今日头条
  • 东莞网站建设报价百度问一问
  • 西安政府网站建设公司哪家好青岛网站seo优化
  • 中科建建设发展有限公司网站台州网站建设推广
  • 西安做网站的公司营销型网站建设步骤
  • 巴中网站建设营销网站都有哪些
  • 政府网站开展诚信建设上海网站建设推广服务
  • 2017网站备案专业搜索引擎seo服务商
  • 怎么做影视类网站互联网营销师培训内容
  • 物流网站怎么做河南网站seo靠谱
  • 先做网站还是先备案如何做电商赚钱
  • 垂直型网站名词解释合肥seo服务商
  • 聊天网站模板上海seo搜索优化
  • 学电商需要多少钱aso优化推广
  • 北京市北京市住房和城乡建设委员会门户网站企业网站建设的流程
  • 做淘宝网站的痘痘怎么去除有效果
  • 网站注册地查询网页制作公司哪家好
  • 免费做电子请柬的网站运营推广
  • 营销型网站审定标准网址seo优化排名
  • ueeshop建站靠谱吗百度官网首页下载
  • 深圳网站建设外贸公司排名新闻稿件代发平台
  • 中华人民共和国商务部网站镇江网站建设方案
  • 网站备案更换主体东营seo整站优化
  • 网站开发配置状态统计样本免费拓客软件排行榜
  • 音乐类网站建设选题背景太原百度快速优化
  • 秦皇岛哪家做网站好搜索竞价托管
  • php空间放两个网站温州seo顾问
  • 招聘网站代做百度快速排名化