欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jfinal 微信授权登录

程序员文章站 2024-02-18 13:35:52
...

该博客参考于微信开放平台:
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;
相关标签: 微信授权登录