app使用微信登录java

微信登录是个oauth2协议,app点击微信授权,弹出页面,用户点击授权后,app获得code,携带code访问服务端,服务端用code、appid(申请)、秘钥 获取用户openid和accesstoken,如果需要更多用户信息,用accesstoken调用微信服务端接口。

直接上代码:

登录接口:

    /**
     * @Description: 微信登录,后端通过code 进行OAuth2登录获取微信信息
     * @Param:
     * @Return:
     * @Author: Pang
     * @Date: 2019/12/21
     */
    @PostMapping("/login/wx")
    public WebApiResult<LoginResult> wxLogin(@RequestBody UserLoginWX userLoginWX) {
        LoginResult loginResult = wxComposition.wxLogin(userLoginWX);
        return WebApiResult.success(loginResult,LoginResult.class);
    }
public class UserLoginWX {

    @NotNull(message = "微信code不能为空")
    private String code;

    @NotNull(message = "设备id不能为空")
    private String clientId;

    @NotNull(message = "登录来源不能为空")
    private Integer loginChannel;
    public LoginResult wxLogin(UserLoginWX userLoginWX) {
        
        Map tokenMap = getTokenOpenId(userLoginWX.getCode());
        if (tokenMap==null){
            throw new BusinessException("微信登录失败");
        }
        String openId = (String) tokenMap.get("openid");
        String accessToken = (String) tokenMap.get("access_token");

      Map wxUserInfo = getWXInfo(accessToken, openId);

        return null;
        
}
    /**
     * @Description: 微信登录根据code获取微信accesstoken和openid
     * @Param:
     * @Return:
     * @Author: PangTiemin
     * @Date: 2019/12/20
     */
    private Map getTokenOpenId(String code) {
        String getTokenUrl = MessageFormat.format(WxAPIHttpUrl.GET_OPENID_URL.getApiUrl(), appId, secret, code);
        String tokenResponse = null;
        Map map = null;
        try {
            tokenResponse = httpClientHelper.executeHttpGet(getTokenUrl);
            map = JSON.parseObject(tokenResponse, Map.class);
            String openId = (String) map.get("openid");
            String access_token = (String) map.get("access_token");
            logger.info("get openid:url={},result={},openId={}", getTokenUrl, tokenResponse, openId);
            return map;
        } catch (IOException e) {
            logger.error("get openid exception", e);
        } finally {
            return map;
        }
    }
    /**
     * @Description: 获取微信用户信息
     * @Param:
     * @Return:
     * @Author: PangTiemin
     * @Date: 2019/12/20
     */
    private Map getWXInfo(String access_token, String openId) {
        String getUserInfourl = MessageFormat.format(WxAPIHttpUrl.GET_USERINFO_URL.getApiUrl(), access_token, openId);
        String userInforesponse = null;
        Map map = new HashMap();
        try {
            userInforesponse = httpClientHelper.executeHttpGet(getUserInfourl);
            logger.info("get wx userinfo:" + userInforesponse);
            JSONObject obj = JSON.parseObject(userInforesponse);
            obj.getString("nickname");
            obj.getString("headimgurl");
        } catch (IOException e) {
            logger.error("get wxUserInfo exception", e);
        }
        return map;
    }
/**
 * 微信API的HTTP地址 
 * @author Herry
 */
public enum WxAPIHttpUrl {
	
	/**
	 * 获取token接口url
	 * <p>参数依次为:appid,secret
	 */
	GET_TOKEN_URL("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", "获取token接口url"),

	/**
	 * 拉微信用户信息接口url
	 * <p>参数依次为:access_token,openid
	 */
	GET_USERINFO_URL("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}", "拉微信用户信息接口url"),

	/**
	 * 根据code获取用户openid的接口url
	 * <p>参数依次为:appid,secret,code
	 * <p>参数依次为:appid,secret,code
	 */
	GET_OPENID_URL("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", "根据code获取用户openid的接口url");

	private String apiUrl;
	
	private String description;
	
	private WxAPIHttpUrl(String url, String desc) {
		this.apiUrl = url;
		this.description = desc;
	}

	public String getApiUrl() {
		return apiUrl;
	}

	public void setApiUrl(String apiUrl) {
		this.apiUrl = apiUrl;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	
	
	
}

httpclient工具类

package cn.susoncloud.wisdomclass.usercenter.infrastructure.util;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.SSLContext;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

/**
 * http请求工具类
 * @author Herry
 */
public class HttpClientHelper {

	private static HttpClientHelper instance = null;
	
	public static HttpClientHelper getInstance() {
		if(null == instance) {
			synchronized (HttpClientHelper.class) {
				if(null == instance) {
					instance = new HttpClientHelper();
				}
			}
		}
		return instance;
	}
	
	private HttpClient webClient;
	
	private Logger log = LoggerFactory.getLogger(HttpClientHelper.class);
	
	private HttpClientHelper() {
		// 初始化webclient
		this.initWebClient();
	}

	/**
	 * @desc 初始化创建 WebClient
	 */
	private void initWebClient() {
		log.info("initWebClient start....");
		try {
			PoolingClientConnectionManager tcm = new PoolingClientConnectionManager();
			tcm.setMaxTotal(10);
			SSLContext ctx = SSLContext.getInstance("TLS");
			X509TrustManager tm = new X509TrustManager() {

				public void checkClientTrusted(X509Certificate[] arg0,
						String arg1) throws CertificateException {

				}

				public void checkServerTrusted(X509Certificate[] arg0,
						String arg1) throws CertificateException {

				}

				public X509Certificate[] getAcceptedIssuers() {
					return null;
				}
			};
			ctx.init(null, new X509TrustManager[] { tm }, null);
			SSLSocketFactory ssf = new SSLSocketFactory(ctx,
					SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
			Scheme sch = new Scheme("https", 443, ssf);
			tcm.getSchemeRegistry().register(sch);
			webClient = new DefaultHttpClient(tcm);
		} catch (Exception ex) {
			log.error("initWebClient exception", ex);
		} finally {
			log.info("initWebClient end....");
		}
	}
	
	/**
	 * @desc 发起HTTP GET请求返回数据
	 * @param url
	 * @return
	 * @throws IOException
	 * @throws ClientProtocolException
	 */
	public String executeHttpGet(String url) throws IOException,
            ClientProtocolException {
		ResponseHandler<?> responseHandler = new BasicResponseHandler();
		log.info("executeHttpGet url is: " + url);
		if(this.webClient == null) {
			log.info("webClient is null");
		}
		String response = (String) this.webClient.execute(new HttpGet(url), responseHandler);
		log.info("return response=====start======");
		log.info(response);
		log.info(url);
		log.info("return response=====end======");
		return response;
	}
	
	public String executeHttpPost( String url, String msg) {
		try {
			log.info("executeHttpPost start.url:" + url);
			log.info("msg:" + msg);
			HttpPost post = new HttpPost(url);
			post.setHeader("Content-Type", "application/json;charset=utf-8");
			post.setHeader("Accept", "application/json;charset=utf-8");
			ResponseHandler<?> responseHandler = new BasicResponseHandler();
			StringEntity entity = new StringEntity(msg, "utf-8");
			post.setEntity(entity);
			String response = (String) this.webClient.execute(post, responseHandler);
			log.info("return response=====start======");
			log.info(response);
			log.info(url);
			log.info("return response=====end======");
			return response;

		} catch (Exception e) {
			log.error("get user info exception", e);
			return null;
		}
	}
}

猜你喜欢

转载自blog.csdn.net/u014203449/article/details/104653529