知識
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價值,我們在追求其視覺表現(xiàn)的同時,更側(cè)重于功能的便捷,營銷的便利,運營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序為后期升級提供便捷的支持!
您當(dāng)前位置>首頁 » 新聞資訊 » 小程序相關(guān) >
支付寶小程序獲取手機(jī)號(證書方式解密)及生產(chǎn)小程序碼代碼示例
發(fā)表時間:2020-10-19
發(fā)布人:葵宇科技
瀏覽次數(shù):224
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipayEncrypt;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayOpenAppQrcodeCreateRequest;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.response.AlipayOpenAppQrcodeCreateResponse;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.ijpay.alipay.AliPayApi;
import com.ijpay.alipay.AliPayApiConfig;
import com.ijpay.alipay.AliPayApiConfigKit;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.NotEmpty;
import org.jservice.boot.core.error.exception.BusiException;
import org.jservice.message.simplejson.dt.out.OutDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
/**
* @author yz
* @className AlipayLoginSvc
* @description
* @date 2020/7/31 9:56
*/
@RequestMapping("/alipayLogin")
@Service
@Validated
public class AlipayLoginSvc {
private final static Logger LOGGER = LoggerFactory.getLogger(AlipayLoginSvc.class);
/**
* 解密成功MSG
*/
private final static String DECRYPT_SUCCESS_MSG = "Success";
@Autowired
private AliPayMiniAppConfig aliPayMiniAppConfig;
/**
* AES接口加密秘鑰
*/
@NotEmpty
@Value("${alipay.miniapp.aesEncryptKey}")
private String miniAesEncryptKey;
/**
* @param oauthTokenRequest {@link AlipaySystemOauthTokenRequest}
* @return {@link OutDTO<AlipaySystemOauthTokenResponse>}
* @description 通過code換取授權(quán)訪問令牌
* @author yz
* @date 2020/7/31 10:36
*/
public OutDTO<AlipaySystemOauthTokenResponse> codeToToken4Mini(@RequestBody AlipaySystemOauthTokenRequest oauthTokenRequest) {
if (StringUtils.isAnyBlank(oauthTokenRequest.getCode(), oauthTokenRequest.getGrantType())) {
return Response.invalid(null);
}
try {
//設(shè)置支付配置
AliPayApiConfigKit.setThreadLocalAliPayApiConfig(aliPayMiniAppConfig.getConfig());
AlipaySystemOauthTokenResponse response = AliPayApi.certificateExecute(oauthTokenRequest);
if (response.isSuccess()) {
return Response.success(response);
}
} catch (AlipayApiException e) {
LOGGER.error("支付寶code換取token授權(quán)失敗!", e);
}
return Response.error("授權(quán)失敗!", null);
}
/**
* @param encryptedStrMap 加密字符串Map
* @return {@link org.jservice.message.simplejson.dt.out.OutDTO<java.lang.String>}
* @description 解密手機(jī)號
* @author yz
* @date 2020/8/1 10:50
*/
public OutDTO<String> decryptPhoneNo(@RequestBody Map<String, String> encryptedStrMap) {
String encryptedStr = encryptedStrMap.get("encryptedStr");
if (StringUtils.isBlank(encryptedStr)) {
return Response.invalid();
}
AliPayApiConfig config;
try {
config = aliPayMiniAppConfig.getConfig();
} catch (AlipayApiException e) {
LOGGER.error("獲取配置失敗!", e);
return Response.error("系統(tǒng)錯誤!");
}
try {
String decryptStr = doDecrypt(encryptedStr, config);
//轉(zhuǎn)為map
Map decryptMap = JSON.parseObject(decryptStr, Map.class);
if (DECRYPT_SUCCESS_MSG.equals(decryptMap.get("msg"))) {
return Response.success((String) decryptMap.get("mobile"));
} else {
LOGGER.error("手機(jī)號獲取失敗!{}", decryptMap.get("msg"));
return Response.error("手機(jī)號獲取失敗!");
}
} catch (Exception e) {
return Response.error(e.getMessage());
}
}
/**
* @param encryptedStr 加密字符串
* @param config 支付寶配置
* @return {@link String}
* @description 解密方法
* @author yz
* @date 2020/8/1 10:47
*/
private String doDecrypt(String encryptedStr, AliPayApiConfig config) throws Exception {
//1. 獲取驗簽和解密所需要的參數(shù)
Map<String, String> openapiResult = JSON.parseObject(encryptedStr, new TypeReference<Map<String, String>>() {
},
Feature.OrderedField);
String content = openapiResult.get("response");
String sign = openapiResult.get("sign");
//2.判斷報文是否加密
//若報文沒有加密,則 response 的值為 json,而加密情況 response 是 Base64 字符串。
//所以可以通過 response 的值是否是括號符開頭:”{“來進(jìn)行判斷 。
boolean isDataEncrypted = !content.startsWith("{");
//3.驗簽
boolean signCheckPass;
String signContent = isDataEncrypted ? "\"" + content + "\"" : content;
//如果是加密的報文則需要在密文的前后添加雙引號
try {
signCheckPass = AlipaySignature.rsaCertCheck(signContent, sign, config.getAliPayCertPath(), "UTF-8",
"RSA2");
} catch (AlipayApiException e) {
LOGGER.error("驗簽失敗", e);
throw new BusiException("驗簽失敗!");
}
if (!signCheckPass) {
//驗簽不通過(異?;蛘邎笪谋淮鄹?#xff09;,終止流程(不需要做解密)
throw new BusiException("驗簽失敗!");
}
//4. 解密
try {
//未加密直接返回原內(nèi)容,否則解密后返回
return isDataEncrypted ? AlipayEncrypt.decryptContent(content, "AES", miniAesEncryptKey,
config.getCharset()) : content;
} catch (AlipayApiException e) {
//解密異常, 記錄日志
LOGGER.error("解密異常", e);
throw new BusiException("解密異常");
}
}
/**
* 生成支付寶小程序碼
*
* @author yz
* @date 2020/8/4 9:25
*/
public OutDTO<String> getAlipayQrCode(@RequestBody Map<String,String> bizContentMap) throws AlipayApiException {
AliPayApiConfigKit.setThreadLocalAliPayApiConfig(aliPayMiniAppConfig.getConfig());
AlipayOpenAppQrcodeCreateRequest request = new AlipayOpenAppQrcodeCreateRequest();
String bizContent = JSON.toJSONString(bizContentMap);
request.setBizContent(bizContent);
AlipayOpenAppQrcodeCreateResponse response = AliPayApi.certificateExecute(request);
if(response.isSuccess()){
return Response.success(response.getQrCodeUrl());
} else {
return Response.error("調(diào)用失敗");
}
}
}
相關(guān)案例查看更多
相關(guān)閱讀
- 小程序退款
- 汽車拆解管理系統(tǒng)
- 昆明小程序開發(fā)
- 云南小程序制作
- 搜索引擎自然排名
- 云南軟件公司
- 生成海報
- 百度小程序開發(fā)公司
- 云南小程序開發(fā)公司推薦
- 網(wǎng)站沒排名
- flex
- 英文網(wǎng)站建設(shè)公司
- 云南網(wǎng)站建設(shè)招商
- 云南小程序商城
- 小程序開發(fā)平臺前十名
- web開發(fā)技術(shù)
- 網(wǎng)站建設(shè)服務(wù)
- 云南網(wǎng)站建設(shè)制作
- 云南網(wǎng)站建設(shè)百度
- 云南科技公司
- 昆明網(wǎng)站制作
- 云南etc微信小程序
- 網(wǎng)站排名優(yōu)化
- 云南省建設(shè)廳官方網(wǎng)站
- 報廢車管理
- 網(wǎng)站建設(shè)價格
- 網(wǎng)站建設(shè)快速優(yōu)化
- 迪慶小程序開發(fā)
- 云南軟件開發(fā)
- 汽車回收管理系統(tǒng)