数据加密和完整性校验
This commit is contained in:
parent
ada6ccb7a7
commit
7a11a976e2
|
|
@ -1,21 +1,9 @@
|
|||
package com.bonus.common.core.utils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLDecoder;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
import com.alibaba.nacos.shaded.com.google.common.collect.Maps;
|
||||
import com.bonus.common.core.utils.encryption.AesCbcUtils;
|
||||
import com.bonus.common.core.utils.global.SystemGlobal;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.bonus.common.core.constant.Constants;
|
||||
import com.bonus.common.core.domain.R;
|
||||
import com.bonus.common.core.text.Convert;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
|
@ -25,12 +13,21 @@ import org.springframework.util.LinkedCaseInsensitiveMap;
|
|||
import org.springframework.web.context.request.RequestAttributes;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.bonus.common.core.constant.Constants;
|
||||
import com.bonus.common.core.domain.R;
|
||||
import com.bonus.common.core.text.Convert;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLDecoder;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 客户端工具类
|
||||
*
|
||||
|
|
@ -245,8 +242,8 @@ public class ServletUtils {
|
|||
* @param value 响应内容
|
||||
* @return Mono
|
||||
*/
|
||||
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, Object value, boolean jimi) {
|
||||
return webFluxResponseWriter(response, HttpStatus.OK, value, R.FAIL, jimi);
|
||||
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, Object value) {
|
||||
return webFluxResponseWriter(response, HttpStatus.OK, value, R.FAIL);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -257,8 +254,8 @@ public class ServletUtils {
|
|||
* @param value 响应内容
|
||||
* @return Mono<Void>
|
||||
*/
|
||||
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, Object value, int code, boolean jimi) {
|
||||
return webFluxResponseWriter(response, HttpStatus.OK, value, code, jimi);
|
||||
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, Object value, int code) {
|
||||
return webFluxResponseWriter(response, HttpStatus.OK, value, code);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -270,8 +267,8 @@ public class ServletUtils {
|
|||
* @param value 响应内容
|
||||
* @return Mono<Void>
|
||||
*/
|
||||
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, HttpStatus status, Object value, int code, boolean jimi) {
|
||||
return webFluxResponseWriter(response, MediaType.APPLICATION_JSON_VALUE, status, value, code, jimi);
|
||||
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, HttpStatus status, Object value, int code) {
|
||||
return webFluxResponseWriter(response, MediaType.APPLICATION_JSON_VALUE, status, value, code);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -284,7 +281,7 @@ public class ServletUtils {
|
|||
* @param value 响应内容
|
||||
* @return Mono<Void>
|
||||
*/
|
||||
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, String contentType, HttpStatus status, Object value, int code, boolean jimi) {
|
||||
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, String contentType, HttpStatus status, Object value, int code) {
|
||||
response.setStatusCode(status);
|
||||
response.getHeaders().add(HttpHeaders.CONTENT_TYPE, contentType);
|
||||
R<?> result = R.fail(code, value.toString());
|
||||
|
|
|
|||
|
|
@ -4,8 +4,6 @@ import com.bonus.common.core.utils.StringUtils;
|
|||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
|
|
@ -13,8 +11,8 @@ import javax.crypto.spec.SecretKeySpec;
|
|||
import java.security.Security;
|
||||
|
||||
/**
|
||||
*
|
||||
* AES加密工具类
|
||||
*
|
||||
* @author HeiZi
|
||||
*/
|
||||
@Slf4j
|
||||
|
|
@ -57,19 +55,19 @@ public class AesCbcUtils {
|
|||
/**
|
||||
* AES加密
|
||||
* (CBC模式)
|
||||
*
|
||||
* @param source 源字符串
|
||||
* @param
|
||||
* @throws Exception
|
||||
* @return 加密后的密文
|
||||
* @throws Exception
|
||||
*/
|
||||
public static String encrypt(String source ) {
|
||||
public static String encrypt(String source, String key, String encoding, String ivParameter, String cipherAlgorithm, String keyAlgorithm) {
|
||||
try {
|
||||
String key=sKey;
|
||||
byte[] sourceBytes = source.getBytes(ENCODING);
|
||||
byte[] keyBytes = key.getBytes(ENCODING);
|
||||
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM, "BC");
|
||||
IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER.getBytes(ENCODING));
|
||||
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyBytes, KEY_ALGORITHM), iv);
|
||||
byte[] sourceBytes = source.getBytes(encoding);
|
||||
byte[] keyBytes = key.getBytes(encoding);
|
||||
Cipher cipher = Cipher.getInstance(cipherAlgorithm, "BC");
|
||||
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes(encoding));
|
||||
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyBytes, keyAlgorithm), iv);
|
||||
byte[] decrypted = cipher.doFinal(sourceBytes);
|
||||
return Base64.encodeBase64String(decrypted);
|
||||
} catch (Exception e) {
|
||||
|
|
@ -93,25 +91,25 @@ public class AesCbcUtils {
|
|||
/**
|
||||
* AES解密
|
||||
* (CBC模式)
|
||||
*
|
||||
* @param data 加密后的密文
|
||||
* @param
|
||||
* @throws Exception
|
||||
* @return 源字符串
|
||||
* @throws Exception
|
||||
*/
|
||||
public static String decrypt(String data) {
|
||||
public static String decrypt(String data, String key, String encoding, String ivParameter, String cipherAlgorithm, String keyAlgorithm) {
|
||||
try {
|
||||
String encryptStr = "";
|
||||
if (StringUtils.isNotEmpty(data)) {
|
||||
encryptStr = data.replace(" ", "+");
|
||||
}
|
||||
String key=sKey;
|
||||
byte[] sourceBytes = Base64.decodeBase64(encryptStr);
|
||||
byte[] keyBytes = key.getBytes(ENCODING);
|
||||
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM, "BC");
|
||||
IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER.getBytes(ENCODING));
|
||||
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyBytes, KEY_ALGORITHM), iv);
|
||||
byte[] keyBytes = key.getBytes(encoding);
|
||||
Cipher cipher = Cipher.getInstance(cipherAlgorithm, "BC");
|
||||
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes(encoding));
|
||||
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyBytes, keyAlgorithm), iv);
|
||||
byte[] decoded = cipher.doFinal(sourceBytes);
|
||||
return new String(decoded, ENCODING);
|
||||
return new String(decoded, encoding);
|
||||
} catch (Exception e) {
|
||||
log.info("------------------->请求加密参数不正确");
|
||||
log.error(e.toString(), e);
|
||||
|
|
|
|||
Loading…
Reference in New Issue