request.getRequestURL()和request.getRequestURI()

本文详细解析了如何使用request对象获取关键HTTP请求属性,如请求路径、URL、上下文路径、服务路径,并解释了这些属性在Web开发中的实际应用。
request.getRequestURI() /ABCWeb/resources/request.jsp
request.getRequestURL() http://localhost:8080/ABCWeb/resources/request.jsp
request.getContextPath()/ABCWeb
request.getServletPath()/resources/request.jsp

注: resources为WebContext下的目录名
      ABCWeb 为工程名
package sfw.xmut.guomao.aspect; import sfw.xmut.guomao.entity.OperationLog; import sfw.xmut.guomao.mapper.OperationLogMapper; import sfw.xmut.guomao.utils.ServletUtil; import jakarta.servlet.http.HttpServletRequest; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import java.time.LocalDateTime; @Aspect @Component public class OperationLogAspect { private final OperationLogMapper logMapper; public OperationLogAspect(OperationLogMapper logMapper) { this.logMapper = logMapper; } @Around("execution(* sfw.xmut.guomao.controller.*.*(..))") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes(); HttpServletRequest request = attributes.getRequest(); if (request.getRequestURI().endsWith("/operationLog/page")) { return joinPoint.proceed(); } long startTime = System.currentTimeMillis(); Object result = joinPoint.proceed(); long spendTime = System.currentTimeMillis() - startTime; OperationLog log = new OperationLog(); log.setUserName("admin"); // TODO: Get from JWT token log.setOperationType(request.getRequestURI()); log.setOperationMethod(request.getMethod()); log.setRequestUrl(request.getRequestURL().toString()); log.setOperationIp(ServletUtil.getClientIP(request)); log.setOperationStatus("成功"); log.setLastOperationTime(LocalDateTime.now()); logMapper.insert(log); return result; } }log.setUserName("admin"); // TODO: Get from JWT token 这一行代码怎么把他改成不管哪个用户登录这个管理系统日志上面都可以更新,不要像这个admin锁住固定住不管谁登录都是显示他的名称
06-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值