微信授权登录:移动端[unionid](一)

微信授权登录:移动端[unionid](一)如果你有多端登录统一用户,或者是同一产品下不同子产品之间统一用户的需求的话,请提前在微信开放平台打通微信网页授权并绑定,不然的话后期打通用户处理起来比较麻烦。因为同一微信用户不同公众号生成的openid不一样,这样没办法统一用户,在微信开放平台绑定后,会有一个会有unionid,这个unionid是唯一的,这就可以统一用户,当然要花300RMB,没有这种需求的话,可以不用管。一,准备工…

大家好,欢迎来到IT知识分享网。

专栏简介

💒个人主页

📄本栏目录

                          📖心灵鸡汤📖

生活中其实没有绝境,绝境在于你自己的心没有打开。

          ✍相关博文✍

微信分享开发:准备工作
微信PC端扫码登录


如果你有多端登录统一用户,或者是同一产品下不同子产品之间统一用户的需求的话,请提前在微信开放平台打通微信网页授权并绑定,不然的话后期打通用户处理起来比较麻烦。因为同一微信用户不同公众号生成的openid不一样,这样没办法统一用户,在微信开放平台绑定后,会有一个会有unionid,这个unionid是唯一的,这就可以统一用户,当然要花300RMB ,没有这种需求的话,可以不用管。

一,准备工作

①,微信公众平台申请配置,详情见微信分享开发:准备工作[微信公众平台以及微信中控服务配置](一)

②,微信公众平台已经给了详细的授权登录流程接口,我们只需要根据其提供的文档按照步骤实现即可,下面我将根据微信公众平台提供的文档结合项目进行实现。微信公众平台

微信授权登录:移动端[unionid](一)

二,开发实现预热

①,获取code

https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect

备注:redirect_uri需要进行urlEncode编码,scope使用的是snsapi_userinfo,下面提供一个范例

https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx010f4709f3abffe3&redirect_uri=http%3A%2F%2Fdevelope.sh01net.com%2Fauth%2FloginByWeixin&response_type=code&scope=snsapi_userinfo#wechat_redirect

唤起微信授权页面

微信授权登录:移动端[unionid](一)

确认登录后会换取到一个code值,并回调到redirect_uri配置的方法函数上并带着code值。

②,通过code换取access_token和openid,然后通过openid换取用户信息并保存。

③,简单的流程图

https://blog.csdn.net/qq_27185879/article/details/86598590

登录成功页面自由跳转实现方式:实现进入他人分享的指定页面:[带登录权限机制]

三,代码实现(精简了一下,仅供参考,不一定适用你的业务需求,但业务流程都一样的)

①,Controller层(微信回调地址)

    @RequestMapping(value = "/loginByWeixin",method = {RequestMethod.GET,RequestMethod.POST})
    public String LoginByWeixin(HttpServletResponse response, String code) throws IOException{
        System.out.print("======code======"+code);
        if(code==null || "".equals(code.trim())){
            response.sendRedirect("http://develope.sh01net.com/xc-ui-pc-static-portal/index.html#/login");//我这里跳转的是页面,也可以跳转相应方法
            return null;
        }
        try {
            String token;//weixin openId
            String accessToken;//weixin accessToken
			String unionid;
			String APPID="";//在微信公众平台申请的appid
			String APP_SECRET="";//在微信公众平台申请的APPSECRET
			WeixinOauth2Token weixinOauth2Token =AdvancedUtil.getOauth2AccessToken(APPID, APP_SECRET, code);
            // weixinOauth2Token 微信请求可能会报错。报错后为null,避免循环
            if(weixinOauth2Token==null || weixinOauth2Token.getOpenId()==null){
                response.sendRedirect("http://develope.sh01net.com/xc-ui-pc-static-portal/index.html#/login");
                return null;
            }
            token = weixinOauth2Token.getOpenId();
            accessToken = weixinOauth2Token.getAccessToken();
            //获取微信用户信息
			SNSUserInfo snsUserInfo =  AdvancedUtil.getSNSUserInfo(accessToken, token);
            //查询当前微信是否登录过,你可以根据openid查询或者Unionid查询,具体根据什么查询根据自己的实际情况
			unionid=snsUserInfo.getUnionid();
			XcUser xcUser =authService.getUserByUnionid(unionid);//替换成自己的查询方法
            String access_token=null;

            if(xcUser == null ){
            
                //存储用户信息到数据库。比较简单,具体代码省略...
            }
                 response.sendRedirect("http://develope.sh01net.com/xc-ui-pc-static-portal/index.html#/search");//这里跳转授权登录成功的页面或方法
                 return null;
        } catch (IOException e) {
            e.printStackTrace();
            logger.error(e);
            response.sendRedirect("http://develope.sh01net.com/xc-ui-pc-static-portal/index.html");
            return null;
        }
    }

②WeixinOauth2Token.java

/**
 * @author lvyq
 *网页授权信息
 */
public class WeixinOauth2Token {
    /**网页授权接口调用凭证  */
    private String accessToken;
    /**凭证有效时长  */
    private int expiresIn;
    /**用于刷新凭证  */
    private String refreshToken;
    /**用户标识  */
    private String openId;
    /**用户授权作用域  */
    private String scope;
	/** 
	 * 返回 网页授权接口调用凭证
	 *  
	 * @return 网页授权接口调用凭证 
	 */
	public String getAccessToken() {
		return accessToken;
	}
	/** 
	 * 设置 网页授权接口调用凭证
	 *  
	 * @param accessToken 
	 *            网页授权接口调用凭证 
	 */
	
	public void setAccessToken(String accessToken) {
		this.accessToken = accessToken;
	}
	/** 
	 * 返回 凭证有效时长
	 *  
	 * @return 凭证有效时长 
	 */
	public int getExpiresIn() {
		return expiresIn;
	}
	/** 
	 * 设置 凭证有效时长
	 *  
	 * @param expiresIn 
	 *            凭证有效时长 
	 */
	
	public void setExpiresIn(int expiresIn) {
		this.expiresIn = expiresIn;
	}
	/** 
	 * 返回 用于刷新凭证
	 *  
	 * @return 用于刷新凭证 
	 */
	public String getRefreshToken() {
		return refreshToken;
	}
	/** 
	 * 设置 用于刷新凭证
	 *  
	 * @param refreshToken 
	 *            用于刷新凭证 
	 */
	
	public void setRefreshToken(String refreshToken) {
		this.refreshToken = refreshToken;
	}
	/** 
	 * 返回 用户标识
	 *  
	 * @return 用户标识 
	 */
	public String getOpenId() {
		return openId;
	}
	/** 
	 * 设置 用户标识
	 *  
	 * @param openId 
	 *            用户标识 
	 */
	
	public void setOpenId(String openId) {
		this.openId = openId;
	}
	/** 
	 * 返回 用户授权作用域
	 *  
	 * @return 用户授权作用域 
	 */
	public String getScope() {
		return scope;
	}
	/** 
	 * 设置 用户授权作用域
	 *  
	 * @param scope 
	 *            用户授权作用域 
	 */
	
	public void setScope(String scope) {
		this.scope = scope;
	}

}

③,SNSUserInfo.java

/**
 * @author lvyq
 *  通过网页授权获取的用户信息
 */
public class SNSUserInfo {
	/**用户标识  */
	private String openId;
	/**用户昵称  */
	private String nickname;
	/**性别(1是男性,2是女性,0是未知)  */
	private int sex;
	/**国家  */
	private String country;
	/**省份  */
	private String province;
	/**城市  */
	private String city;
	/**用户头像链接  */
	private String headImgUrl;
	/**用户特权信息  */
	private List<String> privilegeList;
	/**用户unionid,需要绑定开放平台才会获取到  */
	private String unionid;
	
	/** 
	 * 返回 用户标识
	 *  
	 * @return 用户标识 
	 */
	public String getOpenId() {
		return openId;
	}
	/** 
	 * 设置 用户标识
	 *  
	 * @param openId 
	 *            用户标识 
	 */
	
	public void setOpenId(String openId) {
		this.openId = openId;
	}
	/** 
	 * 返回 用户昵称
	 *  
	 * @return 用户昵称 
	 */
	public String getNickname() {
		return nickname;
	}
	/** 
	 * 设置 用户昵称
	 *  
	 * @param nickname 
	 *            用户昵称 
	 */
	
	public void setNickname(String nickname) {
		this.nickname = nickname;
	}
	/** 
	 * 返回 性别(1是男性,2是女性,0是未知)
	 *  
	 * @return 性别(1是男性,2是女性,0是未知) 
	 */
	public int getSex() {
		return sex;
	}
	/** 
	 * 设置 性别(1是男性,2是女性,0是未知)
	 *  
	 * @param sex 
	 *            性别(1是男性,2是女性,0是未知) 
	 */
	
	public void setSex(int sex) {
		this.sex = sex;
	}
	/** 
	 * 返回 国家
	 *  
	 * @return 国家 
	 */
	public String getCountry() {
		return country;
	}
	/** 
	 * 设置 国家
	 *  
	 * @param country 
	 *            国家 
	 */
	
	public void setCountry(String country) {
		this.country = country;
	}
	/** 
	 * 返回 省份
	 *  
	 * @return 省份 
	 */
	public String getProvince() {
		return province;
	}
	/** 
	 * 设置 省份
	 *  
	 * @param province 
	 *            省份 
	 */
	
	public void setProvince(String province) {
		this.province = province;
	}
	/** 
	 * 返回 城市
	 *  
	 * @return 城市 
	 */
	public String getCity() {
		return city;
	}
	/** 
	 * 设置 城市
	 *  
	 * @param city 
	 *            城市 
	 */
	
	public void setCity(String city) {
		this.city = city;
	}
	/** 
	 * 返回 用户头像链接
	 *  
	 * @return 用户头像链接 
	 */
	public String getHeadImgUrl() {
		return headImgUrl;
	}
	/** 
	 * 设置 用户头像链接
	 *  
	 * @param headImgUrl 
	 *            用户头像链接 
	 */
	
	public void setHeadImgUrl(String headImgUrl) {
		this.headImgUrl = headImgUrl;
	}
	/** 
	 * 返回 用户特权信息
	 *  
	 * @return 用户特权信息 
	 */
	public List<String> getPrivilegeList() {
		return privilegeList;
	}
	/** 
	 * 设置 用户特权信息
	 *  
	 * @param privilegeList 
	 *            用户特权信息 
	 */
	
	public void setPrivilegeList(List<String> privilegeList) {
		this.privilegeList = privilegeList;
	}

		/** 
	 * 返回 用户Unionid
	 *  
	 * @return 用户Unionid 
	 */
	public String getUnionid() {
		return unionid;
	}

	/** 
	 * 设置用户Unionid
	 *  
	 * @param 用户Unionid 
	 */
	public void setUnionid(String unionid) {
		this.unionid = unionid;
	}
}

④,AdvancedUtil .java 

/**
 * @author lvyq  获取网页授权凭证
 */
public class AdvancedUtil {
	private static final Logger logger = LogManager.getLogger(AdvancedUtil.class);
	/**
	 * 获取网页授权凭证
	 * 
	 * @param appId
	 *            公众账号的唯一标识
	 * @param appSecret
	 *            公众账号的密钥
	 * @param code
	 * @return WeixinAouth2Token
	 */
	public static WeixinOauth2Token getOauth2AccessToken(String appId,
			String appSecret, String code) {
		WeixinOauth2Token wat = null;
		// 拼接请求地址
		String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
		requestUrl = requestUrl.replace("APPID", appId);
		requestUrl = requestUrl.replace("SECRET", appSecret);
		requestUrl = requestUrl.replace("CODE", code);
		// 获取网页授权凭证
		JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null);
		logger.info("// 获取网页授权凭证access_token"+jsonObject);
		if (null != jsonObject) {
			try {
				wat = new WeixinOauth2Token();
				wat.setAccessToken(jsonObject.getString("access_token"));
				wat.setExpiresIn(jsonObject.getInt("expires_in"));
				wat.setRefreshToken(jsonObject.getString("refresh_token"));
				wat.setOpenId(jsonObject.getString("openid"));
				wat.setScope(jsonObject.getString("scope"));
			} catch (Exception e) {
				wat = null;
				int errorCode = jsonObject.getInt("errcode");
				String errorMsg = jsonObject.getString("errmsg");
				logger.info("获取网页授权凭证失败 errcode:{} errmsg:{}" + errorCode + errorMsg);
				e.printStackTrace();
			}
		}

		return wat;
	}
	
	
	/**
	 * 通过网页授权获取用户信息
	 * 
	 * @param accessToken
	 * 网页授权接口调用凭证
	 * @param openId
	 *  用户标识
	 * @return SNSUserInfo
	 */
	@SuppressWarnings({ "deprecation", "unchecked" })
	public static SNSUserInfo getSNSUserInfo(String accessToken, String openId) {
		SNSUserInfo snsUserInfo = null;
		// 拼接请求地址
		String requestUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openId=OPENID&lang=zh_CN";
		requestUrl = requestUrl.replace("ACCESS_TOKEN", accessToken).replace(
				"OPENID", openId);
		// 通过网页授权获取用户信息
		JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null);
		logger.info("// 通过网页授权获取用户信息"+jsonObject);
		if (null != jsonObject) {
			try {
				snsUserInfo = new SNSUserInfo();
				// 用户的标识
				snsUserInfo.setOpenId(openId);
				// 昵称
				snsUserInfo.setNickname(jsonObject.getString("nickname"));
				// 性别(1是男性,2是女性,0是未知)
				snsUserInfo.setSex(jsonObject.getInt("sex"));
				// 用户所在国家
				snsUserInfo.setCountry(jsonObject.getString("country"));
				// 用户所在省份
				snsUserInfo.setProvince(jsonObject.getString("province"));
				// 用户所在城市
				snsUserInfo.setCity(jsonObject.getString("city"));
				// 用户头像
				snsUserInfo.setHeadImgUrl(jsonObject.getString("headimgurl"));
				// 用户特权信息
				snsUserInfo.setPrivilegeList(JSONArray.toList(
						jsonObject.getJSONArray("privilege"), List.class));
				//获取unionid
				snsUserInfo.setUnionid(jsonObject.getString("unionid"));
			} catch (Exception e) {
				int errorCode = jsonObject.getInt("errcode");
				String errorMsg = jsonObject.getString("errmsg");
				logger.info("获取用户信息失败 errcode:{} errmsg:{}" + errorCode + errorMsg);
				e.printStackTrace();
			}
		}

		return snsUserInfo;
	}
	

⑤,CommonUtil.java

	/**
	 * 发送https请求
	 * 
	 * @param requestUrl
	 *            请求地址
	 * @param requestMethod
	 *            请求方式(GET、POST)
	 * @param outputStr
	 *            提交的数据
	 * @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值)
	 */
	/**
     * 发送https请求
     * 
     * @param requestUrl 请求地址
     * @param requestMethod 请求方式(GET、POST)
     * @param outputStr 提交的数据
     * @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值)
     */
    public static JSONObject httpsRequest(String requestUrl, String requestMethod, String outputStr) {
        JSONObject jsonObject = null;
        try {
            // 创建SSLContext对象,并使用我们指定的信任管理器初始化
            TrustManager[] tm = { new MyX509TrustManager() };
            SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
            sslContext.init(null, tm, new java.security.SecureRandom());
            // 从上述SSLContext对象中得到SSLSocketFactory对象
            SSLSocketFactory ssf = sslContext.getSocketFactory();

            URL url = new URL(requestUrl);
            HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
            conn.setSSLSocketFactory(ssf);
            
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            // 设置请求方式(GET/POST)
            conn.setRequestMethod(requestMethod);

            // 当outputStr不为null时向输出流写数据
            if (null != outputStr) {
                OutputStream outputStream = conn.getOutputStream();
                // 注意编码格式
                outputStream.write(outputStr.getBytes("UTF-8"));
                outputStream.close();
            }

            // 从输入流读取返回内容
            InputStream inputStream = conn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String str = null;
            StringBuffer buffer = new StringBuffer();
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }

            // 释放资源
            bufferedReader.close();
            inputStreamReader.close();
            inputStream.close();
            inputStream = null;
            conn.disconnect();
            jsonObject = JSONObject.fromObject(buffer.toString());
        } catch (ConnectException ce) {
        	 
            ce.printStackTrace();
        } catch (Exception e) {
        	 
            e.printStackTrace();
        }
        return jsonObject;
    }

⑥MyX509TrustManager.java(发送https请求绕过认证,注意引入的包)

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

/**
 * @author lvyq 证书信任管理器
 */
public class MyX509TrustManager implements X509TrustManager {

	// 检查客户端证书
	public void checkClientTrusted(X509Certificate[] chain, String authType)
			throws CertificateException {
	}

	// 检查服务器端证书
	public void checkServerTrusted(X509Certificate[] chain, String authType)
			throws CertificateException {
	}

	// 返回受信任的X509证书数组
	public X509Certificate[] getAcceptedIssuers() {
		return null;
	}
}

⑦,至此,已经实现了微信授权登录的业务。

微信授权登录:PC端扫码登录[unionid](二)

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/20494.html

(0)

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信