官方文檔:https://work.weixin.qq.com/api/doc/90001/90142/90595
只做授權登錄的話,直接看身份驗證模塊掃碼授權登錄就可以了
1。構建授權url
@ApiOperation("構造網頁授權鏈接")
@ApiImplicitParams({@ApiImplicitParam(name = "state", value = "用於企業或服務商自行校驗session", dataType = "string", paramType = "query"),
@ApiImplicitParam(name = "redirectUri", value = "授權登錄之后目的跳轉網址", dataType = "string", required = true, paramType = "query")})
@GetMapping("/auth/qywxUrl")
public Result makeUrl(@RequestParam(value = "state") String state,@RequestParam(value = "redirectUri", required = true)String redirectUri){
String qywxAuthUrl = thirdAuth.getQywxAuthUrl(corpID, redirectUri, state);
return Result.success(qywxAuthUrl);
}
public String getQywxAuthUrl(String appid,String redirectUri, String state){
String qywxAuthUrl = "";
try {
// redirect_uri需要經過一次urlencode作為參數
String redirectUri_encode = java.net.URLEncoder.encode(redirectUri, "UTF-8");
qywxAuthUrl = "https://open.work.weixin.qq.com/wwopen/sso/3rd_qrConnect?appid="+appid+"&redirect_uri="+redirectUri_encode+"&state="+state+"&usertype=member";
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return qywxAuthUrl;
}
必須在指定域名下請求這個url ,然后拿到auth_code
2。獲取登錄用戶信息
先獲取服務商憑證
/**
* 獲取服務商憑證
*/
public String getProviderToken(String corpid,String providerSecret) {
String providerToken = null;
if (StringUtils.isNotEmpty(corpid) && StringUtils.isNotEmpty(providerSecret)) {
String url = "https://qyapi.weixin.qq.com/cgi-bin/service/get_provider_token";
JSONObject parma = new JSONObject();
parma.put("corpid", corpid);
parma.put("provider_secret", providerSecret);
String str = restTemplate.postForObject(url, parma.toString(), String.class);
JSONObject response = JSONObject.parseObject(str);
if (response.containsKey("provider_access_token")) {
providerToken = response.getString("provider_access_token");
}
}
return providerToken;
}
//獲取登錄用戶信息
public String getUserInfo(String accessToken,String authCode) {
String userInfo = null;
if (StringUtils.isNotEmpty(accessToken) && StringUtils.isNotEmpty(authCode)) {
String url = "https://qyapi.weixin.qq.com/cgi-bin/service/get_login_info?access_token=" + accessToken;
JSONObject parma = new JSONObject();
parma.put("auth_code", authCode);
String str = restTemplate.postForObject(url, parma.toString(), String.class);
JSONObject response = JSONObject.parseObject(str);
if (response.containsKey("user_info")) {
userInfo = response.getString("user_info");
}
}
return userInfo;
}
先掃碼綁定用戶信息 再登錄校驗用戶信息
