一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java實現微信網頁授權的示例代碼

Java實現微信網頁授權的示例代碼

2021-05-21 11:02敲不完的代碼 Java教程

這篇文章主要介紹了Java實現微信網頁授權的示例代碼,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

開發前的準備:

1、需要有一個公眾號(我這里用的測試號),拿到appid和appsecret;

2、進入公眾號開發者中心頁配置授權回調域名。具體位置:接口權限-網頁服務-網頁賬號-網頁授權獲取用戶基本信息-修改

注意,這里僅需填寫全域名(如www.qq.com、www.baidu.com),勿加 http:// 等協議頭及具體的地址字段;

 我們可以通過使用ngrok來虛擬一個域名映射到本地開發環境,網址https://www.ngrok.cc/,大家自己去下載學習怎么使用

Java實現微信網頁授權的示例代碼

同時還需要掃一下這個二維碼

Java實現微信網頁授權的示例代碼

授權步驟:

1、引導用戶進入授權頁面同意授權,獲取code

2、通過code換取網頁授權access_token(與基礎支持中的access_token不同)

3、通過網頁授權access_token和openid獲取用戶基本信息

Java實現微信網頁授權的示例代碼

先看一下我的項目結構:

Java實現微信網頁授權的示例代碼

web.xml相關代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="webapp_id" version="3.0">
 <display-name>wxauth</display-name>
 <welcome-file-list>
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.htm</welcome-file>
  <welcome-file>index.jsp</welcome-file>
  <welcome-file>default.html</welcome-file>
  <welcome-file>default.htm</welcome-file>
  <welcome-file>default.jsp</welcome-file>
 </welcome-file-list>
  
 <servlet>
   <servlet-name>wxcallback</servlet-name>
   <servlet-class>com.xingshang.servlet.callbackserclet</servlet-class>
   <init-param>
     <param-name>dburl</param-name>
     <param-value>jdbc:mysql://127.0.0.1:3306/wxauth</param-value>
   </init-param>
   <init-param>
     <param-name>driverclassname</param-name>
     <param-value>com.mysql.jdbc.driver</param-value>
   </init-param>
   <init-param>
     <param-name>username</param-name>
     <param-value>root</param-value>
   </init-param>
   <init-param>
     <param-name>password</param-name>
     <param-value>123456</param-value>
   </init-param>
   <load-on-startup>1</load-on-startup>
 </servlet>
 
 <servlet-mapping>
   <servlet-name>wxcallback</servlet-name>
   <url-pattern>/wxcallback</url-pattern>
 </servlet-mapping>
 
</web-app>

authutil工具類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.xingshang.util;
import java.io.ioexception;
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 net.sf.json.jsonobject;
 
public class authutil {
  
  public static final string appid = "wx45c1428e5584fcdb";
  public static final string appsecret = "98174450eb706ada330f37e646be85d5";
 
  public static jsonobject dogetjson(string url) throws clientprotocolexception, ioexception{
    jsonobject jsonobject = null;
    //首先初始化httpclient對象
    defaulthttpclient client = new defaulthttpclient();
    //通過get方式進行提交
    httpget httpget = new httpget(url);
    //通過httpclient的execute方法進行發送請求
    httpresponse response = client.execute(httpget);
    //從response里面拿自己想要的結果
    httpentity entity = response.getentity();
    if(entity != null){
      string result = entityutils.tostring(entity,"utf-8");
      jsonobject = jsonobject.fromobject(result);
    }
    //把鏈接釋放掉
    httpget.releaseconnection();
    return jsonobject;
  }
}

java實現:

1、引導用戶進入授權頁面同意授權,獲取code

這一步其實就是將需要授權的頁面url拼接到微信的認證請求接口里面,比如需要用戶在訪問頁面 時進行授權認證

其中的scope參數有兩個值:

snsapi_base:只能獲取到用戶openid。好處是靜默認證,無需用戶手動點擊認證按鈕,感覺上像是直接進入網站一樣。

snsapi_userinfo:可以獲取到openid、昵稱、頭像、所在地等信息。需要用戶手動點擊認證按鈕。

相關代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.xingshang.servlet;
 
import java.io.ioexception;
import java.net.urlencoder;
 
import javax.servlet.servletexception;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
 
import com.xingshang.util.authutil;
 
/**
 * 入口地址
 * @author administrator
 *
 */
@webservlet("/wxlogin")
public class loginservlet extends httpservlet {
 
  /**
   *
   */
  private static final long serialversionuid = 1l;
 
  @override
  protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
    
    //第一步:引導用戶進入授權頁面同意授權,獲取code
    
