|
@@ -0,0 +1,67 @@
|
|
|
+package com.fdkankan.scene.config;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.aspectj.lang.JoinPoint;
|
|
|
+import org.aspectj.lang.annotation.Aspect;
|
|
|
+import org.aspectj.lang.annotation.Before;
|
|
|
+import org.aspectj.lang.annotation.Pointcut;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
+
|
|
|
+@Component
|
|
|
+@Aspect
|
|
|
+@Slf4j
|
|
|
+public class V4ControllerAspect {
|
|
|
+ // 切入点表达式
|
|
|
+ @Pointcut("execution(public * com.fdkankan.scene.controller.TestController.*(..))")
|
|
|
+ public void privilege() {
|
|
|
+ }
|
|
|
+
|
|
|
+ @Before("privilege()")
|
|
|
+ public void beforeHandler(JoinPoint pjp) throws Throwable {
|
|
|
+ // 定义返回参数
|
|
|
+ Object result = null;
|
|
|
+ // 获取方法参数
|
|
|
+ HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
|
|
+ // 请求的URL
|
|
|
+ String requestURL = request.getRequestURL().toString();
|
|
|
+ String ip = getIpAddr(request);
|
|
|
+ System.out.println(requestURL);
|
|
|
+ System.out.println(ip);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Title: getIpAddr
|
|
|
+ * @Description: 获取ip
|
|
|
+ * @param request
|
|
|
+ * @return
|
|
|
+ * @return String 返回类型
|
|
|
+ */
|
|
|
+ public String getIpAddr(HttpServletRequest request) {
|
|
|
+ String ipAddress = null;
|
|
|
+ ipAddress = request.getHeader("x-forwarded-for");
|
|
|
+ if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
|
|
|
+ ipAddress = request.getHeader("Proxy-Client-IP");
|
|
|
+ }
|
|
|
+ if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
|
|
|
+ ipAddress = request.getHeader("WL-Proxy-Client-IP");
|
|
|
+ }
|
|
|
+ if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
|
|
|
+ ipAddress = request.getRemoteAddr();
|
|
|
+ }
|
|
|
+
|
|
|
+ //对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
|
|
|
+ if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
|
|
|
+ // = 15
|
|
|
+ if (ipAddress.indexOf(",") > 0) {
|
|
|
+ ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 或者这样也行,对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
|
|
|
+ //return ipAddress!=null&&!"".equals(ipAddress)?ipAddress.split(",")[0]:null;
|
|
|
+ return ipAddress;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|