成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

慕課網(wǎng)_《微信授權(quán)登錄》學(xué)習(xí)總結(jié)

coordinate35 / 522人閱讀

摘要:時間年月日星期六說明本文部分內(nèi)容均來自慕課網(wǎng)。第六章公眾號與開發(fā)平臺關(guān)聯(lián)公眾號與開放平臺關(guān)聯(lián)情景說明當(dāng)使用端進(jìn)行微信授權(quán)登錄時,得到的和公眾號授權(quán)登錄時得到的不一樣。

時間:2017年08月12日星期六
說明:本文部分內(nèi)容均來自慕課網(wǎng)。@慕課網(wǎng):http://www.imooc.com
教學(xué)源碼:無
學(xué)習(xí)源碼:https://github.com/zccodere/s...

第一章:課程介紹 1-1 課程介紹

課程內(nèi)容

課程介紹
登錄方式介紹
基于微信公眾號授權(quán)登錄
微信開放平臺介紹
基于微信開放平臺實現(xiàn)授權(quán)登錄
微信公眾號與微信開放平臺關(guān)聯(lián)整合
第二章:登錄方式介紹 2-1 登錄方式介紹

微信登錄介紹

微信開放平臺
微信公眾號(微信公眾平臺)

手機授權(quán)登錄頁

實現(xiàn)方式

沒有自己的賬號體系,直接拉取微信用戶信息來進(jìn)行網(wǎng)站登錄。
有自己的賬號體系,授權(quán)成功后需要綁定自己的賬號。
第三章:基于公眾號的登錄 3-1 公眾號授權(quán)登錄

接口文檔

路徑:微信網(wǎng)頁開發(fā)》微信網(wǎng)頁授權(quán)
地址:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842

創(chuàng)建名為wxdevauth的maven項目,POM文件如下


    4.0.0

    com.myimooc
    wxdevauth
    0.0.1-SNAPSHOT
    jar

    wxdevauth
    http://maven.apache.org

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.1.RELEASE
         
    

    
        UTF-8
        UTF-8
    

    
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            mysql
            mysql-connector-java
        
        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
        
            com.alibaba
            fastjson
            1.2.36
        
        
        
            org.apache.httpcomponents
            httpclient
        
        
        
          commons-codec
          commons-codec
            
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    1.8
                    1.8
                
            
        
    

完成后的項目結(jié)構(gòu)如下

說明:由于條件限制,此項目代碼均沒有進(jìn)行測試,這里只是顯示大概開發(fā)過程。

代碼演示:

1.編寫User類

package com.myimooc.wxdevauth.wxauth.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

/**
 * 自有用戶賬號體系
 * @author ZhangCheng on 2017-08-12
 *
 */
@Entity
public class User {
    
    @Id
    @GeneratedValue
    private Long id;
    private String account;
    private String password;
    private String nickname;
    private String openid;
    private String unionid;
    
    @Override
    public String toString() {
        return "User [id=" + id + ", account=" + account + ", password=" + password + ", nickname=" + nickname
                + ", openid=" + openid + "]";
    }
    
    public String getUnionid() {
        return unionid;
    }

    public void setUnionid(String unionid) {
        this.unionid = unionid;
    }

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getAccount() {
        return account;
    }
    public void setAccount(String account) {
        this.account = account;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getNickname() {
        return nickname;
    }
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
    public String getOpenid() {
        return openid;
    }
    public void setOpenid(String openid) {
        this.openid = openid;
    }
    
    
    
}

2.編寫UserRepository類

package com.myimooc.wxdevauth.wxauth.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.myimooc.wxdevauth.wxauth.domain.User;

/**
 * 用戶相關(guān)資源類
 * @author ZhangCheng on 2017-08-12
 *
 */
public interface UserRepository extends JpaRepository {
    
    User findByunionid(String unionid);
    
}

3.編寫AuthUtils類

package com.myimooc.wxdevauth.wxauth.util;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;

/**
 * 使用Http進(jìn)行認(rèn)證請求
 * @author ZhangCheng on 2017-08-12
 *
 */
public class AuthUtils {
    
    public static final String APPID="dsadqawer2124a5wdqw1";
    public static final String APPSECRET = "dsadaq875w5edqwd58qwdqwbgthr4t5qa";
    private static final String CHARSET_FORMAT = "UTF-8";
    
