微信第三方掃碼登陸

1 項目應用去微信開放平臺申請,不是公衆平臺json

2 在頁面中添加微信登陸按鈕api

須要修改的參數 appid:申請應用的appid,redirect_uri:登陸後的回調地址(回調地址要在網站應用設置的回調域名下,不能有端口號,url要通過URLcode轉碼),其餘不變服務器

<a href="  https://open.weixin.qq.com/connect/qrconnect?appid=wxed213960e&redirect_uri=https%3a%2f%2frefuge.picp.vip%2fLoginThirdController%2fwxLogin&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect">
   <img src="/images/li_img/weixin.jpg" alt="" class="logo"></a>

3,後臺處理回調接口編寫,登陸成功會回調redirect_uri,並在地址添加code與state參數,只要code參數就行微信

@RequestMapping("wxLogin")
public ModelAndView wxLogin(@RequestParam String code, ModelAndView modelAndView){

 4.用返回的code 參數去微信服務器換取openid(用戶惟一標誌),修改的參數 appid ,secret(申請應用的密碼),code剛取得的codeapp

String url="https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appid+"&secret="+ secret+"&code="+code+"&grant_type=authorization_code";

可用httpclient請求。返回json字符串,用fastjson解析,並提取openid參數網站

JSONObject jsonObject=  JSONObject.parseObject(result);
Object openid=  jsonObject.get("openid");

拿到用戶id,解析完成。url

相關文章
相關標籤/搜索