步骤:
1、 使用第三方工具,生成内网映射
2、 微信公众平台使用测试账号
3、 测试账号中需要配置自己的域名,并关注该临时测试使用的公众号
4、 下载一个浏览器的二维码插件
4、 编码测试
使用花生壳进行内网穿透
注意,因为我使用的是8080端口,所以映射成内网端口设置为8080才有效微信公众号使用测试账号以及配置
首先进入微信公众号官网,登陆后的页面下拉,左下角即有开发者工具
选择测试账号
进入后即可看到提供测试用的appID和appsecret,这在后续会用到
再关注这个公众号
并且需要在网页账号这儿配置自己的域名
点击修改
最后下载个二维码插件,以便在微信手机端登录
- 编码:java
1 第一步:用户同意授权,获取code
2 第二步:通过code换取网页授权access_token
3 第三步:刷新access_token(如果需要)
4 第四步:拉取用户信息(需scope为 snsapi_userinfo)
5 附:检验授权凭证(access_token)是否有效
具体信息见官网
maven工程,pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>login</groupId>
<artifactId>wechat</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>wechat Maven Webapp</name>
<url>http://maven.apache.org</url>
<profiles>
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<scope>provided</scope>
<version>3.0-alpha-1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- 没用到 -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160810</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.4</version>
</dependency>
</dependencies>
<build>
<finalName>wechat</finalName>
</build>
</project>
- 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
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>servlet.LoginServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>CallBackServlet</servlet-name>
<servlet-class>servlet.CallBackServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/wxLogin</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>CallBackServlet</servlet-name>
<url-pattern>/wx/callBack</url-pattern>
</servlet-mapping>
</web-app>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
AuthUtil.java发起http请求的工具类
若不熟悉httpClient相关知识,可以参考这篇文章:
HttpClient服务器模拟浏览器发送请求
package Utils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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 final String APPID="wx893db98985206df6";
public static final String APPSECRET="a7681bcfbd70c0beb497715bfa1fb35a";
public static JSONObject doGetJson(String url) throws IOException {
JSONObject jsonObject=null;
DefaultHttpClient defaultHttpClient=new DefaultHttpClient();
HttpGet httpGet=new HttpGet(url);
HttpResponse httpResponse = defaultHttpClient.execute(httpGet);
HttpEntity httpEntity=httpResponse.getEntity();
if(httpEntity!=null){
String result= EntityUtils.toString(httpEntity,"UTF-8");
jsonObject=new JSONObject(result);
//System.out.println("jsonObject: "+jsonObject);
}
httpGet.releaseConnection();
return jsonObject;
}
}
- 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
LoginServlet登录请求处理
package servlet;
import Utils.AuthUtil;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
/**
* Created by asus on 2017/8/2.
*/
public class LoginServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String backUrl="http://3261045722cwy.vicp.io/wx/callBack";
/**
*这儿一定要注意!!首尾不能有多的空格(因为直接复制往往会多出空格),其次就是参数的顺序不能变动
**/
String url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + AuthUtil.APPID+
"&redirect_uri=" + URLEncoder.encode(backUrl,"UTF-8")+
"&response_type=code" +
"&scope=snsapi_userinfo" +
"&state=STATE#wechat_redirect";
resp.sendRedirect(url);
}
}
- 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
CallBackServlet处理回调请求
package servlet;
import Utils.AuthUtil;
import org.json.JSONObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Created by asus on 2017/8/2.
*/
public class CallBackServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("hahah");
String code=req.getParameter("code");
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);
String openid=jsonObject.getString("openid");
String token=jsonObject.getString("access_token");
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);
}
}