Commit e5fcc7a387f7bc16fc719a56dec880ca3d943211

Authored by YunaiV
1 parent d23ed5a6

boot3:适合 ServletUtils

yudao-example/yudao-sso-demo-by-code/src/main/java/cn/iocoder/yudao/ssodemo/framework/core/util/ServletUtils.java
@@ -25,4 +25,8 @@ public class ServletUtils { @@ -25,4 +25,8 @@ public class ServletUtils {
25 ServletUtil.write(response, content, MediaType.APPLICATION_JSON_UTF8_VALUE); 25 ServletUtil.write(response, content, MediaType.APPLICATION_JSON_UTF8_VALUE);
26 } 26 }
27 27
  28 + public static void write(HttpServletResponse response, String text, String contentType) {
  29 + ServletUtil.write(response, text, contentType);
  30 + }
  31 +
28 } 32 }
yudao-example/yudao-sso-demo-by-password/src/main/java/cn/iocoder/yudao/ssodemo/framework/core/util/ServletUtils.java
@@ -25,4 +25,8 @@ public class ServletUtils { @@ -25,4 +25,8 @@ public class ServletUtils {
25 ServletUtil.write(response, content, MediaType.APPLICATION_JSON_UTF8_VALUE); 25 ServletUtil.write(response, content, MediaType.APPLICATION_JSON_UTF8_VALUE);
26 } 26 }
27 27
  28 + public static void write(HttpServletResponse response, String text, String contentType) {
  29 + ServletUtil.write(response, text, contentType);
  30 + }
  31 +
28 } 32 }
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/servlet/ServletUtils.java
@@ -14,6 +14,7 @@ import javax.servlet.http.HttpServletRequest; @@ -14,6 +14,7 @@ import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.http.HttpServletResponse; 14 import javax.servlet.http.HttpServletResponse;
15 import java.io.IOException; 15 import java.io.IOException;
16 import java.net.URLEncoder; 16 import java.net.URLEncoder;
  17 +import java.util.Map;
