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