https://open.weixin.qq.com/connect/oauth2/authorize?
是 | ||
是 | ||
是 | ||
是 | ||
否 | ||
是 |
/**
* 第三方登錄接口
*/
@RequestMapping("/login")
public void getCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
//拼接url
StringBuilder url = new StringBuilder(); url.append("https://open.weixin.qq.com/connect/oauth2/authorize?"); //微信開放平臺的appid
url.append("appid=" + ConfigConstans.wechatAppid); // 項目中的公眾號的appid
//轉碼
try {
//回調地址 ,回調地址要進行Encode轉碼
String redirect_uri = URLEncoder.encode(ConfigConstans.contextPath + "/callback", "utf-8");
System.out.println("redirect_uri==" + redirect_uri); url.append("&redirect_uri=" + redirect_uri); //微信授權后回調的地址鏈接,代碼如下
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
url.append("&response_type=code"); url.append("&scope=snsapi_userinfo"); // 當前使用的是授權登錄,(snsapi_base) 不彈出授權頁面,根據需求使用
url.append("&state=" + request.getSession().getId()); // 微信中的回調參數
url.append("#wechat_redirect");
System.out.println("url===" + url.toString());
String s = url.toString(); response.sendRedirect(s);
}
https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html
/**
* 微信 授權登錄回調
**/
@RequestMapping("/callback")
public String callback(String code, String state, HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("====" + code + "===" + state + "===="); log.info("code===" + code);
log.info("state===" + state);
HttpSession session = request.getSession(); //拼寫微信獲取access_token訪問地址
String url = "https://api.weixin.qq.com/sns/oauth2/access_token"; url += "?appid=" + ConfigConstans.wechatAppid; // 使用項目中的公眾號的appid
url += "&secret=" + ConfigConstans.wechatAppsecret; // 使用項目中的公眾號的appsecret
url += "&code=" + code; // code==200時,請求正確,信息返回正確 url += "&grant_type=authorization_code";
log.info("url=="+url); //獲取返回數據
JSONObject userMap = urlHalder(url); // 接收回調信息
//獲取openid
Object openId = userMap.get("openid"); //用戶唯一標識,若程序有PC端和AP端,建議是有回調信息中的uuid
Object accessToken = userMap.get("access_token");
String loginurl = "";
if (ObjectUtils.isNotNull(openId) && ObjectUtils.isNotNull(accessToken)) {
String infourl = "https://api.weixin.qq.com/sns/userinfo"; infourl += "?access_token=" + accessToken;
infourl += "&openid=" + openId; //獲取返回數據
JSONObject userInfoMap = urlHalder(infourl); //根據openId 獲取用戶信息
BasUser basUser = basUserService.getUserByopenId(userInfoMap.get("openid"));
if (ObjectUtils.isNull(basUser)){ //沒有當前數據時,進入程序中的綁定頁面
loginurl = ConfigConstans.contextPath+"/user/login?openId="+userInfoMap.get("openid");
}else { //有當前數據時,進入程序中的首頁 loginurl = ConfigConstans.contextPath+"/index";
} session.setAttribute(UserCacheConstans.USER_OPENID,String.valueOf(userInfoMap.get("openid")));
}
return "redirect:" + loginurl;
}
* 正確時返回的JSON數據包
{
"openid": "OPENID",
"nickname": NICKNAME,
"sex": 1,
"province":"PROVINCE",
"city":"CITY",
"country":"COUNTRY", "headimgurl":"https://thirdwx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46",
"privilege":[ "PRIVILEGE1" "PRIVILEGE2" ],
"unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
}