17 18
18 /** 19 /**
19 * 客户端工具类 20 * 客户端工具类
@@ -40,7 +41,6 @@ public class ServletUtils { @@ -40,7 +41,6 @@ public class ServletUtils {
40 * @param response 响应 41 * @param response 响应
41 * @param filename 文件名 42 * @param filename 文件名
42 * @param content 附件内容 43 * @param content 附件内容
43 - * @throws IOException  
44 */ 44 */
45 public static void writeAttachment(HttpServletResponse response, String filename, byte[] content) throws IOException { 45 public static void writeAttachment(HttpServletResponse response, String filename, byte[] content) throws IOException {
46 // 设置 header 和 contentType 46 // 设置 header 和 contentType
@@ -92,4 +92,19 @@ public class ServletUtils { @@ -92,4 +92,19 @@ public class ServletUtils {
92 return StrUtil.startWithIgnoreCase(request.getContentType(), MediaType.APPLICATION_JSON_VALUE); 92 return StrUtil.startWithIgnoreCase(request.getContentType(), MediaType.APPLICATION_JSON_VALUE);
93 } 93 }
94 94
  95 + public static String getBody(HttpServletRequest request) {
  96 + return ServletUtil.getBody(request);
  97 + }
  98 +
  99 + public static byte[] getBodyBytes(HttpServletRequest request) {
  100 + return ServletUtil.getBodyBytes(request);
  101 + }
  102 +
  103 + public static String getClientIP(HttpServletRequest request) {
  104 + return ServletUtil.getClientIP(request);
  105 + }
  106 +
  107 + public static Map<String, String> getParamMap(HttpServletRequest request) {
  108 + return ServletUtil.getParamMap(request);
  109 + }
95 } 110 }
yudao-framework/yudao-spring-boot-starter-biz-operatelog/src/main/java/cn/iocoder/yudao/framework/operatelog/core/aop/OperateLogAspect.java
@@ -4,7 +4,6 @@ import cn.hutool.core.date.LocalDateTimeUtil; @@ -4,7 +4,6 @@ import cn.hutool.core.date.LocalDateTimeUtil;
4 import cn.hutool.core.exceptions.ExceptionUtil; 4 import cn.hutool.core.exceptions.ExceptionUtil;
5 import cn.hutool.core.util.ArrayUtil; 5 import cn.hutool.core.util.ArrayUtil;
6 import cn.hutool.core.util.StrUtil; 6 import cn.hutool.core.util.StrUtil;
7 -import cn.hutool.extra.servlet.ServletUtil;  
8 import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; 7 import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
9 import cn.iocoder.yudao.framework.common.pojo.CommonResult; 8 import cn.iocoder.yudao.framework.common.pojo.CommonResult;
10 import cn.iocoder.yudao.framework.common.util.json.JsonUtils; 9 import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
@@ -221,7 +220,7 @@ public class OperateLogAspect { @@ -221,7 +220,7 @@ public class OperateLogAspect {
221 // 补全请求信息 220 // 补全请求信息
222 operateLogObj.setRequestMethod(request.getMethod()); 221 operateLogObj.setRequestMethod(request.getMethod());
223 operateLogObj.setRequestUrl(request.getRequestURI()); 222 operateLogObj.setRequestUrl(request.getRequestURI());
224 - operateLogObj.setUserIp(ServletUtil.getClientIP(request)); 223 + operateLogObj.setUserIp(ServletUtils.getClientIP(request));
225 operateLogObj.setUserAgent(ServletUtils.getUserAgent(request)); 224 operateLogObj.setUserAgent(ServletUtils.getUserAgent(request));
226 } 225 }
227 226
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/filter/ApiAccessLogFilter.java
@@ -3,7 +3,6 @@ package cn.iocoder.yudao.framework.apilog.core.filter; @@ -3,7 +3,6 @@ package cn.iocoder.yudao.framework.apilog.core.filter;
3 import cn.hutool.core.date.LocalDateTimeUtil; 3 import cn.hutool.core.date.LocalDateTimeUtil;
4 import cn.hutool.core.exceptions.ExceptionUtil; 4 import cn.hutool.core.exceptions.ExceptionUtil;
5 import cn.hutool.core.map.MapUtil; 5 import cn.hutool.core.map.MapUtil;
6 -import cn.hutool.extra.servlet.ServletUtil;  
7 import cn.iocoder.yudao.framework.apilog.core.service.ApiAccessLog; 6 import cn.iocoder.yudao.framework.apilog.core.service.ApiAccessLog;
8 import cn.iocoder.yudao.framework.apilog.core.service.ApiAccessLogFrameworkService; 7 import cn.iocoder.yudao.framework.apilog.core.service.ApiAccessLogFrameworkService;
9 import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants; 8 import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants;
@@ -50,8 +49,8 @@ public class ApiAccessLogFilter extends ApiRequestFilter { @@ -50,8 +49,8 @@ public class ApiAccessLogFilter extends ApiRequestFilter {
50 // 获得开始时间 49 // 获得开始时间
51 LocalDateTime beginTime = LocalDateTime.now(); 50 LocalDateTime beginTime = LocalDateTime.now();
52 // 提前获得参数,避免 XssFilter 过滤处理 51 // 提前获得参数,避免 XssFilter 过滤处理
53 - Map<String, String> queryString = ServletUtil.getParamMap(request);  
54 - String requestBody = ServletUtils.isJsonRequest(request) ? ServletUtil.getBody(request) : null; 52 + Map<String, String> queryString = ServletUtils.getParamMap(request);
  53 + String requestBody = ServletUtils.isJsonRequest(request) ? ServletUtils.getBody(request) : null;
55 54
56 try { 55 try {
57 // 继续过滤器 56 // 继续过滤器
@@ -101,7 +100,7 @@ public class ApiAccessLogFilter extends ApiRequestFilter { @@ -101,7 +100,7 @@ public class ApiAccessLogFilter extends ApiRequestFilter {
101 accessLog.setRequestParams(toJsonString(requestParams)); 100 accessLog.setRequestParams(toJsonString(requestParams));
102 accessLog.setRequestMethod(request.getMethod()); 101 accessLog.setRequestMethod(request.getMethod());
103 accessLog.setUserAgent(ServletUtils.getUserAgent(request)); 102 accessLog.setUserAgent(ServletUtils.getUserAgent(request));
104 - accessLog.setUserIp(ServletUtil.getClientIP(request)); 103 + accessLog.setUserIp(ServletUtils.getClientIP(request));
105 // 持续时间 104 // 持续时间
106 accessLog.setBeginTime(beginTime); 105 accessLog.setBeginTime(beginTime);
107 accessLog.setEndTime(LocalDateTime.now()); 106 accessLog.setEndTime(LocalDateTime.now());
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/core/filter/CacheRequestBodyWrapper.java
1 package cn.iocoder.yudao.framework.web.core.filter; 1 package cn.iocoder.yudao.framework.web.core.filter;
2 2
3 -import cn.hutool.extra.servlet.ServletUtil; 3 +import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils;
4 4
5 import javax.servlet.ReadListener; 5 import javax.servlet.ReadListener;
6 import javax.servlet.ServletInputStream; 6 import javax.servlet.ServletInputStream;
@@ -25,7 +25,7 @@ public class CacheRequestBodyWrapper extends HttpServletRequestWrapper { @@ -25,7 +25,7 @@ public class CacheRequestBodyWrapper extends HttpServletRequestWrapper {
25 25
26 public CacheRequestBodyWrapper(HttpServletRequest request) { 26 public CacheRequestBodyWrapper(HttpServletRequest request) {
27 super(request); 27 super(request);
28 - body = ServletUtil.getBodyBytes(request); 28 + body = ServletUtils.getBodyBytes(request);
29 } 29 }
30 30
31 @Override 31 @Override
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/core/handler/GlobalExceptionHandler.java
@@ -2,7 +2,6 @@ package cn.iocoder.yudao.framework.web.core.handler; @@ -2,7 +2,6 @@ package cn.iocoder.yudao.framework.web.core.handler;
2 2
3 import cn.hutool.core.exceptions.ExceptionUtil; 3 import cn.hutool.core.exceptions.ExceptionUtil;
4 import cn.hutool.core.map.MapUtil; 4 import cn.hutool.core.map.MapUtil;
5 -import cn.hutool.extra.servlet.ServletUtil;  
6 import cn.iocoder.yudao.framework.apilog.core.service.ApiErrorLog; 5 import cn.iocoder.yudao.framework.apilog.core.service.ApiErrorLog;
7 import cn.iocoder.yudao.framework.apilog.core.service.ApiErrorLogFrameworkService; 6 import cn.iocoder.yudao.framework.apilog.core.service.ApiErrorLogFrameworkService;
8 import cn.iocoder.yudao.framework.common.exception.ServiceException; 7 import cn.iocoder.yudao.framework.common.exception.ServiceException;
@@ -261,12 +260,12 @@ public class GlobalExceptionHandler { @@ -261,12 +260,12 @@ public class GlobalExceptionHandler {
261 errorLog.setApplicationName(applicationName); 260 errorLog.setApplicationName(applicationName);
262 errorLog.setRequestUrl(request.getRequestURI()); 261 errorLog.setRequestUrl(request.getRequestURI());
263 Map<String, Object> requestParams = MapUtil.<String, Object>builder() 262 Map<String, Object> requestParams = MapUtil.<String, Object>builder()
264 - .put("query", ServletUtil.getParamMap(request))  
265 - .put("body", ServletUtil.getBody(request)).build(); 263 + .put("query", ServletUtils.getParamMap(request))
  264 + .put("body", ServletUtils.getBody(request)).build();
266 errorLog.setRequestParams(JsonUtils.toJsonString(requestParams)); 265 errorLog.setRequestParams(JsonUtils.toJsonString(requestParams));
267 errorLog.setRequestMethod(request.getMethod()); 266 errorLog.setRequestMethod(request.getMethod());
268 errorLog.setUserAgent(ServletUtils.getUserAgent(request)); 267 errorLog.setUserAgent(ServletUtils.getUserAgent(request));
269 - errorLog.setUserIp(ServletUtil.getClientIP(request)); 268 + errorLog.setUserIp(ServletUtils.getClientIP(request));
270 errorLog.setExceptionTime(LocalDateTime.now()); 269 errorLog.setExceptionTime(LocalDateTime.now());
271 } 270 }
272 271
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/controller/app/order/AppTradeOrderController.java
1 package cn.iocoder.yudao.module.trade.controller.app.order; 1 package cn.iocoder.yudao.module.trade.controller.app.order;
2 2
3 -import cn.hutool.extra.servlet.ServletUtil;  
4 import cn.iocoder.yudao.framework.common.pojo.CommonResult; 3 import cn.iocoder.yudao.framework.common.pojo.CommonResult;
5 import cn.iocoder.yudao.framework.common.pojo.PageResult; 4 import cn.iocoder.yudao.framework.common.pojo.PageResult;
6 import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated; 5 import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
@@ -55,7 +54,7 @@ public class AppTradeOrderController { @@ -55,7 +54,7 @@ public class AppTradeOrderController {
55 HttpServletRequest servletRequest) { 54 HttpServletRequest servletRequest) {
56 // 获取登录用户、用户 IP 地址 55 // 获取登录用户、用户 IP 地址
57 Long loginUserId = getLoginUserId(); 56 Long loginUserId = getLoginUserId();
58 - String clientIp = ServletUtil.getClientIP(servletRequest); 57 + String clientIp = ServletUtils.getClientIP(servletRequest);
59 // 创建交易订单,预支付记录 58 // 创建交易订单,预支付记录
60 Long orderId = tradeOrderService.createOrder(loginUserId, clientIp, createReqVO); 59 Long orderId = tradeOrderService.createOrder(loginUserId, clientIp, createReqVO);
61 return success(orderId); 60 return success(orderId);
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/captcha/CaptchaController.java
1 package cn.iocoder.yudao.module.system.controller.admin.captcha; 1 package cn.iocoder.yudao.module.system.controller.admin.captcha;
2 2
3 import cn.hutool.core.util.StrUtil; 3 import cn.hutool.core.util.StrUtil;
4 -import cn.hutool.extra.servlet.ServletUtil; 4 +import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils;
5 import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog; 5 import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
6 import com.xingyuv.captcha.model.common.ResponseModel; 6 import com.xingyuv.captcha.model.common.ResponseModel;
7 import com.xingyuv.captcha.model.vo.CaptchaVO; 7 import com.xingyuv.captcha.model.vo.CaptchaVO;
@@ -50,7 +50,7 @@ public class CaptchaController { @@ -50,7 +50,7 @@ public class CaptchaController {
50 } 50 }
51 51
52 public static String getRemoteId(HttpServletRequest request) { 52 public static String getRemoteId(HttpServletRequest request) {
53 - String ip = ServletUtil.getClientIP(request); 53 + String ip = ServletUtils.getClientIP(request);
54 String ua = request.getHeader("user-agent"); 54 String ua = request.getHeader("user-agent");
55 if (StrUtil.isNotBlank(ip)) { 55 if (StrUtil.isNotBlank(ip)) {
56 return ip + ua; 56 return ip + ua;
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/sms/SmsCallbackController.java
1 package cn.iocoder.yudao.module.system.controller.admin.sms; 1 package cn.iocoder.yudao.module.system.controller.admin.sms;
2 2
3 -import cn.hutool.extra.servlet.ServletUtil;  
4 import cn.iocoder.yudao.framework.common.pojo.CommonResult; 3 import cn.iocoder.yudao.framework.common.pojo.CommonResult;
  4 +import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils;
5 import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog; 5 import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
6 import cn.iocoder.yudao.framework.sms.core.enums.SmsChannelEnum; 6 import cn.iocoder.yudao.framework.sms.core.enums.SmsChannelEnum;
7 import cn.iocoder.yudao.module.system.service.sms.SmsSendService; 7 import cn.iocoder.yudao.module.system.service.sms.SmsSendService;
8 -import io.swagger.v3.oas.annotations.tags.Tag;  
9 import io.swagger.v3.oas.annotations.Operation; 8 import io.swagger.v3.oas.annotations.Operation;
  9 +import io.swagger.v3.oas.annotations.tags.Tag;
10 import org.springframework.web.bind.annotation.PostMapping; 10 import org.springframework.web.bind.annotation.PostMapping;
11 import org.springframework.web.bind.annotation.RequestMapping; 11 import org.springframework.web.bind.annotation.RequestMapping;
12 import org.springframework.web.bind.annotation.RestController; 12 import org.springframework.web.bind.annotation.RestController;
@@ -30,7 +30,7 @@ public class SmsCallbackController { @@ -30,7 +30,7 @@ public class SmsCallbackController {
30 @Operation(summary = "阿里云短信的回调", description = "参见 https://help.aliyun.com/document_detail/120998.html 文档") 30 @Operation(summary = "阿里云短信的回调", description = "参见 https://help.aliyun.com/document_detail/120998.html 文档")
31 @OperateLog(enable = false) 31 @OperateLog(enable = false)
32 public CommonResult<Boolean> receiveAliyunSmsStatus(HttpServletRequest request) throws Throwable { 32 public CommonResult<Boolean> receiveAliyunSmsStatus(HttpServletRequest request) throws Throwable {
33 - String text = ServletUtil.getBody(request); 33 + String text = ServletUtils.getBody(request);
34 smsSendService.receiveSmsStatus(SmsChannelEnum.ALIYUN.getCode(), text); 34 smsSendService.receiveSmsStatus(SmsChannelEnum.ALIYUN.getCode(), text);
35 return success(true); 35 return success(true);
36 } 36 }
@@ -40,7 +40,7 @@ public class SmsCallbackController { @@ -40,7 +40,7 @@ public class SmsCallbackController {
40 @Operation(summary = "腾讯云短信的回调", description = "参见 https://cloud.tencent.com/document/product/382/52077 文档") 40 @Operation(summary = "腾讯云短信的回调", description = "参见 https://cloud.tencent.com/document/product/382/52077 文档")
41 @OperateLog(enable = false) 41 @OperateLog(enable = false)
42 public CommonResult<Boolean> receiveTencentSmsStatus(HttpServletRequest request) throws Throwable { 42 public CommonResult<Boolean> receiveTencentSmsStatus(HttpServletRequest request) throws Throwable {
43 - String text = ServletUtil.getBody(request); 43 + String text = ServletUtils.getBody(request);
44 smsSendService.receiveSmsStatus(SmsChannelEnum.TENCENT.getCode(), text); 44 smsSendService.receiveSmsStatus(SmsChannelEnum.TENCENT.getCode(), text);
45 return success(true); 45 return success(true);
46 } 46 }