微信官方文檔:小程序登錄 | 微信開(kāi)放文檔 (qq.com)
微信官方流程圖:
?
一.接入前準(zhǔn)備
1.前端獲取code
獲取微信登錄codehttps://developers.weixin.qq.com/miniprogram/dev/api/open-api/login/wx.login.html
2.獲取微信用戶信息
獲取微信用戶信息https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserProfile.html
后臺(tái)我這邊是使用springboot yml配置
3.yml配置
#微信配置
wx:
#微信公眾號(hào)或者小程序等的appid
appId: xxxxxxxxxxxxxxxxxx
#應(yīng)用密鑰
appSecret: xxxxxxxxxxxxxxxxxx
#微信支付商戶號(hào)
merchantId: xxxxxxxxxxxxxxxxxx
#微信支付商戶密鑰
mchKey: xxxxxxxxxxxxxxxxxx
#商戶證書(shū)目錄
keyPath: xxxxxxxxxxxxxxxxxx
#商戶API私鑰路徑 部署到centos后 /etc/wx-pay/apiclient_cert.pem
privateKeyPath: C:\Users\admin\Desktop\wx\apiclient_cert.pem
#商戶API密鑰證書(shū)路徑 部署到centos后 /etc/wx-pay/apiclient_key.pem
wechatPayCertificatePath: C:\Users\admin\Desktop\wx\apiclient_key.pem
#商戶證書(shū)序列號(hào)
merchantSerialNumber: xxxxxxxxxxxxxxxxxx
#商戶APIV3密鑰
apiV3Key: xxxxxxxxxxxxxxxxxx
4.實(shí)體類參數(shù)配置
@Data
//@Primary 這個(gè)配置暫時(shí)用不上
@Configuration()
@PropertySource("classpath:application.yml") //讀取配置文件
@ConfigurationProperties(prefix = "wx")
public class WXProperties {
/** appId : 微信公眾號(hào)或者小程序等的appid */
private String appId;
/** appSecret : 應(yīng)用密鑰 */
private String appSecret;
/** mchId : 微信支付商戶號(hào) */
private String merchantId;
/** mchKey : 微信支付商戶密鑰 */
private String mchKey;
/** mchKey : 商戶證書(shū)目錄 */
private String keyPath ;
/**商戶API私鑰路徑*/
private String privateKeyPath ;
/**商戶API密鑰證書(shū)路徑*/
private String wechatPayCertificatePath ;
/** 商戶證書(shū)序列號(hào)*/
private String merchantSerialNumber ;
/**商戶APIV3密鑰*/
private String apiV3Key ;
}
二.后臺(tái)接入
1.獲取openid
獲取openid官方文檔https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-login/code2Session.html
? ? ??根據(jù)前端給定微信登錄的code獲取opendiHutool工具官網(wǎng)https://hutool.cn/docs/#/?id=%f0%9f%93%9a%e7%ae%80%e4%bb%8b
@Resource
private WXProperties wxProperties;
public String getOpenid(String code){
//這里是直接拼接的一個(gè)url
String url = "https://api.weixin.qq.com/sns/jscode2session?appid="
+ wxProperties.getAppId() + "&secret=" + wxProperties.getAppSecret()
+ "&js_code=" + code + "&grant_type=authorization_code";
//HttpUtil這個(gè)是hutool工具包里面的,一個(gè)很好用的封裝工具;當(dāng)然你也可以用別的工具
String result = HttpUtil.get(url);
return result;
}
2.我自己的登錄由于需要獲取登錄者的手機(jī)號(hào),代碼如下
微信官方對(duì)不同類型解密文檔https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/signature.html
@PostMapping("/wxLogin")
public Result wxLogin(@RequestParam("code") String code,
@RequestParam("encryptedIv") String encryptedIv,
@RequestParam("rawData") String rawData,
@RequestParam("encryptedData") String encryptedData) {
//wx.login返回的code
log.info("微信登錄參數(shù)code:" + code);
//不需要解密的話可以不傳入這些參數(shù)看自己業(yè)務(wù)
//解密的iv
log.info("登錄信息參數(shù)encryptedIv:" + encryptedIv);
//要解密的數(shù)據(jù)
log.info("登錄信息參數(shù)encryptedData:" + encryptedData);
//rawData微信頭像,昵稱等信息,看自己業(yè)務(wù)需要
log.info("登錄微信賬戶參數(shù)信息rawData:" + rawData);
//想微信服務(wù)器發(fā)送請(qǐng)求獲取用戶信息
//https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
String url = "https://api.weixin.qq.com/sns/jscode2session?appid="
+ wxProperties.getAppId() + "&secret=" + wxProperties.getAppSecret()
+ "&js_code=" + code + "&grant_type=authorization_code";
String result = HttpUtil.get(url );
JSONObject jsonObject = JSONObject.parseObject(result);
log.info("result:" + result);
//獲取session_key和openid
String sessionKey = jsonObject.getString("session_key");
//解密 是獲取微信用戶的手機(jī)號(hào)
String decryptResult = "";
try {
//如果沒(méi)有綁定微信開(kāi)放平臺(tái),解析結(jié)果是沒(méi)有unionid的。
decryptResult = decryptionUserInfo(sessionKey, encryptedIv, encryptedData);
} catch (Exception e) {
e.printStackTrace();
return Result.error("微信登錄失?。?);
}
//解析成功就可以寫(xiě)自己的系統(tǒng)業(yè)務(wù)邏輯了
//我這里就開(kāi)始調(diào)用登錄方法創(chuàng)建token了,至于自己的登錄方法邏輯是什么要做什么邏輯處理就看業(yè)務(wù)需要了
if (StringUtils.hasText(decryptResult)) {
//如果解析成功,獲取token
String token = loginService.wxLogin(decryptResult, result, rawData);
Result r = Result.success();
r.put(Constants.TOKEN, token);
return r;
} else {
return AjaxResult.error("微信登錄失??!");
}
}
用戶信息解密文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-759728.html
public String decryptionUserInfo(String sessionKey, String iv, String encryptedData) {
String result = null;
// 被加密的數(shù)據(jù)
byte[] dataByte = Base64.decode(encryptedData);
// 加密秘鑰
byte[] keyByte = Base64.decode(sessionKey);
// 偏移量
byte[] ivByte = Base64.decode(iv);
try {
// 如果密鑰不足16位,那么就補(bǔ)足. 這個(gè)if 中的內(nèi)容很重要
int base = 16;
if (keyByte.length % base != 0) {
int groups = keyByte.length / base + (keyByte.length % base != 0 ? 1 : 0);
byte[] temp = new byte[groups * base];
Arrays.fill(temp, (byte) 0);
System.arraycopy(keyByte, 0, temp, 0, keyByte.length);
keyByte = temp;
}
//初始化
AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivByte);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKeySpec keySpec = new SecretKeySpec(keyByte, "AES");
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
byte[] doFinal = cipher.doFinal(dataByte);
result = new String(doFinal);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
最后有一個(gè)參考文獻(xiàn),找不到了,有遇到的就提醒一下咯文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-759728.html
到了這里,關(guān)于微信小程序接入微信登錄后端API的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!