    /**
     * 發(fā)起GET請求,并將響應(yīng)數(shù)據(jù)封裝為JSON
     */
    public static JSONObject doGetJson(String url) throws Exception{
        JSONObject jsonObject = null;
        HttpClientBuilder  builder = HttpClientBuilder.create();
        HttpGet httpGet = new HttpGet(url);
        HttpResponse response = builder.build().execute(httpGet);
        HttpEntity entity = response.getEntity();
        if(null != entity){
            String result = EntityUtils.toString(entity,CHARSET_FORMAT);
            jsonObject = JSONObject.parseObject(result);
        }
        return jsonObject;
    }
    
}

4.編寫LoginRest類

package com.myimooc.wxdevauth.wxauth.rest;

import java.net.URLEncoder;
import java.util.Objects;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.myimooc.wxdevauth.wxauth.domain.User;
import com.myimooc.wxdevauth.wxauth.repository.UserRepository;
import com.myimooc.wxdevauth.wxauth.util.AuthUtils;

/**
 * 登錄認(rèn)證REST
 * @author ZhangCheng on 2017-08-12
 *
 */
@Controller
public class LoginRest {
    
    @Autowired
    private UserRepository userRepository;
    
    @RequestMapping(value={"/","","/index"})
    public ModelAndView index(){
        return new ModelAndView("index");
    }
    
    /**
     * 第一步:用戶同意授權(quán),獲取code
     * 入口地址
     */
    @RequestMapping("wxlogin")
    public Object doLogin(HttpServletRequest req){
        // 用戶授權(quán)后微信回調(diào)地址
        String backUrl = "/callback";
        @SuppressWarnings("deprecation")
        String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+AuthUtils.APPID
                + "&redirect_uri="+URLEncoder.encode(backUrl)
                + "&response_type=code"
                + "&scope=snsapi_userinfo "
                + "&state=STATE#wechat_redirect";
        
        return "redirect:"+url;
    }
    
    /**
     * 第二步:通過code換取網(wǎng)頁授權(quán)access_token
     * 回調(diào)地址-得到code,從而去獲得access_token 和 openid
     */
    @RequestMapping("/callback")
    public ModelAndView doCallBack(HttpServletRequest req)throws Exception{
        String code = req.getParameter("code");
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+AuthUtils.APPID
                + "&secret="+AuthUtils.APPSECRET
                + "&code="+code
                + "&grant_type=authorization_code";
        JSONObject jsonObject = AuthUtils.doGetJson(url);
        String openid = jsonObject.getString("openid");
        String access_token = jsonObject.getString("access_token");
        // 第三步:刷新access_token(如果需要)
        // 此處省略
        
        // 第四步:拉取用戶信息(需scope為 snsapi_userinfo)
        String infoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token="+access_token
                + "&openid="+openid
                + "&lang=zh_CN";
        JSONObject userInfo = AuthUtils.doGetJson(infoUrl);
        System.out.println("用戶信息:"+userInfo);
        ModelAndView mv = new ModelAndView("success");
        mv.addObject("info",userInfo);
        String unionid = userInfo.getString("unionid");
        
        // 1.使用微信用戶信息直接登錄,無須注冊和綁定,直接跳轉(zhuǎn)到登錄成功界面
        //ModelAndView mv = new ModelAndView("success");
        //mv.addObject("info",userInfo);
        //return mv;
        
        // 2.將微信與當(dāng)前系統(tǒng)的賬號進(jìn)行綁定,綁定后跳轉(zhuǎn)到登錄成功界面
        User user = userRepository.findByunionid(unionid);
        if(null != user && (!Objects.equals("", user.getNickname()))){
            // 已綁定,直接跳轉(zhuǎn)綁定成功的頁面
            mv.setViewName("bindsuccess");
            mv.addObject("nickname", user.getNickname());
            return mv;
        }else{
            // 未綁定,跳轉(zhuǎn)到自己系統(tǒng)的登錄頁面
            mv.setViewName("login");
            mv.addObject("unionid", unionid);
            return mv;
        }
    }
    
