From f1208474f771a1c233d7425c8ed13fbaa0d521ac Mon Sep 17 00:00:00 2001 From: baoshiwei <baoshiwei@shlanbao.cn> Date: 星期三, 12 三月 2025 09:35:13 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/5.X' into 5.X --- ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/utils/ServletUtils.java | 99 ++++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 80 insertions(+), 19 deletions(-) diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/utils/ServletUtils.java b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/utils/ServletUtils.java index a1316eb..bd1aab8 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/utils/ServletUtils.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/utils/ServletUtils.java @@ -25,7 +25,7 @@ import java.util.Map; /** - * 瀹㈡埛绔伐鍏风被 + * 瀹㈡埛绔伐鍏风被锛屾彁渚涜幏鍙栬姹傚弬鏁般�佸搷搴斿鐞嗐�佸ご閮ㄤ俊鎭瓑甯哥敤鎿嶄綔 * * @author ruoyi */ @@ -33,52 +33,73 @@ public class ServletUtils extends JakartaServletUtil { /** - * 鑾峰彇String鍙傛暟 + * 鑾峰彇鎸囧畾鍚嶇О鐨� String 绫诲瀷鐨勮姹傚弬鏁� + * + * @param name 鍙傛暟鍚� + * @return 鍙傛暟鍊� */ public static String getParameter(String name) { return getRequest().getParameter(name); } /** - * 鑾峰彇String鍙傛暟 + * 鑾峰彇鎸囧畾鍚嶇О鐨� String 绫诲瀷鐨勮姹傚弬鏁帮紝鑻ュ弬鏁颁笉瀛樺湪锛屽垯杩斿洖榛樿鍊� + * + * @param name 鍙傛暟鍚� + * @param defaultValue 榛樿鍊� + * @return 鍙傛暟鍊兼垨榛樿鍊� */ public static String getParameter(String name, String defaultValue) { return Convert.toStr(getRequest().getParameter(name), defaultValue); } /** - * 鑾峰彇Integer鍙傛暟 + * 鑾峰彇鎸囧畾鍚嶇О鐨� Integer 绫诲瀷鐨勮姹傚弬鏁� + * + * @param name 鍙傛暟鍚� + * @return 鍙傛暟鍊� */ public static Integer getParameterToInt(String name) { return Convert.toInt(getRequest().getParameter(name)); } /** - * 鑾峰彇Integer鍙傛暟 + * 鑾峰彇鎸囧畾鍚嶇О鐨� Integer 绫诲瀷鐨勮姹傚弬鏁帮紝鑻ュ弬鏁颁笉瀛樺湪锛屽垯杩斿洖榛樿鍊� + * + * @param name 鍙傛暟鍚� + * @param defaultValue 榛樿鍊� + * @return 鍙傛暟鍊兼垨榛樿鍊� */ public static Integer getParameterToInt(String name, Integer defaultValue) { return Convert.toInt(getRequest().getParameter(name), defaultValue); } /** - * 鑾峰彇Boolean鍙傛暟 + * 鑾峰彇鎸囧畾鍚嶇О鐨� Boolean 绫诲瀷鐨勮姹傚弬鏁� + * + * @param name 鍙傛暟鍚� + * @return 鍙傛暟鍊� */ public static Boolean getParameterToBool(String name) { return Convert.toBool(getRequest().getParameter(name)); } /** - * 鑾峰彇Boolean鍙傛暟 + * 鑾峰彇鎸囧畾鍚嶇О鐨� Boolean 绫诲瀷鐨勮姹傚弬鏁帮紝鑻ュ弬鏁颁笉瀛樺湪锛屽垯杩斿洖榛樿鍊� + * + * @param name 鍙傛暟鍚� + * @param defaultValue 榛樿鍊� + * @return 鍙傛暟鍊兼垨榛樿鍊� */ public static Boolean getParameterToBool(String name, Boolean defaultValue) { return Convert.toBool(getRequest().getParameter(name), defaultValue); } /** - * 鑾峰緱鎵�鏈夎姹傚弬鏁� + * 鑾峰彇鎵�鏈夎姹傚弬鏁帮紙浠� Map 鐨勫舰寮忚繑鍥烇級 * * @param request 璇锋眰瀵硅薄{@link ServletRequest} - * @return Map + * @return 璇锋眰鍙傛暟鐨� Map锛岄敭涓哄弬鏁板悕锛屽�间负鍙傛暟鍊兼暟缁� */ public static Map<String, String[]> getParams(ServletRequest request) { final Map<String, String[]> map = request.getParameterMap(); @@ -86,10 +107,10 @@ } /** - * 鑾峰緱鎵�鏈夎姹傚弬鏁� + * 鑾峰彇鎵�鏈夎姹傚弬鏁帮紙浠� Map 鐨勫舰寮忚繑鍥烇紝鍊间负瀛楃涓插舰寮忕殑鎷兼帴锛� * * @param request 璇锋眰瀵硅薄{@link ServletRequest} - * @return Map + * @return 璇锋眰鍙傛暟鐨� Map锛岄敭涓哄弬鏁板悕锛屽�间负鎷兼帴鍚庣殑瀛楃涓� */ public static Map<String, String> getParamMap(ServletRequest request) { Map<String, String> params = new HashMap<>(); @@ -100,7 +121,9 @@ } /** - * 鑾峰彇request + * 鑾峰彇褰撳墠 HTTP 璇锋眰瀵硅薄 + * + * @return 褰撳墠 HTTP 璇锋眰瀵硅薄 */ public static HttpServletRequest getRequest() { try { @@ -111,7 +134,9 @@ } /** - * 鑾峰彇response + * 鑾峰彇褰撳墠 HTTP 鍝嶅簲瀵硅薄 + * + * @return 褰撳墠 HTTP 鍝嶅簲瀵硅薄 */ public static HttpServletResponse getResponse() { try { @@ -122,12 +147,25 @@ } /** - * 鑾峰彇session + * 鑾峰彇褰撳墠璇锋眰鐨� HttpSession 瀵硅薄 + * <p> + * 濡傛灉褰撳墠璇锋眰宸茬粡鍏宠仈浜嗕竴涓細璇濓紙鍗冲凡缁忓瓨鍦ㄦ湁鏁堢殑 session ID锛夛紝 + * 鍒欒繑鍥炶浼氳瘽瀵硅薄锛涘鏋滄病鏈夊叧鑱斾細璇濓紝鍒欎細鍒涘缓涓�涓柊鐨勪細璇濆璞″苟杩斿洖銆� + * <p> + * HttpSession 鐢ㄤ簬瀛樺偍浼氳瘽绾у埆鐨勬暟鎹紝濡傜敤鎴风櫥褰曚俊鎭�佽喘鐗╄溅鍐呭绛夛紝 + * 鍙互鍦ㄥ涓姹備箣闂村叡浜細璇濇暟鎹� + * + * @return 褰撳墠璇锋眰鐨� HttpSession 瀵硅薄 */ public static HttpSession getSession() { return getRequest().getSession(); } + /** + * 鑾峰彇褰撳墠璇锋眰鐨勮姹傚睘鎬� + * + * @return {@link ServletRequestAttributes} 璇锋眰灞炴�у璞� + */ public static ServletRequestAttributes getRequestAttributes() { try { RequestAttributes attributes = RequestContextHolder.getRequestAttributes(); @@ -137,6 +175,13 @@ } } + /** + * 鑾峰彇鎸囧畾璇锋眰澶寸殑鍊硷紝濡傛灉澶撮儴涓虹┖鍒欒繑鍥炵┖瀛楃涓� + * + * @param request 璇锋眰瀵硅薄 + * @param name 澶撮儴鍚嶇О + * @return 澶撮儴鍊� + */ public static String getHeader(HttpServletRequest request, String name) { String value = request.getHeader(name); if (StringUtils.isEmpty(value)) { @@ -145,6 +190,12 @@ return urlDecode(value); } + /** + * 鑾峰彇鎵�鏈夎姹傚ご鐨� Map锛岄敭涓哄ご閮ㄥ悕绉帮紝鍊间负澶撮儴鍊� + * + * @param request 璇锋眰瀵硅薄 + * @return 璇锋眰澶寸殑 Map + */ public static Map<String, String> getHeaders(HttpServletRequest request) { Map<String, String> map = new LinkedCaseInsensitiveMap<>(); Enumeration<String> enumeration = request.getHeaderNames(); @@ -159,7 +210,7 @@ } /** - * 灏嗗瓧绗︿覆娓叉煋鍒板鎴风 + * 灏嗗瓧绗︿覆娓叉煋鍒板鎴风锛堜互 JSON 鏍煎紡杩斿洖锛� * * @param response 娓叉煋瀵硅薄 * @param string 寰呮覆鏌撶殑瀛楃涓� @@ -176,37 +227,47 @@ } /** - * 鏄惁鏄疉jax寮傛璇锋眰 + * 鍒ゆ柇褰撳墠璇锋眰鏄惁涓� Ajax 寮傛璇锋眰 * - * @param request + * @param request 璇锋眰瀵硅薄 + * @return 鏄惁涓� Ajax 璇锋眰 */ public static boolean isAjaxRequest(HttpServletRequest request) { + // 鍒ゆ柇 Accept 澶撮儴鏄惁鍖呭惈 application/json String accept = request.getHeader("accept"); if (accept != null && accept.contains(MediaType.APPLICATION_JSON_VALUE)) { return true; } + // 鍒ゆ柇 X-Requested-With 澶撮儴鏄惁鍖呭惈 XMLHttpRequest String xRequestedWith = request.getHeader("X-Requested-With"); if (xRequestedWith != null && xRequestedWith.contains("XMLHttpRequest")) { return true; } + // 鍒ゆ柇 URI 鍚庣紑鏄惁涓� .json 鎴� .xml String uri = request.getRequestURI(); if (StringUtils.equalsAnyIgnoreCase(uri, ".json", ".xml")) { return true; } + // 鍒ゆ柇璇锋眰鍙傛暟 __ajax 鏄惁涓� json 鎴� xml String ajax = request.getParameter("__ajax"); return StringUtils.equalsAnyIgnoreCase(ajax, "json", "xml"); } + /** + * 鑾峰彇瀹㈡埛绔� IP 鍦板潃 + * + * @return 瀹㈡埛绔� IP 鍦板潃 + */ public static String getClientIP() { return getClientIP(getRequest()); } /** - * 鍐呭缂栫爜 + * 瀵瑰唴瀹硅繘琛� URL 缂栫爜 * * @param str 鍐呭 * @return 缂栫爜鍚庣殑鍐呭 @@ -216,7 +277,7 @@ } /** - * 鍐呭瑙g爜 + * 瀵瑰唴瀹硅繘琛� URL 瑙g爜 * * @param str 鍐呭 * @return 瑙g爜鍚庣殑鍐呭 -- Gitblit v1.9.3