feat:宁波银行登录加解密

This commit is contained in:
dzc committed 2025-04-30 10:02:41 +08:00
1 parent 2feefc9659
commit 4f3a8c1883
6 files changed
+74 -6

No files matched your search

@@ -1,5 +1,6 @@
package com.bsd.pjcloud.thirdparty.dto.lb;
import jakarta.validation.constraints.NotEmpty;
import lombok.Data;
@Data
@@ -8,6 +9,7 @@ public class LbH5LoginDto {
/**
* 用户手机号
*/
@NotEmpty(message = "手机号不能为空")
private String phone;
}
@@ -23,4 +23,9 @@ public class LbConfig {
*/
private String companyNumber;
/**
* 国密sm4密钥
*/
private String sm4Key;
}
@@ -1,11 +1,13 @@
package com.bsd.pjcloud.thirdparty.controller.lb;
import com.bsd.pjcloud.common.core.util.R;
import com.bsd.pjcloud.common.security.annotation.Inner;
import com.bsd.pjcloud.thirdparty.dto.lb.LbH5LoginDto;
import com.bsd.pjcloud.thirdparty.service.ThirdLbApiService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;
@@ -19,9 +21,10 @@ public class ThirdLbApiController {
private final ThirdLbApiService thirdLbApiService;
@Inner(value = false)
@Operation(summary = "美好生活登录", description = "美好生活登录")
@PostMapping("/h5-login")
public R H5Login(@RequestBody LbH5LoginDto dto) {
public R H5Login(@Valid @RequestBody LbH5LoginDto dto) {
return R.ok(thirdLbApiService.h5Login(dto));
}
@@ -8,7 +8,7 @@ import com.bsd.pjcloud.thirdparty.dto.lb.LbH5LoginDto;
import com.bsd.pjcloud.thirdparty.service.ThirdLbApiService;
import com.bsd.pjcloud.thirdparty.utils.door.api.DoorApi;
import com.bsd.pjcloud.thirdparty.utils.lb.api.LbApi;
import com.bsd.pjcloud.thirdparty.vo.ks.KsBaseVo;
import com.bsd.pjcloud.thirdparty.utils.lb.tool.LbRetailSignUtils;
import com.bsd.pjcloud.thirdparty.vo.lb.LbH5LoginVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@@ -35,7 +35,7 @@ public class ThirdLbApiServiceImpl implements ThirdLbApiService {
public LbH5LoginVo h5Login(LbH5LoginDto dto) {
LbH5LoginVo ksBaseVo = new LbH5LoginVo();
String phone = dto.getPhone();
String signPhone = phone;
String signPhone = LbRetailSignUtils.sm4Encrypt(phone, lbConfig.getSm4Key());
long timeMillis = System.currentTimeMillis();
String signature = apiCountersign(dto.getPhone(), timeMillis);
Map<String, String> queryMap = new HashMap<>();
@@ -51,7 +51,7 @@ public class ThirdLbApiServiceImpl implements ThirdLbApiService {
if (ObjectUtil.isEmpty(res)) throw new RuntimeException("response响应为空");
ksBaseVo = JSONUtil.toBean(res, LbH5LoginVo.class);
} catch (Exception e) {
String error = String.format("|%s||skuSubmit|意外的错误error|%s|stack|%s|", TAG, e.getMessage(), Arrays.toString(e.getStackTrace()));
String error = String.format("|%s||h5Login|意外的错误error|%s|stack|%s|", TAG, e.getMessage(), Arrays.toString(e.getStackTrace()));
log.error(error);
}
return ksBaseVo;
@@ -60,7 +60,7 @@ public class ThirdLbApiServiceImpl implements ThirdLbApiService {
// 加签
public String apiCountersign(String phone, Long timestamp) {
String str = String.format("companyNumber=%s&appId=%s&phone=%s&timestamp=%s&secret=%s");
String str = "companyNumber=%s&appId=%s&phone=%s&timestamp=%s&secret=%s";
str = String.format(str, lbConfig.getCompanyNumber(), lbConfig.getAppId(), phone, timestamp, lbConfig.getAppSecret());
// 使用 SHA-256 进行哈希计算,并返回十六进制字符串。
String sha256 = DigestUtils.sha256Hex(str);
@@ -0,0 +1,56 @@
package com.bsd.pjcloud.thirdparty.utils.lb.tool;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.Security;
import java.util.Base64;
@Slf4j
@UtilityClass
public class LbRetailSignUtils {
static {
// 添加安全提供者(SM2,SM3,SM4等加密算法,CBC、CFB等加密模式,PKCS7Padding等填充方式,不在Java标准库中,由BouncyCastleProvider实现)
Security.addProvider(new BouncyCastleProvider());
}
public static String sm4Encrypt(String plainString, String key) {
String cipherString = null;
try {
String algorithm = "SM4";
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), algorithm);
Cipher cipher = Cipher.getInstance(algorithm + "/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
byte[] cipherBytes = cipher.doFinal(plainString.getBytes(StandardCharsets.UTF_8));
cipherString = Base64.getEncoder().encodeToString(cipherBytes);
} catch (Exception e) {
e.printStackTrace();
}
return cipherString;
}
/**
* SM4,商业密码(Shang Mi4)是中华人民共和国政府采用的一种分组密码标准,由国家密码管理局于2012年3月21日发布,相关标准为“GM/T 0002-2012《SM4分组密码算法》”。
*/
public static String sm4Decrypt(String cipherString, String key) {
String plainString = null;
try {
String algorithm = "SM4";
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), algorithm);
Cipher cipher = Cipher.getInstance(algorithm + "/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
byte[] cipherBytes = cipher.doFinal(Base64.getDecoder().decode(cipherString));
plainString = new String(cipherBytes);
} catch (Exception e) {
e.printStackTrace();
}
return plainString;
}
}
@@ -89,10 +89,12 @@ zy:
# callback-recognition-url: /third/third-ks/callback/recognition
# callback-sku-url: /third/third-ks/callback/sku
lb:
base-url: https://elss-sit.nbcb.com.cn
# base-url: https://elss-sit.nbcb.com.cn
base-url: https://elss-uat.nbcb.com.cn
app-id: c4b0a38e9ba5419eab9b651dd23db462
app-secret: iW9H6BT2AMXg2XnN2Eq+r6ISossJNNh97nyLwrrOrDY=
company-number: p6VQYhHZ
sm4-key: 1234567812345678
sms: