微信第三方登录

/**
	 * 第三方回调
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/authRequst")
	public void authRequst(HttpServletRequest request, HttpServletResponse response) throws Exception {
		StringBuilder param = new StringBuilder();
		//项目的根路径
		String basePath =request.getContextPath();
		String url = "http://" + request.getServerName()+basePath + Constant.URL_OAUTH2_CALLBACK;
		String service = Constant.USER_WECHAT_OPEN_CALLBACK_URL;

		/*
		 * 获取微信配置
		 */
		WeChatConfig weChatConfig = weChatConfigService.getWeChatConfig();

		param.append(Constant.GET_CODE_URL);
		param.append("?appid=");
		param.append(weChatConfig.getOpenAppId());
		param.append("&redirect_uri=");
		param.append(HttpUtils.urlEncode(url + "/callback?service=" + service));
		param.append("&response_type=code");
		param.append("&scope=snsapi_login");
		param.append("#wechat_redirect");
		response.sendRedirect(param.toString());
	}
    // 微信第三方登录回调地址
    public static final String USER_WECHAT_OPEN_CALLBACK_URL = "/api/open/user/wxQRLoginCallback";

    // 微信第三方使用网站应用授权登录
    public static final String GET_CODE_URL = "https://open.weixin.qq.com/connect/qrconnect";

    // 微信获取用户基本信息
    public static final String GET_USER_INFO = "https://api.weixin.qq.com/sns/userinfo";
/**
	 * 服务器认证
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/callback")
	public void callback(HttpServletRequest request, HttpServletResponse response) throws Exception {
		if (SiteContext.getCurrentSite() != null) {
			/*
			 * 获取微信配置
			 */
			WeChatConfig weChatConfig = weChatConfigService.getWeChatConfig();
			String code = request.getParameter("code");
			StringBuilder param = new StringBuilder();
			String uid = null, result = null, nickname = null, access_token = null, photo = null, url = null, sex = null;
			String unionid = null;
			String redirectUrl = null;
			redirectUrl =  request.getParameter("service");

			url = Constant.GET_OPENID_URL;
			NameValuePair[] nvp = new NameValuePair[]{new NameValuePair("grant_type", "authorization_code"), new NameValuePair("appid", weChatConfig.getOpenAppId()), new NameValuePair("secret", weChatConfig.getOpenSecret()), new NameValuePair("code", code)};
			result = HttpUtils.executeGet(url, nvp);
			System.out.println(result);
			JSONObject json = JSONObject.fromObject(result);

			if (json.get("errcode") != null) {
				System.out.println("获取accessToken失败");
				response.sendRedirect("/error");
			}

			uid = (String) json.get("openid");
			unionid = (String) json.get("unionid");

			/*
			 * 获取openId,放在session之后,用户登录验证的时候就会获取
			 */
			SessionContext.set(Constant.SESSION_KEY_OPENID, uid);
			SessionContext.set(Constant.SESSION_KEY_UNIONID, unionid);
			SessionContext.set(Constant.KEY_SESSION_GETUSER_URL, Constant.GET_USER_INFO);

			access_token = (String) json.get("access_token");

			Cache cache = cacheService.getCache(Constant.KEY_CACHE_WECHAT_TOKEN);
			cache.put(weChatConfig.getAppId(), access_token);

			WeChatUserInfo userInfo = weChatService.getUserInfo();

			try {
				nickname = userInfo.getNickname();
				photo = userInfo.getHeadimgurl();
				sex = userInfo.getSex();
			}catch (Exception e){
				System.out.println("json===="+json);
				e.printStackTrace();
				throw e;
			}

			if (StringUtils.isNotBlank(redirectUrl)) {
				redirectUrl += "?nickName=" + HttpUtils.urlEncode(HttpUtils.urlEncode(nickname));
				redirectUrl += "&photo=" + photo;
				redirectUrl += "&gender=" + sex;  // 1为男性,2为女性
				System.out.println("redirectUrl====" + redirectUrl);
				response.sendRedirect(HttpUtils.urlDecode(redirectUrl));
			}else{
				response.sendRedirect("/error");
			}

		} else {
			response.sendRedirect("/error");
		}
	}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值