    //回調地址
//    string backurl = "http://suliu.free.ngrok.cc/wxauth/callback";  //第1種情況使用
    string backurl = "http://suliu.free.ngrok.cc/wxauth/wxcallback";//第2種情況使用,這里是web.xml中的路徑
    
    //授權頁面地址
    string url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+authutil.appid
        + "&redirect_uri="+urlencoder.encode(backurl)
        + "&response_type=code"
        + "&scope=snsapi_userinfo"
        + "&state=state#wechat_redirect";
    
    //重定向到授權頁面
    response.sendredirect(url);
  }
}

2、通過第一步獲取的code換取網頁授權access_token(與基礎支持中的access_token不同)

這一步需要在控制器中獲取微信回傳給我們的code,通過這個code來請求access_token,通過access_token和openid獲取用戶基本信息:

相關代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package com.xingshang.servlet;
 
import java.io.ioexception;
import java.sql.connection;
import java.sql.drivermanager;
import java.sql.preparedstatement;
import java.sql.resultset;
import java.sql.sqlexception;
 
import javax.servlet.servletconfig;
import javax.servlet.servletexception;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
 
import com.xingshang.util.authutil;
 
import net.sf.json.jsonobject;
 
/**
 * 回調地址
 * @author administrator
 *
 */
//@webservlet("/callback")
public class callbackserclet extends httpservlet {
 
  /**
   *
   */
  private static final long serialversionuid = 1l;
  
  private string dburl;
  private string driverclassname;
  private string username;
  private string password;
  
  private connection conn =null;
  private preparedstatement ps =null;
  private resultset rs = null;
  
  //初始化數據庫
  @override
  public void init(servletconfig config) throws servletexception {
    
    //加載驅動
    try {
      this.dburl = config.getinitparameter("dburl");
      this.driverclassname = config.getinitparameter("driverclassname");
      this.username = config.getinitparameter("username");
      this.password = config.getinitparameter("password");
      class.forname(driverclassname);
    } catch (classnotfoundexception e) {
      // todo auto-generated catch block
      e.printstacktrace();
    }
  }
  
  
  @override
  protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
    
    //第二步:通過code換取網頁授權access_token
    
    //從request里面獲取code參數(當微信服務器訪問回調地址的時候,會把code參數傳遞過來)
    string code = request.getparameter("code");
    
    system.out.println("code:"+code);
    
    //獲取code后,請求以下鏈接獲取access_token
    string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + authutil.appid
        + "&secret=" + authutil.appsecret
        + "&code=" + code
        + "&grant_type=authorization_code";
    
    //通過網絡請求方法來請求上面這個接口
    jsonobject jsonobject = authutil.dogetjson(url);
    
    system.out.println("==========================jsonobject"+jsonobject);
    
    //從返回的json數據中取出access_token和openid,拉取用戶信息時用
    string token = jsonobject.getstring("access_token");
    string openid = jsonobject.getstring("openid");
    
    // 第三步:刷新access_token(如果需要)
 
    // 第四步:拉取用戶信息(需scope為 snsapi_userinfo)
    string infourl ="https://api.weixin.qq.com/sns/userinfo?access_token=" + token
        + "&openid=" + openid
        + "&lang=zh_cn";
    //通過網絡請求方法來請求上面這個接口
    jsonobject userinfo = authutil.dogetjson(infourl);
    
