1 .网站微信登录原理:
要想接入微信的登录功能,首先需要在微信开发平台进行用户的注册,同时需要认证为开发者,再创建网站应用,等待微信审批,审批过后,就可以使用相关功能。
客户端授权 - 授权码模式
授权码模式是功能最完整,流程最严密的授权模式。它的特点是通过客户端的后台服务器,与“服务提供商”的认证服务器进行互动,先看下面一张图。
2 .OAuth2.0的认证原理
OAUTH协议为用户资源的授权提供了一个安全的、开放而又简易的标准。与以往的授权方式不同之处是OAUTH的授权不会使第三方触及到用户的帐号信息(如用户名与密码),即第三方无需使用用户的用户名与密码就可以申请获得该用户资源的授权,因此OAUTH是安全的。oAuth是Open Authorization的简写,目前的版本是2.0版。
3. 代码实现
<--导入包:代码里可以访问路径-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
创建页面:login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="/wechat/login1">微信登录</a>
</body>
</html>
3.1.拉起微信二维码页面
<%--引入微信的js支持--%>
<script type="text/javascript" src="http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js"></script>
<script type="text/javascript">
var obj = new WxLogin({
self_redirect:true,
id:"login_container",
appid: "wxd853562a0548a7d0",
scope: "snsapi_login",
redirect_uri: "http://bugtracker.itsource.cn/wechat/callback",
state: "xxx",
style: "white",
href: ""
});
</script>
3.2.工具类的封装
public class HttpClientUtil {
// http://bugtracker.itsource.cn/wechat/callback?code=222&state=99
// http://bugtracker.itsource.cn/wechat/callback code=222&state=99
public static String doGet(String uri){
//1:创建一个HttpClient的实例
CloseableHttpClient httpclient = HttpClients.createDefault();
//2:创建一个get请求实例
HttpGet httpGet = new HttpGet(uri);
//请求的响应:
CloseableHttpResponse response1=null;
try {
//3:使用HttpClient的实例执行get请求
response1= httpclient.execute(httpGet);
//http请求的状态:404 500 200
System.out.println(response1.getStatusLine());
int statusCode = response1.getStatusLine().getStatusCode();
if(statusCode==200){
//请求成功:
HttpEntity entity1 = response1.getEntity();
String result = EntityUtils.toString(entity1, "utf-8");
System.out.println(result);
return result;
}else{
//请求失败:自己做自己的业务逻辑
}
}catch (Exception ex){
ex.printStackTrace();
}
return null;
}
3.3.常量封装
public class WxConstants {
public final static String APPID = "wxd853562####";//开发申请的账号
//用户授权后微信的回调域名
public final static String CALLBACK="http://bugtracker.itsource.cn";
public final static String SCOPE = "snsapi_login";
public final static String APPSECRET = "4a5d5615f93f24b#######";//开发申请的密钥
//微信上获取code的地址
public final static String CODEURL = "https://open.weixin.qq.com/connect/qrconnect?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";
//微信上获取at的地址
public final static String ACCESSTOKEURL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
//微信上获取用户信息的地址
public final static String USERINFOURL = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID";
}
3.4.核心代码
@Controller
@RequestMapping("/wechat")
public class WeChatController {
@RequestMapping("/login")
public String index(){
return "wxlogin";
}
@RequestMapping("/callback")
public String callback(String code, String state, Model model, HttpServletRequest req){
String atUrl = WeChatConstants.ACCESS_TOKEN.replace("APPID",WeChatConstants.APPID)
.replace("SECRET",WeChatConstants.SECRET)
.replace("CODE",code);
String atJsonStr = HttpClientUtil.doGet(atUrl);
System.out.println("atJsonStr:"+atJsonStr);
JSONObject jsonObject = (JSONObject)JSON.parse(atJsonStr);
String access_token = String.valueOf(jsonObject.get("access_token"));
String open_id = String.valueOf(jsonObject.get("openid"));
System.out.println("access_token:"+access_token);
System.out.println("open_id:"+open_id);
String userInfoUrl = WeChatConstants.USER_INFO.replace("ACCESS_TOKEN", access_token).replace("OPENID", open_id);
String userInfo = HttpClientUtil.doGet(userInfoUrl);
JSONObject userJson = (JSONObject)JSON.parse(userInfo);
System.out.println(userJson);
//完成绑定操作
model.addAttribute("userInfo", userInfo);
return "main";
}
}
3.5绑定操作:自己实现业务逻辑
判断该微信用户是否以及绑定过
如果没有绑定过:–弹出绑定页面,输入用户的账号进行绑定(数据库需要设计一张绑定表。用于记录用户的绑定的情况)
如果已经绑定过:–自己放行到登录页面,而且应该 通过微信用户获取绑定用户,进行权限的展示;