!20 🐛 修复在JDK 17的环境下无sun相关的包

Merge pull request !20 from 徐晓伟/master-sun
This commit is contained in:
大粽子
2023-06-15 03:21:56 +00:00
committed by Gitee
3 changed files with 30 additions and 39 deletions

View File

@@ -1,12 +1,12 @@
package com.zbkj.common.interceptor;
import cn.hutool.core.codec.Base64;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import sun.misc.BASE64Decoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -52,7 +52,7 @@ public class SwaggerInterceptor extends HandlerInterceptorAdapter {
public boolean httpBasicAuth(String authorization) throws IOException {
if(check){
if (authorization != null && authorization.split(" ").length == 2) {
String userAndPass = new String(new BASE64Decoder().decodeBuffer(authorization.split(" ")[1]));
String userAndPass = Base64.decodeStr(authorization.split(" ")[1]);
String username = userAndPass.split(":").length == 2 ? userAndPass.split(":")[0] : null;
String password = userAndPass.split(":").length == 2 ? userAndPass.split(":")[1] : null;
return this.username.equals(username) && this.password.equals(password);

View File

@@ -1,7 +1,6 @@
package com.zbkj.common.request;
import com.baomidou.mybatisplus.annotation.TableName;
import com.sun.org.apache.xpath.internal.operations.Bool;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@@ -9,11 +8,8 @@ import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.hibernate.validator.constraints.Range;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.List;
/**
* 商品表

View File

@@ -1,23 +1,19 @@
package com.zbkj.common.utils;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.DES;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zbkj.common.constants.Constants;
import com.zbkj.common.exception.CrmebException;
import org.apache.commons.lang3.StringUtils;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.security.Security;
import java.text.NumberFormat;
import java.util.*;
import java.util.regex.Matcher;
@@ -37,43 +33,26 @@ import java.util.regex.Pattern;
*/
public class CrmebUtil {
public static String encryptPassword(String pwd, String key){
try {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
Key _key = getDESSercretKey(key);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, _key);
byte[] data = pwd.getBytes(StandardCharsets.UTF_8);
byte[] result = cipher.doFinal(data);
return new sun.misc.BASE64Encoder().encode(result);
}catch (Exception e){
throw new CrmebException("密码处理异常");
}
public static String encryptPassword(String pwd, String key) {
DES des = new DES(getDESSercretKey(key));
byte[] result = des.encrypt(pwd);
return Base64.encode(result);
}
/**
* 解密密码
*/
public static String decryptPassowrd(String pwd, String key)
throws Exception {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
Key aKey = getDESSercretKey(key);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, aKey);
byte[] data = new sun.misc.BASE64Decoder().decodeBuffer(pwd);
byte[] result = cipher.doFinal(data);
return new String(result, StandardCharsets.UTF_8);
public static String decryptPassowrd(String pwd, String key) {
DES des = new DES(getDESSercretKey(key));
return des.decryptStr(pwd);
}
/**
* 获得DES加密秘钥
* @param key
* @return
* @throws UnsupportedEncodingException
*/
public static SecretKey getDESSercretKey(String key) throws UnsupportedEncodingException {
public static byte[] getDESSercretKey(String key) {
byte[] result = new byte[8];
byte[] keys = null;
keys = key.getBytes(StandardCharsets.UTF_8);
@@ -84,7 +63,7 @@ public class CrmebUtil {
result[i] = 0x01;
}
}
return new SecretKeySpec(result, "DES");
return result;
}
/**
@@ -134,8 +113,24 @@ public class CrmebUtil {
* @param args String[] 字符串数组
*/
public static void main(String[] args) throws Exception {
// System.out.println(encryptPassword("123456", "admin"));
// System.out.println(decryptPassowrd("", ""));
String key = "123456";
String data = "中国123ABCabc";
System.out.println("原始数据:" + data);
String encryptPassword = encryptPassword(data, key);
System.out.println("加密结果:" + encryptPassword);
String decryptPassowrd = decryptPassowrd(encryptPassword, key);
System.out.println("解密结果:" + decryptPassowrd);
// 执行结果如下:
// 原始数据中国123ABCabc
// 加密结果5JNGj04iE/XUuTZM75zMrA==
// 解密结果中国123ABCabc
System.out.println(encryptPassword("crmeb@123456", "18292417675"));
// System.out.println(decryptPassowrd("Ffg0LtOQNyWU1//Y16+72A==", "18292417675"));
// 执行结果f6mcpGQ8NEmwbab2TlkpUg==
// 与 SQL 中的数据一致
}
/**