    /**
     * 登錄并綁定微信賬號
     */
    @PostMapping("/bindwx")
    public Object bindwx(User user){
        userRepository.save(user);
        return "賬號綁定成功";
    }
}
第四章:微信開放平臺介紹 4-1 微信開放平臺介紹

微信開放平臺

地址:https://open.weixin.qq.com/

注冊并登錄成功后,需進(jìn)行開發(fā)者認(rèn)證

第五章:基于開放平臺登錄 5-1 開放平臺授權(quán)登錄

接口文檔

路徑:網(wǎng)站應(yīng)用》網(wǎng)站應(yīng)用微信登錄開發(fā)指南
地址:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419316505&token=&lang=zh_CN

開發(fā)方式與使用微信公眾號登錄的方式類似,僅僅可能是調(diào)用的微信接口地址不同而已,就不提供代碼演示了,源碼請到我的github地址查看。

第六章:公眾號與開發(fā)平臺關(guān)聯(lián) 6-1 公眾號與開放平臺關(guān)聯(lián)

情景說明:

當(dāng)使用pc端進(jìn)行微信授權(quán)登錄時,得到的openid和公眾號授權(quán)登錄時得到的openid不一樣。

為什么不一樣

當(dāng)我們在微信公眾號里面綁定了并且授權(quán)了應(yīng)用A的時候,會產(chǎn)生一個openid。而在開放平臺授權(quán)應(yīng)用A的時候,又會參數(shù)另外一個openid。它們兩個是完全獨立的,即微信公眾號與開放平臺之間是相互獨立的,它們之間并沒有關(guān)聯(lián)關(guān)系。

在開放平臺里面綁定公眾賬號

開放平臺與公眾賬號之間的關(guān)系是如何體現(xiàn)的

使用UnionID機制

什么是UnionID機制

比如像萬達(dá)集團(tuán),萬達(dá)影業(yè)與萬達(dá)百貨希望做到會員卡通用。
微信在這里做了一個打通機制,對于同一個企業(yè),在用戶屬性里面加了一個企業(yè)屬性(UnionID),
方便同一個企業(yè)在不同的產(chǎn)品中識別到同一個用戶。

如何識別是同一個企業(yè)

只要綁定在同一個開放平臺下所有移動應(yīng)用、網(wǎng)站應(yīng)用、微信公眾號都具備同一個UnionID

即在綁定微信賬號時,不再使用openid字段進(jìn)行綁定,使用unionid字段進(jìn)行綁定即可。

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/67599.html

相關(guān)文章

  • 課網(wǎng)_《Spring Boot 發(fā)送郵件》學(xué)習(xí)總結(jié)

    摘要:慕課網(wǎng)發(fā)送郵件學(xué)習(xí)總結(jié)時間年月日星期六說明本文部分內(nèi)容均來自慕課網(wǎng)。 慕課網(wǎng)《Spring Boot 發(fā)送郵件》學(xué)習(xí)總結(jié) 時間:2018年09月08日星期六 說明:本文部分內(nèi)容均來自慕課網(wǎng)。@慕課網(wǎng):https://www.imooc.com 教學(xué)源碼:https://github.com/ityouknow/... 學(xué)習(xí)源碼:https://github.com/zccoder...

    Meathill 評論0 收藏0
  • 課網(wǎng)_《初識Java微信公眾號開發(fā)》學(xué)習(xí)總結(jié)

    摘要:時間年月日星期五說明本文部分內(nèi)容均來自慕課網(wǎng)。本套課程介紹微信公眾號開發(fā),主要涉及公眾號介紹編輯模式介紹開發(fā)模式介紹等。慕課網(wǎng)是垂直的互聯(lián)網(wǎng)技能免費學(xué)習(xí)網(wǎng)站。 時間:2017年08月11日星期五說明:本文部分內(nèi)容均來自慕課網(wǎng)。@慕課網(wǎng):http://www.imooc.com教學(xué)源碼:https://github.com/zccodere/s...學(xué)習(xí)源碼:https://github...

    PrototypeZ 評論0 收藏0
  • 課網(wǎng)_《Java圖片驗證碼》學(xué)習(xí)總結(jié)

    時間:2017年07月09日星期日說明:本文部分內(nèi)容均來自慕課網(wǎng)。@慕課網(wǎng):http://www.imooc.com教學(xué)源碼:無學(xué)習(xí)源碼:https://github.com/zccodere/s... 第一章:概述 1-1 課程概述 主要內(nèi)容 驗證碼歷史 課程內(nèi)容 不同方案對比 設(shè)計與實現(xiàn) 總結(jié) 1-2 驗證碼歷史 驗證碼歷史 無驗證碼:垃圾騷擾 Luis von Ahn:Captcha 不斷...

    haitiancoder 評論0 收藏0
  • 課網(wǎng)_《Docker入門》學(xué)習(xí)總結(jié)

    摘要:時間年月日星期六說明本文部分內(nèi)容均來自慕課網(wǎng)。必填用于執(zhí)行命令,當(dāng)執(zhí)行完畢后,將產(chǎn)生一個新的文件層??蛇x指定此鏡像啟動時默認(rèn)執(zhí)行命令??蛇x用于指定需要暴露的網(wǎng)絡(luò)端口號??蛇x向鏡像中掛載一個卷組。 時間:2017年09月16日星期六說明:本文部分內(nèi)容均來自慕課網(wǎng)。@慕課網(wǎng):http://www.imooc.com 教學(xué)源碼:無 學(xué)習(xí)源碼:無 第一章:課程簡介 1-1 課程介紹 Docke...

    CoorChice 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<