|
|
@@ -0,0 +1,195 @@
|
|
|
+/*
|
|
|
+ *
|
|
|
+ * Copyright (c) 2018-2025, lengleng All rights reserved.
|
|
|
+ *
|
|
|
+ * Redistribution and use in source and binary forms, with or without
|
|
|
+ * modification, are permitted provided that the following conditions are met:
|
|
|
+ *
|
|
|
+ * Redistributions of source code must retain the above copyright notice,
|
|
|
+ * this list of conditions and the following disclaimer.
|
|
|
+ * Redistributions in binary form must reproduce the above copyright
|
|
|
+ * notice, this list of conditions and the following disclaimer in the
|
|
|
+ * documentation and/or other materials provided with the distribution.
|
|
|
+ * Neither the name of the pig4cloud.com developer nor the names of its
|
|
|
+ * contributors may be used to endorse or promote products derived from
|
|
|
+ * this software without specific prior written permission.
|
|
|
+ * Author: lengleng (wangiegie@gmail.com)
|
|
|
+ *
|
|
|
+ */
|
|
|
+
|
|
|
+package com.pig4cloud.pigx.common.core.util;
|
|
|
+
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
+import org.springframework.util.Assert;
|
|
|
+import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
+import org.springframework.web.method.HandlerMethod;
|
|
|
+
|
|
|
+import javax.servlet.http.Cookie;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.PrintWriter;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * Miscellaneous utilities for web applications.
|
|
|
+ *
|
|
|
+ * @author L.cm
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+public class WebUtils extends org.springframework.web.util.WebUtils {
|
|
|
+
|
|
|
+ public static final String UNKNOWN = "unknown";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断是否ajax请求
|
|
|
+ * spring ajax 返回含有 ResponseBody 或者 RestController注解
|
|
|
+ *
|
|
|
+ * @param handlerMethod HandlerMethod
|
|
|
+ * @return 是否ajax请求
|
|
|
+ */
|
|
|
+ public static boolean isBody(HandlerMethod handlerMethod) {
|
|
|
+ ResponseBody responseBody = ClassUtils.getAnnotation(handlerMethod, ResponseBody.class);
|
|
|
+ return responseBody != null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 读取cookie
|
|
|
+ *
|
|
|
+ * @param name cookie name
|
|
|
+ * @return cookie value
|
|
|
+ */
|
|
|
+ public static String getCookieVal(String name) {
|
|
|
+ HttpServletRequest request = WebUtils.getRequest();
|
|
|
+ Assert.notNull(request, "request from RequestContextHolder is null");
|
|
|
+ return getCookieVal(request, name);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 读取cookie
|
|
|
+ *
|
|
|
+ * @param request HttpServletRequest
|
|
|
+ * @param name cookie name
|
|
|
+ * @return cookie value
|
|
|
+ */
|
|
|
+ public static String getCookieVal(HttpServletRequest request, String name) {
|
|
|
+ Cookie cookie = getCookie(request, name);
|
|
|
+ return cookie != null ? cookie.getValue() : null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 清除 某个指定的cookie
|
|
|
+ *
|
|
|
+ * @param response HttpServletResponse
|
|
|
+ * @param key cookie key
|
|
|
+ */
|
|
|
+ public static void removeCookie(HttpServletResponse response, String key) {
|
|
|
+ setCookie(response, key, null, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置cookie
|
|
|
+ *
|
|
|
+ * @param response HttpServletResponse
|
|
|
+ * @param name cookie name
|
|
|
+ * @param value cookie value
|
|
|
+ * @param maxAgeInSeconds maxage
|
|
|
+ */
|
|
|
+ public static void setCookie(HttpServletResponse response, String name, String value, int maxAgeInSeconds) {
|
|
|
+ Cookie cookie = new Cookie(name, value);
|
|
|
+ cookie.setPath("/");
|
|
|
+ cookie.setMaxAge(maxAgeInSeconds);
|
|
|
+ cookie.setHttpOnly(true);
|
|
|
+ response.addCookie(cookie);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取 HttpServletRequest
|
|
|
+ *
|
|
|
+ * @return {HttpServletRequest}
|
|
|
+ */
|
|
|
+ public static HttpServletRequest getRequest() {
|
|
|
+ return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取 HttpServletResponse
|
|
|
+ *
|
|
|
+ * @return {HttpServletResponse}
|
|
|
+ */
|
|
|
+ public static HttpServletResponse getResponse() {
|
|
|
+ return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回json
|
|
|
+ *
|
|
|
+ * @param response HttpServletResponse
|
|
|
+ * @param result 结果对象
|
|
|
+ */
|
|
|
+ public static void renderJson(HttpServletResponse response, Object result) {
|
|
|
+ renderJson(response, result, MediaType.APPLICATION_JSON_UTF8_VALUE);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回json
|
|
|
+ *
|
|
|
+ * @param response HttpServletResponse
|
|
|
+ * @param result 结果对象
|
|
|
+ * @param contentType contentType
|
|
|
+ */
|
|
|
+ public static void renderJson(HttpServletResponse response, Object result, String contentType) {
|
|
|
+ response.setCharacterEncoding("UTF-8");
|
|
|
+ response.setContentType(contentType);
|
|
|
+ try (PrintWriter out = response.getWriter()) {
|
|
|
+ out.append(JSONUtil.toJsonStr(result));
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取ip
|
|
|
+ *
|
|
|
+ * @return {String}
|
|
|
+ */
|
|
|
+ public static String getIP() {
|
|
|
+ return getIP(WebUtils.getRequest());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取ip
|
|
|
+ *
|
|
|
+ * @param request HttpServletRequest
|
|
|
+ * @return {String}
|
|
|
+ */
|
|
|
+ public static String getIP(HttpServletRequest request) {
|
|
|
+ Assert.notNull(request, "HttpServletRequest is null");
|
|
|
+ String ip = request.getHeader("X-Requested-For");
|
|
|
+ if (StringUtils.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
|
|
|
+ ip = request.getHeader("X-Forwarded-For");
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
|
|
|
+ ip = request.getHeader("Proxy-Client-IP");
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
|
|
|
+ ip = request.getHeader("WL-Proxy-Client-IP");
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
|
|
|
+ ip = request.getHeader("HTTP_CLIENT_IP");
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
|
|
|
+ ip = request.getHeader("HTTP_X_FORWARDED_FOR");
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
|
|
|
+ ip = request.getRemoteAddr();
|
|
|
+ }
|
|
|
+ return StringUtils.isBlank(ip) ? null : ip.split(",")[0];
|
|
|
+ }
|
|
|
+}
|
|
|
+
|