微信登录

2020-03-22 16:05:12来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

微信登录

------------恢复内容开始------------

1.背景:现在很多app或者网站都想要接入微信登录,可以使用户不需要注册就能快速使用APP或网站。

2.微信登录需要一些前置操作

  2.1 搜索:微信开放平台 链接:https://open.weixin.qq.com/

  2.2 注册成功,获取到开发所需要的appID和appsecret

  

 

 3.微信的登录的交互流程

  3.1 微信用户首先向第三APP请求登录 

  3.2 第三APP弹出一个二维码(这个二维码可以由客户端或者H5去做),用户扫描二维(请求微信Oauth2.0授权登录)

  3.3 app弹出一个确认登录的页面(微信平台请求用户确认)

  3.4 用户点击确认

  3.5 微信公众平台拉起第三方或者重定向到第三方,带上授权临时票据code(前端或者客户端的工作,前端把回调地址放在3.2步骤的,或者在公众平台配置)

  3.6 请求到服务端:服务端需要把code、appId、appscret作为参数,在程序内部调起微信接口,获取到access_token和openId

     

  3.7 获取到用户的access_token和openId后就可以获取到用户的微信信息了

    

 

 

   3.8 获取到用户unionId后,后端进行存储,为该用户生成一条用户信息,用户登录成功,后端可以重定向到前端某一个登录的页面,也可以让前端,根据返回值进行跳              转

   3.9 登录成功后的操作,前端请求后端接口时把unionId带上,后端根据unionId,确定,当前用户unionId,找到用户在自己APP的ID

4. 附上完整代码:

  HTTPClient:maven 依赖

 

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.6</version>
</dependency>


package com.example.student.project.controller;

import com.example.student.project.domain.WXUserInfoData;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Map;

@Controller
@RequestMapping(value = "/CourtSystem")
public class WeiXinLoginController {

private static final Logger logger = LoggerFactory.getLogger(WeiXinLoginController.class);

public static final String WX_AUTH_LOGIN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token";
public static final String WX_USERINFO_URL = "https://api.weixin.qq.com/sns/userinfo";
//appid和appSecret 是在公众平台上申请的
//AppId:应用唯一标识,在微信开放平台提交应用审核通过后获得
public static final String WX_APP_ID = "wx9ecd6f7******";
//AppSecret:应用密钥AppSecret,在微信开放平台提交应用审核通过后获得
public static final String WX_APP_KEY = "c1bf387181aaf6e5ff********";


/**
* 第三方微信登录
* @param code 客户端返回的code
* @return
*/
@RequestMapping(value = "/checkLogin", method = RequestMethod.POST)
@ResponseBody
public WXUserInfoData checkLogin(String code) throws JSONException {
//通过code获取access_token
//String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code"
StringBuffer loginUrl = new StringBuffer();
//url拼接
// WX_AUTH_LOGIN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token"
//AppId:应用唯一标识,在微信开放平台提交应用审核通过后获得
//WX_APP_ID = "wx9ecd6f7******";
//AppSecret:应用密钥AppSecret,在微信开放平台提交应用审核通过后获得
//WX_APP_KEY = "c1bf387181aaf6e5ff********";
loginUrl.append(WX_AUTH_LOGIN_URL).append("?appid=") //AppId
.append(WX_APP_ID).append("&secret=") //AppSecret
.append(WX_APP_KEY).append("&code=").append(code) //填写第二步获取的code参数
.append("&grant_type=authorization_code"); //填authorization_code(固定,来自于官方文档)
String loginRet = get(loginUrl.toString());
JSONObject grantObj = new JSONObject(loginRet);
String errcode = grantObj.optString("errcode");
if (!StringUtils.isEmpty(errcode))
{
logger.error("login weixin error"+loginRet);
return null;
}
String openId = grantObj.optString("openid");
if (StringUtils.isEmpty(openId))
{
logger.error("login weixin getOpenId error"+loginRet);
return null;
}

String accessToken = grantObj.optString("access_token"); //接口调用凭证
String expiresIn = grantObj.optString("expires_in"); // access_token接口调用凭证超时时间,单位(秒)
String refreshToken = grantObj.optString("refresh_token"); //用户刷新access_token
String scope = grantObj.optString("scope"); //用户授权的作用域,使用逗号(,)分隔

//通过access_token获取用户微信信息
StringBuffer userUrl = new StringBuffer();
// String url = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID";
// 第三步获取的access_token ; OPENID:第三步获取的openId
//WX_USERINFO_URL = "https://api.weixin.qq.com/sns/userinfo"
userUrl.append(WX_USERINFO_URL).append("?access_token=").append(accessToken).append("&openid=").append(openId);
String userRet = get(userUrl.toString());
JSONObject userObj = new JSONObject(userRet);
WXUserInfoData userInfo = new WXUserInfoData();
userInfo.setOpenId(openId); // 用户标识
userInfo.setAuthToken(accessToken);
userInfo.setAuthRefreshToken(refreshToken); // 专用于刷新access token的token
userInfo.setScope(scope); // scope:权限
userInfo.setExpiresIn(Integer.valueOf(expiresIn));
String nickname = userObj.optString("nickname"); // 用户昵称
String sex = userObj.optString("sex"); // 1男,2女,0未知
String userImg = userObj.optString("headimgurl"); //头像链接
String unionid = userObj.optString("unionid"); //全局唯一标识
userInfo.setName(nickname);
userInfo.setIcon(userImg);
userInfo.setGender(sex);
userInfo.setLoginId(unionid);
return userInfo;
}


public static String get(String url) {
String body = null;
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
logger.info("create httppost:" + url);
HttpGet get = new HttpGet(url);
get.addHeader("Accept-Charset","utf-8");
HttpResponse response = sendRequest(httpClient, get);
body = parseResponse(response);
} catch (IOException e) {
logger.error("send post request failed: {}", e.getMessage());
}

return body;
}

private static String paramsToString(Map<String, String> params) {
StringBuilder sb = new StringBuilder();
try{
for (String key : params.keySet()) {
sb.append(String.format("&%s=%s", key, URLEncoder.encode(params.get(key), StandardCharsets.UTF_8.toString())));
}
}catch(UnsupportedEncodingException e){
logger.warn("{}: encode url parameters failed", e.getMessage());
}
return sb.length() > 0 ? "?".concat(sb.substring(1)) : "";
}

private static HttpResponse sendRequest(CloseableHttpClient httpclient, HttpUriRequest httpost)
throws ClientProtocolException, IOException {
HttpResponse response = null;
response = httpclient.execute(httpost);
return response;
}

private static String parseResponse(HttpResponse response) {
logger.info("get response from http server..");
HttpEntity entity = response.getEntity();

logger.info("response status: " + response.getStatusLine());
Charset charset = ContentType.getOrDefault(entity).getCharset();
if (charset != null) {
logger.info(charset.name());
}

String body = null;
try {
body = EntityUtils.toString(entity, "utf-8");
logger.info("body " + body);
} catch (IOException e) {
logger.warn("{}: cannot parse the entity", e.getMessage());
}

return body;
}


}

 


原文链接:https://www.cnblogs.com/zhaochaoyeu/p/12549067.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:NIO中的ZeroCopy

下一篇:Cookie和Seesion