|
@@ -1,10 +1,15 @@
|
|
|
package com.fdkankan.web.util;
|
|
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.http.ContentType;
|
|
|
+import cn.hutool.http.useragent.UserAgent;
|
|
|
+import cn.hutool.http.useragent.UserAgentUtil;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import java.util.HashMap;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import org.aspectj.lang.JoinPoint;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
+import org.springframework.http.server.reactive.ServerHttpRequest;
|
|
|
|
|
|
/**
|
|
|
* <p>
|
|
@@ -16,20 +21,67 @@ import org.aspectj.lang.JoinPoint;
|
|
|
**/
|
|
|
public class WebUtil {
|
|
|
|
|
|
+ private final static String UNKNOWN = "unknown";
|
|
|
+
|
|
|
/**
|
|
|
- * 获取请求参数中的场景码
|
|
|
+ * aop中获取请求参数中的参数值
|
|
|
* @param pjp
|
|
|
* @param request
|
|
|
* @return
|
|
|
*/
|
|
|
- public static String getNum(JoinPoint pjp, HttpServletRequest request){
|
|
|
+ public static String getParameter(String paramName, JoinPoint pjp, HttpServletRequest request){
|
|
|
Object[] args = pjp.getArgs();
|
|
|
String contentType = request.getContentType();
|
|
|
if(contentType.contains(ContentType.JSON.getValue())){
|
|
|
HashMap hashMap = JSON.parseObject(JSON.toJSONString(args[0]), HashMap.class);
|
|
|
- return (String) hashMap.get("num");
|
|
|
+ return (String) hashMap.get(paramName);
|
|
|
+ }
|
|
|
+ return request.getParameter(paramName);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取客户端请求终端地址
|
|
|
+ * @param request 客户端请求request
|
|
|
+ * @return 终端ip地址
|
|
|
+ */
|
|
|
+ public static String getIpAddress(HttpServletRequest request) {
|
|
|
+ String ip = request.getHeader("X-Forwarded-For");
|
|
|
+ if(StrUtil.isNotBlank(ip) && UNKNOWN.equalsIgnoreCase(ip)){
|
|
|
+ if(ip.indexOf(",") != -1){
|
|
|
+ ip = ip.split(",")[0];
|
|
|
+ return ip;
|
|
|
+ }
|
|
|
}
|
|
|
- return request.getParameter("num");
|
|
|
+ ip = request.getHeader("Proxy-Client-IP");
|
|
|
+ if(StrUtil.isNotBlank(ip)) return ip;
|
|
|
+ ip = request.getHeader("WL-Proxy-Client-IP");
|
|
|
+ if(StrUtil.isNotBlank(ip)) return ip;
|
|
|
+ ip = request.getHeader("HTTP-CLIENT-IP");
|
|
|
+ if(StrUtil.isNotBlank(ip)) return ip;
|
|
|
+ ip = request.getHeader("HTTP-X-FORWARDED-FOR");
|
|
|
+ if(StrUtil.isNotBlank(ip)) return ip;
|
|
|
+ ip = request.getHeader("X-Real-IP");
|
|
|
+ if(StrUtil.isNotBlank(ip)) return ip;
|
|
|
+ ip = request.getRemoteAddr();
|
|
|
+ return ip;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * <p>
|
|
|
+ 获取浏览器版本
|
|
|
+ * </p>
|
|
|
+ * @author dengsixing
|
|
|
+ * @date 2022/8/19
|
|
|
+ * @param request
|
|
|
+ * @return java.lang.String
|
|
|
+ **/
|
|
|
+ public static String getBrowser(HttpServletRequest request){
|
|
|
+ String userAgentStr = request.getHeader("User-Agent");
|
|
|
+ UserAgent userAgent = UserAgentUtil.parse(userAgentStr);
|
|
|
+ String browserType = userAgent.getBrowser().toString();
|
|
|
+ String browserVersion = userAgent.getVersion();
|
|
|
+ String browserFormat = "%s(版本%s)";
|
|
|
+ return String.format(browserFormat, browserType, browserVersion);
|
|
|
}
|
|
|
|
|
|
}
|