From 3ddf96bd4ccbed71b44549ff32f62f7e3c1eba9d Mon Sep 17 00:00:00 2001 From: wenc000 <450292408@qq.com> Date: Fri, 30 Oct 2020 16:34:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EAES=E5=8A=A0=E5=AF=86?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/WechatMiniAppAuthServiceImpl.java | 6 +++++- .../main/java/com/cm/common/utils/AesUtil.java | 15 ++++++++++----- pom.xml | 8 ++++++++ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/cloud-common-wechat/src/main/java/com/cm/common/wechat/service/impl/WechatMiniAppAuthServiceImpl.java b/cloud-common-wechat/src/main/java/com/cm/common/wechat/service/impl/WechatMiniAppAuthServiceImpl.java index 4a8fc1c..846816d 100644 --- a/cloud-common-wechat/src/main/java/com/cm/common/wechat/service/impl/WechatMiniAppAuthServiceImpl.java +++ b/cloud-common-wechat/src/main/java/com/cm/common/wechat/service/impl/WechatMiniAppAuthServiceImpl.java @@ -77,7 +77,11 @@ public class WechatMiniAppAuthServiceImpl implements IWechatMiniAppAuthService { LOG.debug("绑定用户 | 登录"); String wechatSignInfo = Base64.encodeBase64String(AesUtil.aesCommonEncoder("WECHAT_SIGN_INFO", new StringBuilder(wechatMiniAppUserInfo.getOpenid()).append("_WenG_").append(wechatMiniAppProperties.getAppKey()).toString()).getBytes("UTF-8")); String token = getAppToken(wechatSignInfo); - WechatMiniAppManager.getInstance().addUser(token, wechatMiniAppUserInfo); + if (token.endsWith("_0") || token.endsWith("_1")) { + WechatMiniAppManager.getInstance().addUser(token.substring(0, token.length() - 2), wechatMiniAppUserInfo); + } else { + WechatMiniAppManager.getInstance().addUser(token, wechatMiniAppUserInfo); + } return new SuccessResultData(token); } diff --git a/cloud-common/src/main/java/com/cm/common/utils/AesUtil.java b/cloud-common/src/main/java/com/cm/common/utils/AesUtil.java index b2f1122..bfca12f 100644 --- a/cloud-common/src/main/java/com/cm/common/utils/AesUtil.java +++ b/cloud-common/src/main/java/com/cm/common/utils/AesUtil.java @@ -1,15 +1,13 @@ package com.cm.common.utils; import org.apache.commons.codec.binary.Base64; +import org.apache.commons.codec.binary.StringUtils; import javax.crypto.*; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.io.UnsupportedEncodingException; -import java.security.InvalidAlgorithmParameterException; -import java.security.InvalidKeyException; -import java.security.NoSuchAlgorithmException; -import java.security.SecureRandom; +import java.security.*; public class AesUtil { @@ -110,6 +108,9 @@ public class AesUtil { byte[] enCodeFormat = key.getBytes(); SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES"); IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes); + if (StringUtils.equals(paddingType, PKCS_7)) { + Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); + } Cipher cipher = Cipher.getInstance("AES/CBC/" + paddingType); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec); return cipher.doFinal(byteContent); @@ -143,7 +144,11 @@ public class AesUtil { public static byte[] aesCommonDecoderDetail(byte[] keyBytes, byte[] encryptedBytes, byte[] ivBytes, String paddingType) throws Exception { SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES"); IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes); - Cipher cipher = Cipher.getInstance("AES/CBC/" + paddingType); + Cipher cipher; + if (StringUtils.equals(paddingType, PKCS_7)) { + Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); + } + cipher = Cipher.getInstance("AES/CBC/" + paddingType); cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec); return cipher.doFinal(encryptedBytes); } diff --git a/pom.xml b/pom.xml index 38bb0bb..22f7f16 100644 --- a/pom.xml +++ b/pom.xml @@ -78,6 +78,7 @@ 5.5.0 1.0.9.RELEASE 4.1.50.Final + 1.46 @@ -431,6 +432,13 @@ ${netty.version} + + + org.bouncycastle + bcprov-jdk15on + 1.56 + +