微信授權登錄(微信訂閱號使用測試賬號)


1.微信授權登錄;
微信公眾號測試登錄:
准備:
1.1 花生殼! 下載地址:http://hsk.oray.com/download/

1.2 微信公眾號:https://mp.weixin.qq.com/ 服務號申請起來太麻煩,我之前申請了一個訂閱號,正好用來做測試

這個是測試賬號的appid和appsecret

使用前提,設置【網頁授權獲取用戶基本信息】中的【授權回調頁面域名:】

所需的jar包:

開始步驟及准備:
1.AuthUtil.java

package com.wanglixia;

import net.sf.json.JSONObject;
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 java.io.IOException;

/**
 * 功能描述:
 * <p>
 * Created by Mr.wang on 2017/9/17 11:57.
 */
public class AuthUtil {

    public static final String APPID = "這塊填appid";
    public static final String APPSECRET = "這塊是appsecret";

    public static JSONObject doGetJson(String url) throws IOException {
        JSONObject jsonObject = null;
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        HttpResponse httpResponse = client.execute(httpGet);
        HttpEntity entity = httpResponse.getEntity();
        if (entity != null) {
            String result = EntityUtils.toString(entity, "UTF-8");
            jsonObject = JSONObject.fromObject(result);
        }
        httpGet.releaseConnection();
        return jsonObject;
    }
}

2.CallBackServlet.java

package com.wanglixia;

import net.sf.json.JSONObject;

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 java.io.IOException;

/**
 * 功能描述:
 * <p>
 * Created by Mr.wang on 2017/9/17 13:06.
 */
@WebServlet("/callBack")
public class CallBackServlet extends HttpServlet {
    protected void service(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);
        System.out.println(jsonObject.toString());
        String openid = jsonObject.getString("openid");
        String token = jsonObject.getString("access_token");
//        String expires_in = jsonObject.getString("expires_in");
//        String refresh_token = jsonObject.getString("refresh_token");
//        String scope = jsonObject.getString("scope");
        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、使用微信用戶信息直接登錄,無需注冊和綁定
//        req.setAttribute("info", userInfo);
//        req.getRequestDispatcher("/index1.jsp").forward(req, resp);


    }
}

3.WxLogin.java

package com.wanglixia;

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 java.io.IOException;
import java.net.URLEncoder;

/**
 * 功能描述:
 * <p>
 * Created by Mr.wang on 2017/9/17 11:17.
 */
@WebServlet("/wxLogin")
public class WxLogin extends HttpServlet {
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String backUrl = "http://這兒是你的回調地址,上圖填的那個/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);
    }
}

4.index.jsp

<%--
  Created by IntelliJ IDEA.
  User: Mr.wang
  Date: 2017/9/17
  Time: 11:02
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>$Title$</title>
</head>
<body style="font-size: 40px;text-align: center;">
<a href="/wxLogin">微信公眾授權登錄</a>
</body>
</html>

5.index1.jsp

<%--
  Created by IntelliJ IDEA.
  User: Mr.wang
  Date: 2017/9/17
  Time: 11:02
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>$Title$</title>
</head>
<body>
<div>登錄成功!</div>
<div>${info}</div>
<div><img style="width: 100px;height: 100px;" src="${info.headimgurl}"></div>
</body>
</html>

中間遇到的問題:

  1. 接口回調地址設置錯誤,這個地址需要是公網中能夠訪問到的地址,因此需要用花生殼來進行內網映射;
  2. 因為沒有微信公眾服務號,因此,找了半天,突然想起有個測試賬號,哈哈~~。
    參考自:慕課網教程:http://www.imooc.com/learn/713


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM