1. 程式人生 > >JAVA實現第三方網站微信掃碼登入Demo

JAVA實現第三方網站微信掃碼登入Demo

微信掃碼登入Demo

用JAVA Servlet實現的第三方網站微信掃碼登入的Demo,做這個確實很辛苦,很不容易實現,但是還是做出來了,這裡把程式碼貼一下,避免做微信登入開發的朋友們少走彎路

package com.wxlogin.common;

import java.net.URLEncoder;

public class ConstantManager { /* 返回狀態失敗 */ public static final String ERROR_STATUS = “999”;

/** 返回狀態成功 **/
public static final String SUCCESS_STATUS = "000";

/** 返回資訊失敗 **/
public static final String ERROR_MESSAGE = "FAILED";

/** 返回狀態等待 **/
public static final String IN_PROGRESS = "666";

/**許可權不足**/
public static final String OAUTH_STATUS = "777";

/** 返回資訊成功 **/
public static final String SUCCESS_MESSAGE = "SUCCESS";

public static final String RETURNMSG = "returnMsg";

public static final String RETURNSTATUS = "returnStatus";

public static final String RETURNDATA = "returnData";

//使用者詳細資訊
public static final String OAUTH_USER_INFO="snsapi_userinfo";

public static final String OAUTH_QQ_USER_INFO="get_user_info";


//靜默授權
public static final String OAUTH_USER_BASE="snsapi_base";

public static final String GRANT_TYPE="authorization_code";

//網頁授權作用域
public static final String OAUTH_WEB_LOGIN="snsapi_login";


public static final String PC="pc";

public static final String WAP="wap";







/*************************************************v2******************************************************/



/**
 * PC授權回撥
 */
public static final String WEBCALLBACKURL = URLEncoder.encode("http://www.sucaiku.xin/callback/wxCallback.do");

}

package com.wxlogin.common;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;

import com.wxlogin.common.ConstantManager;;

/** * * @description 微信介面呼叫地址配置 * * @author tjz * * @time 2017年2月23日 */

public class UrlConfig {

/**
 * 微信全域性呼叫accessToken
 * 
 * @return
 */
public String getGlobalAccessTokenUrl(String wxMpAppId,String wxMpAppSecrte) {
    return "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + wxMpAppId + "&secret="+ wxMpAppSecrte;
}

/**
 * 微信伺服器IP列表
 * 
 * @return
 */
public String getWeiXinIpUrl(String wxMpAppId,String wxMpAppSecrte,String appId) {
    //return "https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=" + weiXinUtil.getGlobalAccessToken(wxMpAppId, wxMpAppSecrte,appId);
    return null;
}


/**
 * 微信授權
 */
public String getOauth(String type,String redirectUrl,String param,String wxMpAppId) {
    return "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + wxMpAppId + "&redirect_uri="+ redirectUrl + "&response_type=code&scope=" + type + "&state=" + param + "#wechat_redirect";
}



/**
 * 網頁授權
 */
public String getOauthAccessToken(String code,String wxMpAppId,String wxMpAppSecret) {
    return "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + wxMpAppId + "&secret=" + wxMpAppSecret+ "&code=" + code + "&grant_type=" + ConstantManager.GRANT_TYPE;
}

/**
 * 重新整理網頁授權accessToken
 */
public String refreshAccessToken(String refreshToken,String wxMpAppId) {
    return "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" + wxMpAppId+ "&grant_type=refresh_token&refresh_token=" + refreshToken.trim();
}

/**
 * 微信使用者詳細資訊 accessToken:網頁授權accessToken openId:使用者openid 通過授權獲取
 */
public String getUserInfo(String accessToken, String openId) {
    return "https://api.weixin.qq.com/sns/userinfo?access_token=" + accessToken.trim() + "&openid=" + openId.trim()+ "&lang=zh_CN";
}

/**
 * 獲取使用者詳細資訊 通過全域性accessToken獲取
 */
public String getUserInfoByGlobalToken(String globalToken, String openId) {
    return "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + globalToken + "&openid=" + openId+ "&lang=zh_CN ";
}

/**
 * 長短連線轉換
 */
public String getShortUrl(String accessToken) {
    return "https://api.weixin.qq.com/cgi-bin/shorturl?access_token=" + accessToken;
}

/**
 * 獲取模版列表
 */
public String getModelList(String accessToken) {
    return "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=" + accessToken;
}

/**
 * 傳送模版訊息
 */
public String sendModelMessage(String accessToken) {
    return "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
}

/**
 * 獲取模版設定的行業資訊
 */
public String getModelIndustry(String accessToken) {
    return "https://api.weixin.qq.com/cgi-bin/template/get_industry?access_token=" + accessToken;
}

/**
 * 獲取jsapi簽名
 */
public String getJsApiConfig(String accessToken){
    return "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+accessToken+"&type=jsapi";
}

/**
 * 獲取關注的使用者列表
 */
public String getUserList(String accessToken,String openId){
    if(StringUtils.isBlank(openId)){
        return "https://api.weixin.qq.com/cgi-bin/user/get?access_token="+accessToken;
    }else{
        return "https://api.weixin.qq.com/cgi-bin/user/get?access_token="+accessToken+"&next_openid="+openId;
    }
}

/**
 * 建立二維碼ticket
 */
public String createQrcodeTicket(String accessToken){
    return " https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="+accessToken;
}

/**
 * 通過ticket換取二維碼
 */
public String showQrcode(String ticket){
    return "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket="+ticket;
}

/**
 * 微信web登入url
 */
public String getWxWebLoginUrl(String type,String redirectUrl,String param,String wxOpenAppId){
    return "https://open.weixin.qq.com/connect/qrconnect?appid="+wxOpenAppId+"&redirect_uri="+redirectUrl+"&response_type=code&scope="+type+"&state="+param+"#wechat_redirect";
}

/**
 * 微信卡包ticket
 */
public String getTicketUrl(String accessToken){
    return "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+accessToken+"&type=wx_card";
}

/**
 * 新增客服帳號
 * @description 
 *
 * @author tjz
 *
 * @param accessToken
 * @return
 *
 * @time 2018年2月2日 上午9:54:20
 */
public String addKfAccountUrl(String accessToken){
    return "https://api.weixin.qq.com/customservice/kfaccount/add?access_token="+accessToken;
}


/**
 * 獲取所有客服帳號
 */
public String getKfListUrl(String accessToken){
    return "https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token="+accessToken;
}

/**
 * 傳送客服訊息
 */
public String getSendMessageUrl(String accessToken){
    return "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+accessToken;
}

/**
 * 獲取QQ登入Url
 */
public String getQqLoginUrl(String appId,String callbackUrl,String state,String scope){
    return "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id="+appId+"&redirect_uri="+callbackUrl+"&state="+state+"&scope="+scope;
}




/**
 * 獲取PC版QQ登入accessToken
 */
public String getQqLoginAccessTokenByPcUrl(String qqAppId,String qqAppKey,String code,String redirectUri){
    return "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id="+qqAppId+"&client_secret="+qqAppKey+"&code="+code+"&redirect_uri="+redirectUri;
}


/**
 * 獲取WAP版QQ登入accessToken
 */
public String getQqLoginAccessTokenByWapUrl(String qqAppId,String qqAppKey,String code,String redirectUri){
    return "https://graph.z.qq.com/moc2/token?grant_type=authorization_code&client_id="+qqAppId+"&client_secret="+qqAppKey+"&code="+code+"&redirect_uri="+redirectUri;
}


/**
 * 重新整理pc授權accessToken
 */
public String refreshPcQqAccessTokenUrl(String qqAppId,String qqAppKey,String refreshToken){
    return "https://graph.qq.com/oauth2.0/token?grant_type=refresh_token&client_id="+qqAppId+"&client_secret="+qqAppKey+"&refresh_token="+refreshToken;
}

/**
 * 重新整理wap授權accessToken
 */
public String refreshWapQqAccessTokenUrl(String qqAppId,String qqAppKey,String refreshToken){
    return "https://graph.z.qq.com/moc2/token?grant_type=refresh_token&client_id="+qqAppId+"&client_secret="+qqAppKey+"&refresh_token="+refreshToken;
}

/**
 * 獲取PC qq使用者openId
 */
public String getPcQqUserInfoByAccessTokenUrl(String accessToken){
    return "https://graph.qq.com/oauth2.0/me?access_token="+accessToken;
}

/**
 * 獲取wap qq使用者openId
 */
public String getWapQqUserInfoByAccessTokenUrl(String accessToken){
    return "https://graph.z.qq.com/moc2/me?access_token="+accessToken;
}


/**
 * 獲取QQ使用者詳細資訊
 */
public String getQqUserInfoByAccessTokenAndOpenId(String appId,String accessToken,String openId){
    return "https://graph.qq.com/user/get_user_info?access_token="+accessToken+"&oauth_consumer_key="+appId+"&openid="+openId;
}

} package com.wxlogin.config;

