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
    评论
要实现微信授权登录,需要以下步骤: 1. 在微信开放平台注册开发者帐号,并创建应用。 2. 在应用中配置授权回调域名。 3. 在 Java 程序中使用微信 SDK 获取授权链接。 4. 将用户导向授权链接,用户进行授权后会跳转回指定的回调地址。 5. 在回调地址处理中,使用微信 SDK 换取用户信息。 6. 使用用户信息进行登录或注册操作。 下面是一个简单的示例代码: ```java import com.github.wxpay.sdk.WXPayUtil; import com.github.wxpay.sdk.WXPay; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class WeChatAuth { private static final String APP_ID = "your_app_id"; private static final String APP_SECRET = "your_app_secret"; private static final String REDIRECT_URI = "your_redirect_uri"; public void doGet(HttpServletRequest request, HttpServletResponse response) throws Exception { String state = "state"; // 可以是任意字符串 String scope = "snsapi_userinfo"; // 授权类型,可以是 snsapi_base 或 snsapi_userinfo String redirectUrl = URLEncoder.encode(REDIRECT_URI, "UTF-8"); String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + APP_ID + "&redirect_uri=" + redirectUrl + "&response_type=code&scope=" + scope + "&state=" + state + "#wechat_redirect"; response.sendRedirect(url); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws Exception { String code = request.getParameter("code"); String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + APP_ID + "
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值