jfinal 微信授权登录

该博客参考于微信开放平台:
https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419317853&token=&lang=zh_CN

第一步,获取code,上述链接中有iOS平台和Android平台获取code的示例,暂不做描述
第二步,通过code获取access_token和openid

private static final String WEIXIN_AUTH_LOGIN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token";	// 授权登录url
private static final String XEIXIN_USERINFO_URL = "https://api.weixin.qq.com/sns/userinfo";					// 获取微信用户信息url
private static final String WEIXIN_APPID = "xxx";															// 应用唯一标识 在微信开放平台提交应用审核通过后获得
private static final String WEIXIN_SECRET = "xxx";															// 应用密钥AppSecret 在微信开放平台提交应用审核通过后获得
// 获取access_token和openid
StringBuffer sbOauth = new StringBuffer();
sbOauth.append(WEIXIN_AUTH_LOGIN_URL)
  .append("?appid=").append(WEIXIN_APPID)
  .append("&secret=").append(WEIXIN_SECRET)
  .append("&code=").append(code)
  .append("&grant_type=authorization_code");
String strLogin = HttpKit.get(sbOauth.toString());
JSONObject joLogin = null;
try {
	joLogin = new JSONObject(strLogin);
} catch (JSONException e) {
	e.printStackTrace();
}
String errcode = joLogin.optString("errcode");
if (StringUtils.isNotEmpty(errcode)) {	// 授权出错
	return null;
}
String openId = joLogin.optString("openid");
if (StringUtils.isEmpty(openId)) {
	return null;
}
String accessToken = joLogin.optString("access_token");		// 接口调用凭证
String refreshToken = joLogin.optString("refresh_token");	// 用户刷新access_token
String scope = joLogin.optString("scope");					// 用户授权的作用域
String expiresIn = joLogin.optString("expires_in");			// access_token接口调用凭证超时时间,单位秒

第三步,通过access_token和openid获取用户信息

// 获取用户信息
StringBuffer sbUser = new StringBuffer();
sbUser.append(XEIXIN_USERINFO_URL)
	  .append("?access_token=").append(accessToken)
	  .append("&openid=").append(openId);
String strUser = HttpKit.get(sbUser.toString());
JSONObject joUser = null;
try {
	joUser = new JSONObject(strUser);
} catch (JSONException e) {
	e.printStackTrace();
}
String nickName = joUser.optString("nickname");			// 用户昵称
String headImgUrl = joUser.optString("headimgurl");		// 用户头像
String unionId = joUser.optString("unionid");			// 用户统一标识
String sex = joUser.optString("sex");					// 用户性别,1为男性,2为女性

最后,生成token并处理需要的逻辑

String userName = null;
String password = null;
String mail = null;
String phone = null;
String sex = null;
String headImgUrl = wxUser.getHeadimgurl();
String nickName = wxUser.getNickName();
String openId = wxUser.getOpenId();   
User user = userService.findInfoByOpenId(wxUser.getOpenId());
if (user == null) {	// 第一次登录时添加用户
	// 保存头像
	String fileName = System.currentTimeMillis()+".jpg";
	String filePath = PropKit.get("uploadImgPath")+"/userHeadImg/";
	HttpURLConnectionUtil.urlDownload(headImgUrl, filePath, fileName);
	// 添加用户
	userService.saveUser(userName, password, mail, phone, sex, fileName, nickName, openId);
	user = userService.findInfoByOpenId(openId);
}
String newToken = TokenManager.getMe().generateToken(user);
// 处理别的逻辑

jsonObject.put("token", newToken);
jsonObject.put("user", user);
jsonObject.put("code", 1);
jsonObject.put("msg", "登录成功");
renderJson(jsonObject.toJSONString());
return;
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值