public class WeiXinConfig {

public static String wxOpenAppId = "wx92255d9573d";

public static String wxOpenappSecret = "97d500770006b4b3exh4dh554387e6c";

} package com.wxlogin.entity;

import java.io.Serializable;

/** * accessToken 物件 * * @author Administrator * */ public class AccessToken implements Serializable {

private static final long serialVersionUID = -3443292138870183363L;

private String access_token;

private String expires_in;

private String refresh_token;

private String openid;

private String scope;

public String getAccess_token() {
    return access_token;
}

public void setAccess_token(String access_token) {
    this.access_token = access_token;
}

public String getExpires_in() {
    return expires_in;
}

public void setExpires_in(String expires_in) {
    this.expires_in = expires_in;
}

public String getRefresh_token() {
    return refresh_token;
}

public void setRefresh_token(String refresh_token) {
    this.refresh_token = refresh_token;
}

public String getOpenid() {
    return openid;
}

public void setOpenid(String openid) {
    this.openid = openid;
}

public String getScope() {
    return scope;
}

public void setScope(String scope) {
    this.scope = scope;
}

} package com.wxlogin.entity;

import java.io.Serializable; import java.util.List;

/** * * @description 微信使用者物件 * * @author tjz * * @time 2017年3月12日 */ public class UserEntity implements Serializable {

private static final long serialVersionUID = -920969954069444740L;

private Integer subscribe;// 使用者是否訂閱該公眾號標識,值為0時,代表此使用者沒有關注該公眾號,拉取不到其餘資訊。

private String openid;// 使用者的標識,對當前公眾號唯一

private String nickname;// 使用者的暱稱

private Integer sex;// 使用者的性別,值為1時是男性,值為2時是女性,值為0時是未知

private String city;// 使用者所在城市

private String country;// 使用者所在國家

private String province;// 使用者所在省份

private String language;// 使用者的語言,簡體中文為zh_CN

private String headimgurl;// 使用者頭像,最後一個數值代表正方形頭像大小(有0、46、64、96、132數值可選,0代表640*640正方形頭像),使用者沒有頭像時該項為空。若使用者更換頭像,原有頭像URL將失效。

private String subscribe_time;// 使用者關注時間,為時間戳。如果使用者曾多次關注,則取最後關注時間

private String unionid;// 只有在使用者將公眾號繫結到微信開放平臺帳號後,才會出現該欄位。

private String remark;// 公眾號運營者對粉絲的備註,公眾號運營者可在微信公眾平臺使用者管理介面對粉絲新增備註

private Integer groupid;// 使用者所在的分組ID(相容舊的使用者分組介面)

private List tagid_list;// 使用者被打上的標籤ID列表

public Integer getSubscribe() {
    return subscribe;
}

public void setSubscribe(Integer subscribe) {
    this.subscribe = subscribe;
}

public String getOpenid() {
    return openid;
}

public void setOpenid(String openid) {
    this.openid = openid;
}

public String getNickname() {
    return nickname;
}

public void setNickname(String nickname) {
    this.nickname = nickname;
}

public Integer getSex() {
    return sex;
}

public void setSex(Integer sex) {
    this.sex = sex;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

public String getCountry() {
    return country;
}

public void setCountry(String country) {
    this.country = country;
}

public String getProvince() {
    return province;
}

public void setProvince(String province) {
    this.province = province;
}

public String getLanguage() {
    return language;
}

public void setLanguage(String language) {
    this.language = language;
}

public String getHeadimgurl() {
    return headimgurl;
}

public void setHeadimgurl(String headimgurl) {
    this.headimgurl = headimgurl;
}

public String getSubscribe_time() {
    return subscribe_time;
}

public void setSubscribe_time(String subscribe_time) {
    this.subscribe_time = subscribe_time;
}

public String getUnionid() {
    return unionid;
}

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

public String getRemark() {
    return remark;
}

public void setRemark(String remark) {
    this.remark = remark;
}

public Integer getGroupid() {
    return groupid;
}

public void setGroupid(Integer groupid) {
    this.groupid = groupid;
}

public List getTagid_list() {
    return tagid_list;
}

public void setTagid_list(List tagid_list) {
    this.tagid_list = tagid_list;
}

} package com.wxlogin.servlet;

import java.io.IOException;

import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONObject; import com.wxlogin.common.UrlConfig; import com.wxlogin.config.WeiXinConfig; import com.wxlogin.entity.AccessToken; import com.wxlogin.entity.UserEntity; import com.wxlogin.util.HttpUtil;

/** * 微信PC掃碼登入回撥 * * @description * * @author tjz * * @time 2018年9月6日 下午3:48:08 */ public class CallbackServlet extends HttpServlet { private static final long serialVersionUID = 1L;

private UrlConfig urlConfig = new UrlConfig();

public CallbackServlet() {
    super();
}

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String code = request.getParameter("code");
    String state = request.getParameter("state");
    try {
        String result = HttpUtil.doGet(urlConfig.getOauthAccessToken(code, WeiXinConfig.wxOpenAppId, WeiXinConfig.wxOpenappSecret));
        JSONObject jsonObject = JSONObject.parseObject(result);
        Integer errcode = jsonObject.getInteger("errcode");
        if (errcode != null) {
            System.out.println("微信回撥失敗,授權終止!");
            return;
        }
        AccessToken accessToken = JSONObject.toJavaObject(jsonObject, AccessToken.class);
        if (accessToken == null) {
            System.out.println("微信回撥處理失敗!獲取accessToken時無返回結果!");
            return;
        }

        JSONObject returnJson = getUserInfoByAccessToken(accessToken.getAccess_token(), accessToken.getOpenid());

        System.out.println("微信使用者資訊獲取完畢*****************************************");
        System.out.println(returnJson.toJSONString());

        //這裡獲取到使用者資訊 就打印出來了  其他你自己實現

    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * 通過accessToken獲取使用者資訊
 */
public JSONObject getUserInfoByAccessToken(String accessToken, String openId) {
    System.out.println("通過accessToken獲取使用者資訊 引數:accessToken:" + accessToken + "*******引數openId:" + openId);
    JSONObject returnJson = new JSONObject();
    returnJson.put("code", 1);
    try {
        String result = HttpUtil.doGet(urlConfig.getUserInfo(accessToken, openId));
        if (StringUtils.isBlank(result)) {
            System.out.println("通過授權accessToken獲取使用者資訊失敗!" + accessToken + "openId:" + openId);
            return null;
        }
        JSONObject jsonObject = JSONObject.parseObject(result);
        Integer errcode = jsonObject.getInteger("errcode");
        if (errcode != null) {
            if ("40001".equals(errcode)) {
                System.out.println("授權過期,請重新授權!");
            } else {
                System.out.println("獲取使用者資訊失敗!" + jsonObject.toJSONString());
            }
            return null;
        }
        UserEntity userEntity = JSONObject.toJavaObject(jsonObject, UserEntity.class);
        returnJson.put("data", userEntity);
        returnJson.put("code", 0);
        returnJson.put("msg", "獲取微信使用者資訊成功");
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("通過accesstoken獲取使用者詳細資訊異常" + e.getMessage());
    }
    return returnJson;
}

} package com.wxlogin.servlet;

import java.io.IOException;

import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

import com.wxlogin.common.ConstantManager; import com.wxlogin.common.UrlConfig; import com.wxlogin.config.WeiXinConfig;

public class WxLoginServlet extends HttpServlet { private static final long serialVersionUID = 1L;

private UrlConfig urlConfig=new UrlConfig();

public WxLoginServlet() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * 發起微信登入
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String url = urlConfig.getWxWebLoginUrl(ConstantManager.OAUTH_WEB_LOGIN, ConstantManager.WEBCALLBACKURL, null,WeiXinConfig.wxOpenAppId);
    response.sendRedirect(url);

}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
}

} package com.wxlogin.util;

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.InetSocketAddress; import java.net.Proxy; import java.net.URL; import java.net.URLConnection; import java.util.Iterator; import java.util.Map;