    system.out.println(userinfo);
    
    
    //第1種情況:使用微信用戶信息直接登錄,無需注冊和綁定
//    request.setattribute("info", userinfo);
    //直接跳轉
//    request.getrequestdispatcher("/index1.jsp").forward(request, response);
    
    
    //第2種情況: 將微信與當前系統的賬號進行綁定(需將第1種情況和@webservlet("/callback")注釋掉)
    //第一步,根據當前openid查詢數據庫,看是否該賬號已經進行綁定
    try {
      string nickname = getnickname(openid);
      if(!"".equals(nickname)){
        //已綁定
        request.setattribute("nickname", nickname);
        request.getrequestdispatcher("/index2.jsp").forward(request, response);
      }else{
        //未綁定
        request.setattribute("openid", openid);
        request.getrequestdispatcher("/login.jsp").forward(request, response);
      }
    } catch (sqlexception e) {
      // todo auto-generated catch block
      e.printstacktrace();
    }
    
  }
 
  //數據庫的查詢
  public string getnickname(string openid) throws sqlexception{
    string nickname = "";
    //創建數據庫鏈接
    conn = drivermanager.getconnection(dburl, username, password);
    string sql = "select nickname from user where openid = ?";
    ps = conn.preparestatement(sql);
    ps.setstring(1, openid);
    rs = ps.executequery();
    while (rs.next()) {
      nickname = rs.getstring("nickname");
    }
    
    //關閉鏈接
    rs.close();
    ps.close();
    conn.close();
    
    return nickname;
  }
  
  //數據庫的修改(openid的綁定)
  public int updateuser(string account,string password,string openid) throws sqlexception{
    
    //創建數據庫鏈接
    conn = drivermanager.getconnection(dburl, username, password);
    string sql = "update user set openid = ? where account = ? and password = ?";
    ps = conn.preparestatement(sql);
    ps.setstring(1, openid);
    ps.setstring(2, account);
    ps.setstring(3, password);
    int temp = ps.executeupdate();
    
    //關閉鏈接
    rs.close();
    ps.close();
    conn.close();
    
    return temp;
  }
  
  //post方法,用來接受登錄請求
  @override
  protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
    
    string account = request.getparameter("account");
    string password = request.getparameter("password");
    string openid = request.getparameter("openid");
    
    try {
      int temp = updateuser(account, password, openid);
      
      if(temp > 0){
        string nickname = getnickname(openid);
        request.setattribute("nickname", nickname);
        request.getrequestdispatcher("/index2.jsp").forward(request, response);
        system.out.println("賬號綁定成功");
      }else{
        system.out.println("賬號綁定失敗");
      }
      
    } catch (sqlexception e) {
      // todo auto-generated catch block
      e.printstacktrace();
    }
    
  }
  
}

login.jsp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%@ page language="java" contenttype="text/html; charset=utf-8"
  pageencoding="utf-8"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>insert title here</title>
</head>
<body>
  <form action="/wxauth/wxcallback" method="post">
    <input type="text" name="account" />
    <input type="password" name="password" />
    <input type="hidden" name="openid" value="${openid }" />
    <input type="submit" value="提交并綁定" />
  </form>
</body>
</html>

index.jsp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<%@ page language="java" contenttype="text/html; charset=utf-8"
  pageencoding="utf-8"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>insert title here</title>
</head>
<body style="font-size: 40px; text-align: center;">
  <a href="/wxauth/wxlogin" rel="external nofollow" >微信公眾授權登錄</a>
</body>
</html>

index1.jsp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%@ page language="java" contenttype="text/html; charset=utf-8"
  pageencoding="utf-8"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>insert title here</title>
</head>
<body>
  <div>登陸成功!</div>
  <div>用戶昵稱:${info.nickname}</div>
  <div>用戶頭像:<img style="text-align: top;" width="100" src="${info.headimgurl }"></div>
</body>
</html>

index2.jsp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<%@ page language="java" contenttype="text/html; charset=utf-8"
  pageencoding="utf-8"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>insert title here</title>
</head>
<body>
  <div>登陸成功!</div>
  <div>用戶昵稱:${nickname}</div>
</body>
</html>

最后附上需要的jar包

Java實現微信網頁授權的示例代碼

到此,微信授權登錄成功,如果有運行問題請自行調試,我這邊能正常運行的

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://www.cnblogs.com/sutao/p/8727019.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲色图2| 视频高清在线观看 | 亚洲精品二三区伊人久久 | 禁忌4中文 | 久久久乱码精品亚洲日韩 | 高清欧美不卡一区二区三区 | 粗又长好猛好爽视频 | 亚洲欧美另类在线观看 | 夫妻性生活在线 | 国产永久免费爽视频在线 | 男男同志gaysxxx| 亚洲成综合人影院在院播放 | 把美女屁股眼扒开图片 | 亚洲 欧美 日本 国产 高清 | 大伊香蕉精品二区视频在线 | 天天操天天舔 | 欧美成人精品福利网站 | 日韩在线一区二区 | 水蜜桃一二二区视在线 | 久久久久国产一级毛片高清片 | 国产亚洲精品一区久久 | 国产成人99久久亚洲综合精品 | 国内视频一区二区三区 | 2019年国产不卡在线刷新 | 国产成人精品免费视频软件 | 成人黄页网站 | 精油按摩日本 | 国产高清视频网站 | 狠狠色成人综合网图片区 | 亚洲第成色999久久网站 | 成人在线视频观看 | 国产在线视频色综合 | 色综合网天天综合色中文男男 | 五月最新商场女厕所高跟嘘嘘 | 国产在线观看精品香蕉v区 国产在线观看a | 性bbwbbwbbwbbw撒尿 | 美女撒尿部位无遮挡 | 国产rpg迷雾之风冷狐破解 | 俄罗斯女同和女同xx | 青涩体验在线观看未删减 | 第一国内永久免费福利视频 |