1. springboot版本升级3.x

2. mybatis plus版本升级3.x
3. dynamic mybatis plus版本升级3.x
4. redis版本升级3.x
5. reids实现配置优化
6. 替换swagger依赖(支持3.x)
7. 新增请求头工具类
8. 参数校验异常捕获优化
9. 拦截器注册为spring容器管理
10. 新增本地日志配置文件
This commit is contained in:
xiongf
2024-10-12 17:32:56 +08:00
parent cf4c78766b
commit 247e6f456a
16 changed files with 361 additions and 174 deletions

View File

@@ -1,14 +1,21 @@
package cn.xf.basedemo.common.exception;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
/**
* @Author: xiongfeng
@@ -58,4 +65,22 @@ public class GlobalExceptionHandler extends ResponseEntityExceptionHandler{
return new GenericResponse(exception.getCode(),null,exception.getMessage());
}
/**
* 重写handleMethodArgumentNotValid 方法自定义处理参数校验错误信息
*
* @param ex
* @param headers
* @param status
* @param request
* @return
*/
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
Map<String, String> errors = new HashMap<>();
ex.getBindingResult().getFieldErrors().forEach(error -> {
errors.put(error.getField(), error.getDefaultMessage());
});
return new ResponseEntity<>(errors, HttpStatus.BAD_REQUEST);
}
}

View File

@@ -2,6 +2,7 @@ package cn.xf.basedemo.common.utils;
import org.apache.tomcat.util.codec.binary.Base64;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.security.*;
@@ -34,14 +35,15 @@ public class RSAUtils {
/**
* 创建rsa密匙对
*
* @return
*/
private static Map<String, String> createEncryptKey(){
private static Map<String, String> createEncryptKey() {
KeyPairGenerator kpg;
try {
kpg = KeyPairGenerator.getInstance(RSA);
}catch (Exception e){
} catch (Exception e) {
throw new IllegalArgumentException();
}
kpg.initialize(ENCRYPT_SIZE);
@@ -52,7 +54,7 @@ public class RSAUtils {
PrivateKey aPrivate = keyPair.getPrivate();
String privateKey = Base64.encodeBase64URLSafeString(aPrivate.getEncoded());
Map<String, String> map =new HashMap<>();
Map<String, String> map = new HashMap<>();
map.put("publicKey", publicKey);
map.put("privateKey", privateKey);
@@ -62,6 +64,7 @@ public class RSAUtils {
/**
* 获取ras公匙
*
* @param publicKeyStr 公匙加密字符串
* @return
* @throws NoSuchAlgorithmException
@@ -77,6 +80,7 @@ public class RSAUtils {
/**
* 获取ras私匙
*
* @param privateKeyStr 私匙加密字符串
* @return
* @throws NoSuchAlgorithmException
@@ -91,12 +95,13 @@ public class RSAUtils {
}
/**
* 公匙加密
* @param data 字符串
* 公匙加密
*
* @param data 字符串
* @param publicKey
* @return
*/
public static String publicEncrypt(String data, RSAPublicKey publicKey){
public static String publicEncrypt(String data, RSAPublicKey publicKey) {
try {
@@ -106,19 +111,19 @@ public class RSAUtils {
byte[] bytes = cipher.doFinal(data.getBytes());
return Base64.encodeBase64URLSafeString(bytes);
// return Base64.encodeBase64URLSafeString(rsaSplitCodec(cipher, Cipher.ENCRYPT_MODE, data.getBytes(CHARSET), publicKey.getModulus().bitLength()));
}catch (Exception e){
} catch (Exception e) {
throw new RuntimeException();
}
}
public static String privateDecryption(String data, RSAPrivateKey privateKey){
public static String privateDecryption(String data, RSAPrivateKey privateKey) {
try {
Cipher cipher = Cipher.getInstance(RSA);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
// byte[] bytes = cipher.doFinal(Base64.decodeBase64(data.getBytes(CHARSET)));
// return new String(bytes);
return new String(rsaSplitCodec(cipher, Cipher.DECRYPT_MODE, Base64.decodeBase64(data.getBytes(CHARSET)), privateKey.getModulus().bitLength()), CHARSET);
return new String(rsaSplitCodec(cipher, Cipher.DECRYPT_MODE, Base64.decodeBase64(data.getBytes(CHARSET), 0, data.getBytes(CHARSET).length), privateKey.getModulus().bitLength()), CHARSET);
} catch (Exception e) {
throw new RuntimeException("解密字符串[" + data + "]时遇到异常", e);
}
@@ -178,7 +183,7 @@ public class RSAUtils {
// String s1 = privateDecryption(ss, getPrivateKey(privateKey1));
// System.out.println("解密后明文:" + s1);
}catch (Exception e){
} catch (Exception e) {
throw new IllegalArgumentException();
}
@@ -186,5 +191,4 @@ public class RSAUtils {
}
}

View File

@@ -0,0 +1,67 @@
package cn.xf.basedemo.common.utils;
import cn.xf.basedemo.common.exception.LoginException;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.util.StringUtils;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
/**
* packageName com.wenliao.customer.common.utils
*
* @author remaindertime
* @className RequestHeaderUtil
* @date 2024/10/10
* @description
*/
public class RequestHeaderUtil {
/**
* 获取所有请求头
* @param request HttpServletRequest 对象
* @return 请求头的键值对 Map
*/
public static Map<String, String> getHeaders(HttpServletRequest request) {
Map<String, String> headersMap = new HashMap<>();
Enumeration<String> headerNames = request.getHeaderNames();
if (headerNames != null) {
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
String headerValue = request.getHeader(headerName);
headersMap.put(headerName, headerValue);
}
}
return headersMap;
}
/**
* 获取特定的请求头
* @param request HttpServletRequest 对象
* @param headerName 请求头名称
* @return 请求头值,如果没有则返回 null
*/
public static String getHeader(HttpServletRequest request, String headerName) {
return request.getHeader(headerName);
}
/**
* 获取token
*/
public static String getToken(HttpServletRequest request) {
//登录处理
String token = request.getHeader("Authorization");
if (StringUtils.isEmpty(token))
token = request.getParameter("token");
if (StringUtils.isEmpty(token)) {
throw new LoginException("请先登录");
}
// 如果 Authorization 头部中存在且以 "Bearer " 开头,则提取 token
if (token.startsWith("Bearer ")) {
token = token.substring(7); // 去除 "Bearer " 部分,获取真正的 token
}
return token;
}
}