import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.RequestEntity; import org.apache.commons.httpclient.methods.StringRequestEntity; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger;

public class HttpUtil {

private static final Logger logger = LogManager.getLogger(HttpUtil.class);

private static String charset = "utf-8";
private Integer connectTimeout = null;
private Integer socketTimeout = null;
private static String proxyHost = null;
private static Integer proxyPort = null;
private static HttpClient httpClient = null;

private static GetMethod getMethod = null;

private static PostMethod postMethod = null;

/**
 * POST請求 json
 * @param url
 * @param params
 * @return
 */
public static String doPostJson(String url, String params,boolean header) {
    try {
        HttpClient httpClient = new HttpClient();
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
        httpClient.getParams().setBooleanParameter("http.protocol.expect-continue", false);
        PostMethod postMethod = new PostMethod(url);
        if(header){
            postMethod.setRequestHeader("Content-Type","application/json");
            postMethod.setRequestHeader("Authorization","Basic YWRtaW46YWRtaW4=");
        }
        if(params != null && !params.trim().equals("")) {
            RequestEntity requestEntity = new StringRequestEntity(params,"application/json","UTF-8");
            postMethod.setRequestEntity(requestEntity);
        }
        postMethod.releaseConnection();
        httpClient.executeMethod(postMethod);
        BufferedReader reader = new BufferedReader(new InputStreamReader(postMethod.getResponseBodyAsStream()));  
        StringBuffer stringBuffer = new StringBuffer();  
        String str = "";  
        while((str = reader.readLine())!=null){  
            stringBuffer.append(str);  
        }
        logger.info("傳送POST_JSON請求");
        logger.info("URL:"+url);
        logger.info("引數:"+params);
        logger.info("響應結果:"+stringBuffer.toString());
        return stringBuffer.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

/**
 * 傳送Get請求
 * 
 * @param url
 * @return
 * @throws Exception
 */
public static String doGet(String url) throws Exception {
    URL localURL = new URL(url);
    URLConnection connection = openConnection(localURL);
    HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
    httpURLConnection.setRequestProperty("Accept-Charset", charset);
    httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    InputStream inputStream = null;
    InputStreamReader inputStreamReader = null;
    BufferedReader reader = null;
    StringBuffer resultBuffer = new StringBuffer();
    String tempLine = null;
    if (httpURLConnection.getResponseCode() >= 300) {
        throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
    }
    try {
        inputStream = httpURLConnection.getInputStream();
        inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
        reader = new BufferedReader(inputStreamReader);
        while ((tempLine = reader.readLine()) != null) {
            resultBuffer.append(tempLine);
        }
    } finally {
        if (reader != null) {
            reader.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (inputStream != null) {
            inputStream.close();
        }
    }
    logger.info("傳送GET請求");
    logger.info("URL:"+url);
    logger.info("響應結果:"+resultBuffer.toString());
    return resultBuffer.toString();
}

/**
 * 傳送Post請求
 * 
 * @param url
 * @param parameterMap
 * @return
 * @throws Exception
 */
public static String doPost(String url, Map parameterMap) throws Exception {
    StringBuffer parameterBuffer = new StringBuffer();
    if (parameterMap != null) {
        Iterator iterator = parameterMap.keySet().iterator();
        String key = null;
        String value = null;
        while (iterator.hasNext()) {
            key = (String) iterator.next();
            if (parameterMap.get(key) != null) {
                value = (String) parameterMap.get(key);
            } else {
                value = "";
            }
            parameterBuffer.append(key).append("=").append(value);
            if (iterator.hasNext()) {
                parameterBuffer.append("&");
            }
        }
    }
    URL localURL = new URL(url);
    URLConnection connection = openConnection(localURL);
    HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
    httpURLConnection.setDoOutput(true);
    httpURLConnection.setRequestMethod("POST");
    httpURLConnection.setRequestProperty("Accept-Charset", charset);
    httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    httpURLConnection.setRequestProperty("Content-Length", String.valueOf(parameterBuffer.length()));
    OutputStream outputStream = null;
    OutputStreamWriter outputStreamWriter = null;
    InputStream inputStream = null;
    InputStreamReader inputStreamReader = null;
    BufferedReader reader = null;
    StringBuffer resultBuffer = new StringBuffer();
    String tempLine = null;
    try {
        outputStream = httpURLConnection.getOutputStream();
        outputStreamWriter = new OutputStreamWriter(outputStream);
        outputStreamWriter.write(parameterBuffer.toString());
        outputStreamWriter.flush();
        if (httpURLConnection.getResponseCode() >= 300) {
            throw new Exception(
                    "HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
        }
        inputStream = httpURLConnection.getInputStream();
        inputStreamReader = new InputStreamReader(inputStream);
        reader = new BufferedReader(inputStreamReader);
        while ((tempLine = reader.readLine()) != null) {
            resultBuffer.append(tempLine);
        }
    } finally {
        if (outputStreamWriter != null) {
            outputStreamWriter.close();
        }
        if (outputStream != null) {
            outputStream.close();
        }
        if (reader != null) {
            reader.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (inputStream != null) {
            inputStream.close();
        }
    }
    logger.info("傳送POST請求");
    logger.info("URL:"+url);
    logger.info("引數:"+parameterBuffer.toString());
    logger.info("響應結果:"+resultBuffer.toString());
    return resultBuffer.toString();
}

private static URLConnection openConnection(URL localURL) throws IOException {
    URLConnection connection;
    if (proxyHost != null && proxyPort != null) {
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
        connection = localURL.openConnection(proxy);
    } else {
        connection = localURL.openConnection();
    }
    return connection;
}

/**
 * Render request according setting
 * 
 * @param request
 */
private void renderRequest(URLConnection connection) {

    if (connectTimeout != null) {
        connection.setConnectTimeout(connectTimeout);
    }

    if (socketTimeout != null) {
        connection.setReadTimeout(socketTimeout);
    }

}

/*
 * Getter & Setter
 */
public Integer getConnectTimeout() {
    return connectTimeout;
}

public void setConnectTimeout(Integer connectTimeout) {
    this.connectTimeout = connectTimeout;
}

public Integer getSocketTimeout() {
    return socketTimeout;
}

public void setSocketTimeout(Integer socketTimeout) {
    this.socketTimeout = socketTimeout;
}

public String getProxyHost() {
    return proxyHost;
}

public void setProxyHost(String proxyHost) {
    this.proxyHost = proxyHost;
}

public Integer getProxyPort() {
    return proxyPort;
}

public void setProxyPort(Integer proxyPort) {
    this.proxyPort = proxyPort;
}

public String getCharset() {
    return charset;
}

public void setCharset(String charset) {
    this.charset = charset;
}

}

微信開放平臺配置 com.wxlogin.config.WeiXinConfig com.wxlogin.common.ConstantManager 變數 WEBCALLBACKURL 為回撥地址 修改成你自己的域名 如:你的域名/callback/wxCallback.do 你的域名/doWxLogin.do 發起微信登入 本地測試的話 可以改host檔案 把域名指向本機

登入效果: 微信掃碼登入 製作過程中有問題可以加QQ76949433,提供技術支援!