前言
为了增加用户体验,用户能够快速的注册登录,第三方账号进行登录注册的的需求也就由此而诞生
开发环境:
IDE: STS
jdk版本: 1.8
框架: SSM
首先你得在 微信开放平台(https://open.weixin.qq.com/cgi-bin/index?t=home/index&lang=zh_CN) 成为开发者,并创建网站应用,
例如:
授权回调域是你项目的路径,开发用ip+端口,线上用域名
现在 开始开发:
1. 微信登录链接
<a href="https://open.weixin.qq.com/connect/qrconnect?appid=xxx&redirect_uri=http://192.168.1.254:8080/weixin_callback.jsp&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect">微信登录</a>
需要把其中的appid换成你自己创建应用的appid;
redirect_uri=http://192.168.1.254:8080/weixin_callback.jsp
redirect_uri 是手机微信扫码授权登录成功后跳转的页面
2. 处理回调参数 weixin_callback.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录成功</title>
<script type="text/javascript" src="${pageContext.request.contextPath}/ejs/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
// 获取地址栏参数
function GetQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null)
return unescape(r[2]);
return null;
}
// 调用方法
var a = GetQueryString("code");
function autoSubmit(){
document.getElementById("code").value=a;
document.getElementById("my-from").submit();
}
function geturl(){
var test = window.location.href;
alert(test);
}
</script>
</head>
<body onload="autoSubmit()">
正在加载...
<form action="/company/loginByWeixin" method="get" id="my-from">
<input type="hidden" id="code" name="codeNum"/>
</form>
</body>
</html>
3. 上一步会处理微信返回参数,然后请求后台进行处理 ,下面是处理逻辑
/**
* 微信登录
*
* @Title loginByWeixin
* @param @param
* codeNum
* @param @param
* stateNum
* @param @return
* @return String
*/
@RequestMapping("/loginByWeixin")
public String loginByWeixin(String codeNum, HttpServletRequest request, Model model) {
try {
// 获取token
String token_url = "https://api.weixin.qq.com/sns/oauth2/access_token";
String token_parm = "appid=xxx&secret=xxx&code=" + codeNum
+ "&grant_type=authorization_code";
String token_result = SendRequestUtils.sendGet(token_url, token_parm);
JSONObject jsonObject = JSONObject.fromObject(token_result);
String weixin_token = jsonObject.getString("access_token");
// 获取 weixin_openid
String weixin_openid = jsonObject.getString("openid");
// 延长token有效期,延长至30天(默认2小时)
String refresh_token_url = "https://api.weixin.qq.com/sns/oauth2/refresh_token";
String refresh_token_parm = "appid=wxfe9c4727ce3bcafc&grant_type=refresh_token&refresh_token="
+ jsonObject.getString("refresh_token");
String refresh_token_result = SendRequestUtils.sendGet(refresh_token_url, refresh_token_parm);
// 检验授权凭证(access_token)是否有效
String exist_token_url = "https://api.weixin.qq.com/sns/auth";
String exist_token_parm = "access_token=" + weixin_token + "&openid=" + weixin_openid;
String exist_token_result = SendRequestUtils.sendGet(exist_token_url, exist_token_parm);
// 获取微信用户信息
String getuserinfo_url = "https://api.weixin.qq.com/sns/userinfo";
String getuserinfo_parm = "access_token=" + weixin_token + "&openid=" + weixin_openid;
String getuserinfo_result = SendRequestUtils.sendGet(getuserinfo_url, getuserinfo_parm);
// 获取微信名
JSONObject wx_string_json = JSONObject.fromObject(getuserinfo_result);
String name = wx_string_json.getString("nickname");
// 判断是否用微信登录过网站
// xxx
// 微信登录过网站, 微信更新用户信息
// xxx
// 没有用qq登录过网站,将opendid和用户信息写入数据库
// xxx
// 跳转首页
return "redirect:/";
} catch (Exception e) {
e.printStackTrace();
return "redirect:/";
}
}
需要填入你创建的网站应用的 AppID 和 AppSecret
依赖工具类 SendRequestUtils : https://blog.csdn.net/Lxinccode/article/details/79419185
需要的一些工具类可以在JAVA网站接入QQ登录 中有写 ;
完。