SpringBoot⼩程序解密获取unionId(JAVA)
如果该⽤户登录过⼩程序或者登陆过tong同⼀平台下的⼩程序,可以直接根据openid获取到⽤户唯⼀标识unionId
代码如下
@ApiOperation(value = "获取UnionID")
@PostMapping(value = "/getUnionID")
public SuccessOutPut<AppOutPut> getUnionID(@RequestBody StringInput code) throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
StringBuffer info = new StringBuffer("api.weixin.qq/cgi-bin/user/info?");
String accessToken = AccessToken();
if (null != accessToken) {
info.append("access_token=").append(accessToken).toString();
} else {
String accessToken2 = AccessToken();
info.append("access_token=").append(accessToken2).toString();
}
info.append("&openid="+StrValue()).append("&lang=zh_CN").toString();
HttpEntity<String> entity = new HttpEntity<String>(headers);
String strbody = String(), HttpMethod.GET, entity, String.class)
.getBody();
LOG.info(strbody);
return new SuccessOutPut<AppOutPut>(JsonUtil.jsonToObject(strbody, AppOutPut.class));
}
如果没有登陆的⽤户向进⾏同⼀平台下⼩程序之前的跳转不再进⾏授权,还要保留该⼩程序的⽤户的信息和资料,当跳转到不同的⼩程序,要根据⽤户的唯⼀标识去获取⽤户⾃⼰的信息,这就需要加密解密。
该⽅法是CSDN⼀位博主分享的,具体的忘了在哪看到的,sorry。
代码如下,根据⾃⼰的需要进⾏更改:
@ApiOperation(value = "加密解密")
@PostMapping(value = "/decodeUserInfo")
public SuccessOutPut<Map<String,Object>> decodeUserInfo(@RequestBody WxInput input) {
Map<String,Object> map = new HashMap<String,Object>();
//登录凭证不能为空
if (Code() == null || Code().length() == 0) {
map.put("status", 0);
map.put("msg", "code 不能为空");
return new SuccessOutPut<Map<String,Object>>(map);
}
//⼩程序唯⼀标识  (在⼩程序管理后台获取)
String wxspAppid = "在⼩程序管理后台获取";
//⼩程序的 app secret (在⼩程序管理后台获取)
String wxspSecret = "在⼩程序管理后台获取";
//授权(必填)代码转换
String grant_type = "authorization_code";
1、向服务器使⽤登录凭证 code 获取 session_key 和 openid
/
/请求参数
String params = "appid=" + wxspAppid + "&secret=" + wxspSecret + "&js_code=" + Code() + "&grant_type=" + grant_type;        //发送请求
String sr = HttpRequest.sendGet("api.weixin.qq/sns/jscode2session", params);
//解析相应内容(转换成json对象)
JSONObject json = JSONObject.parseObject(sr);
//获取会话密钥(session_key)
String session_key = ("session_key").toString();
//⽤户的唯⼀标识(openid)
String openid = (String) ("openid");
System.out.println(openid);
2、对encryptedData加密数据进⾏AES解密
try {
String result = AesUtil.EncryptedData(), session_key, Iv(), "UTF-8");
if (null != result && result.length() > 0) {
map.put("status", 1);
map.put("msg", "解密成功");
JSONObject userInfoJSON = JSONObject.parseObject(result);
Map<String,Object> userInfo = new HashMap<String,Object>();
userInfo.put("openId", ("openId"));
userInfo.put("nickName", ("nickName"));
userInfo.put("gender", ("gender"));
userInfo.put("city", ("city"));
userInfo.put("province", ("province"));
userInfo.put("country", ("country"));
userInfo.put("avatarUrl", ("avatarUrl"));
userInfo.put("unionId", ("unionId"));
map.put("userInfo", userInfo);
return new SuccessOutPut<Map<String,Object>>(map);
}
} catch (Exception e) {
e.printStackTrace();
}
map.put("status", 0);
map.put("msg", "解密失败");
return new SuccessOutPut<Map<String,Object>>(map);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。