欧美三级国产三级日韩三级_亚洲熟妇丰满大屁股熟妇_欧美亚洲成人一区二区三区_国产精品久久久久久模特

spring boot實(shí)現(xiàn)微信公眾號(hào)授權(quán)登錄 - 新聞資訊 - 云南小程序開(kāi)發(fā)|云南軟件開(kāi)發(fā)|云南網(wǎng)站建設(shè)-昆明葵宇信息科技有限公司

159-8711-8523

云南網(wǎng)建設(shè)/小程序開(kāi)發(fā)/軟件開(kāi)發(fā)

知識(shí)

不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X(jué)表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!

您當(dāng)前位置>首頁(yè) » 新聞資訊 » 公眾號(hào)相關(guān) >

spring boot實(shí)現(xiàn)微信公眾號(hào)授權(quán)登錄

發(fā)表時(shí)間:2020-10-22

發(fā)布人:葵宇科技

瀏覽次數(shù):111

1.在配置文件中配置微信公眾號(hào)的appid和secret如下圖

2.controller中實(shí)現(xiàn)微信授權(quán)

即后臺(tái)根據(jù)參數(shù)拼接url返回給前端,前端通過(guò)請(qǐng)求后臺(tái)返回的url實(shí)現(xiàn)授權(quán),實(shí)現(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)成功后會(huì)回調(diào)授權(quán)回調(diào)接口獲取授權(quán)用戶的openid和用戶信息,實(shí)現(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ù)庫(kù)查詢此微信是否注冊(cè)過(guò)
        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)接口中,如果返回的用戶是空的,前端會(huì)走用戶注冊(cè)接口,如果用戶不為空,會(huì)走登錄接口。

在上述代碼中用到的工具類如下:

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;
   }
}

另外,如果在本地測(cè)試的話,需要對(duì)本地IP做一個(gè)內(nèi)網(wǎng)穿透,我在測(cè)試本地時(shí),用的NATAPP做的域名映射本地,但是這個(gè)不是很好用,如果大家有好的建議,歡迎提出。

相關(guān)案例查看更多