知識
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價值,我們在追求其視覺表現(xiàn)的同時,更側(cè)重于功能的便捷,營銷的便利,運營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序為后期升級提供便捷的支持!
您當(dāng)前位置>首頁 » 新聞資訊 » 公眾號相關(guān) >
spring boot實現(xiàn)微信公眾號授權(quán)登錄
發(fā)表時間:2020-10-22
發(fā)布人:葵宇科技
瀏覽次數(shù):111
1.在配置文件中配置微信公眾號的appid和secret如下圖
2.controller中實現(xiàn)微信授權(quán)
即后臺根據(jù)參數(shù)拼接url返回給前端,前端通過請求后臺返回的url實現(xiàn)授權(quán),實現(xiàn)代碼如下
@Value("${app.weixin.gzh.appid}") String appidgzh; @Value("${app.weixin.gzh.secret}") String secretgzh; @PostMapping("/weixin/login") @ApiOperation(value = "微信授權(quán)登錄", httpMethod = "POST", produces = "application/json;charset=UTF-8") public Result weixinLogin(HttpServletRequest request, HttpServletResponse response){ try { Map<String,Object> map = new HashMap<>(); //這里是回調(diào)的url String redirect_uri = URLEncoder.encode(rootUrl+"/weixin/callBack", "UTF-8"); String url = "https://open.weixin.qq.com/connect/oauth2/authorize?" + "appid=APPID" + "&redirect_uri=REDIRECT_URI"+ "&response_type=code" + "&scope=SCOPE" + "&state=123#wechat_redirect"; map.put("datas",url.replace("APPID",appidgzh).replace("REDIRECT_URI",redirect_uri).replace("SCOPE","snsapi_userinfo")); return Result.ok(map); }catch (Exception e){ e.printStackTrace(); return Result.error(901,"微信授權(quán)登錄失敗!"); } }
3.微信授權(quán)后回調(diào)
前端在授權(quán)成功后會回調(diào)授權(quán)回調(diào)接口獲取授權(quán)用戶的openid和用戶信息,實現(xiàn)代碼如下:
@GetMapping("/weixin/callBack") @ApiOperation(value = "微信授權(quán)回調(diào)", httpMethod = "GET", produces = "application/json;charset=UTF-8") public Result weixinCallBack(HttpServletRequest request, HttpServletResponse response){ try { Map<String,Object> map = new HashMap<>(); //獲取回調(diào)地址中的code String code = request.getParameter("code"); //拼接url String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appidgzh + "&secret=" + secretgzh + "&code=" + code + "&grant_type=authorization_code"; JSONObject jsonObject = AuthUtil.doGetJson(url); if(jsonObject.has("errcode")){ Integer errcode = jsonObject.getInt("errcode"); if(null!=errcode){ String errmsg = jsonObject.getString("errmsg"); return Result.fail(errcode,errmsg); } } //1.獲取微信用戶的openid String openid = jsonObject.getString("openid"); //2.獲取獲取access_token String access_token = jsonObject.getString("access_token"); String infoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid + "&lang=zh_CN"; //3.獲取微信用戶信息 JSONObject userInfo = AuthUtil.doGetJson(infoUrl); //去數(shù)據(jù)庫查詢此微信是否注冊過 User user = userService.getDao().getRepo().findByOpenidgzh(openid); map.put("user",user); map.put("userInfo",userInfo.toString()); map.put("token", user!=null?tokenService.generate(user.getId(), 5*24*60):""); return Result.ok(map); }catch (Exception e){ e.printStackTrace(); return Result.error(901,"微信授權(quán)回調(diào)失敗!"); } }
到此,微信授權(quán)就算結(jié)束了,在微信授權(quán)回調(diào)接口中,如果返回的用戶是空的,前端會走用戶注冊接口,如果用戶不為空,會走登錄接口。
在上述代碼中用到的工具類如下:
AuthUtil.class
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.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.json.JSONObject; import java.io.IOException; public class AuthUtil { public static JSONObject doGetJson(String url) throws ClientProtocolException, IOException { JSONObject jsonObject = null; DefaultHttpClient client = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); HttpResponse response = client.execute(httpGet); HttpEntity entity = response.getEntity(); if (entity != null) { String result = EntityUtils.toString(entity, "UTF-8"); jsonObject = new JSONObject(result); } httpGet.releaseConnection(); return jsonObject; } }
另外,如果在本地測試的話,需要對本地IP做一個內(nèi)網(wǎng)穿透,我在測試本地時,用的NATAPP做的域名映射本地,但是這個不是很好用,如果大家有好的建議,歡迎提出。
相關(guān)案例查看更多
相關(guān)閱讀
- 云南做百度小程序的公司
- 網(wǎng)站建設(shè)需要多少錢
- 云南省城鄉(xiāng)建設(shè)廳網(wǎng)站
- 云南網(wǎng)站建設(shè)方案 doc
- painter
- 昆明小程序公司
- 重慶網(wǎng)站建設(shè)公司
- 手機網(wǎng)站建設(shè)
- 網(wǎng)絡(luò)公司報價
- 曲靖小程序開發(fā)
- 搜索排名
- 日歷組件
- 云南建設(shè)廳官方網(wǎng)站
- 網(wǎng)站建設(shè)費用
- python開發(fā)小程序
- 網(wǎng)站建設(shè)價格
- 云南網(wǎng)站設(shè)計
- 云南手機網(wǎng)站建設(shè)
- 網(wǎng)站建設(shè)優(yōu)化
- typescript
- 模版信息
- 云南etc微信小程序
- 網(wǎng)站建設(shè)服務(wù)公司
- 網(wǎng)絡(luò)公司聯(lián)系方式
- 云南小程序開發(fā)公司推薦
- 云南小程序開發(fā)制作公司
- 開發(fā)框架
- 國內(nèi)知名網(wǎng)站建設(shè)公司排名
- 買小程序被騙
- 云南網(wǎng)站建設(shè)服務(wù)公司