package com.wx.auth.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;
/**
* @author 龚道松
* @date 2019年3月18日上午9:30:42
* @version 1.0
*/
public class AuthUtil {
public static final String APPID="wxde2875504f4d6a8b";
public static final String APPSECRET="1ca641d3e32a44eac460615c3a072844";
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 = jsonObject.fromObject(result);
}
httpGet.releaseConnection();
return jsonObject;
}
}
package com.wx.auth.util.servlet;
import java.io.IOException;
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.wx.auth.util.AuthUtil;
import net.sf.json.JSONObject;
/**
* @author 龚道松
* @date 2019年3月18日上午10:43:00
* @version 1.0
*/
// 回调
@WebServlet("callBack")
public class CallbackServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
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);
}
}
package com.wx.auth.util.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.wx.auth.util.AuthUtil;
/**
* @author 龚道松
* @date 2019年3月18日上午10:14:50
* @version 1.0
*/
// 登录
@WebServlet("/wxLogin")
public class LoginServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String backurl = "http://3qgj7u.natappfree.cc/WxAuth/callBack";
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";
resp.sendRedirect(url);
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta 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">微信公众号授权登录</a>
</body>
</html>
成功!!!
继续下面的操作
<?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" version="3.0">
<display-name>WxAuth</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>wxcallBack</servlet-name>
<servlet-class>com.wx.auth.servlet.CallbackServlet</servlet-class>
<init-param>
<param-name>dburl</param-name>
<param-value>jdbc:mysql://127.0.0.1:3306/auth</param-value>
</init-param>
<init-param>
<param-name>driverName</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>