1.为什么会产生oauth2.0认证
互联网开放平台接口相互调用保证接口安全(大型的互联网公司)。
不同开放平台,对接的oauth2.0协议流程相同。
学好这个微信登录,差不多都能登录,只是可能有些参数不用
2.微信测试项目
我们先做小程序登录,这里我们需要申请一下微信测试账号我这里使用SpringBoot对接的先在application.yml配置好测试参数,你们申请下来用自己的就行,哪有出来问题随时问我就行
spring:
mvc:
view:
# 页面默认前缀目录
prefix: /WEB-INF/jsp/
# 响应页面默认后缀
suffix: .jsp
appid: wx4cdae9126a8640f4
secret: 15beb10a813c4fd3a89b870ac71fdce0
redirectUri: http://127.0.0.1:80/callback
### 生成微信授权
authorizedUrl: https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
###获取code后,请求以下链接获取access_token
access_token: https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
###拉取用户信息(需scope为 snsapi_userinfo)
userinfo: https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
server:
port: 80
得配置一个工具类,把application里面参数给接收过来,
@Component
public class WeiXinUtils {
@Value("${appid}")
private String appId;
@Value("${secret}")
private String secret;
@Value("${redirectUri}")
private String redirectUri;
@Value("${authorizedUrl}")
private String authorizedUrl;
@Value("${access_token}")
private String accessToken;
@Value("${userinfo}")
private String userinfo;
/**
* 生成授权登录
*/
public String getAuthorizedUrl() {
return authorizedUrl.replace("APPID", appId).replace("REDIRECT_URI", URLEncoder.encode(redirectUri));
}
/**
* 获取code后,获取access_token
* @param code
* @return
*/
public String getAccessTokenUrl(String code) {
return accessToken.replace("APPID", appId).replace("SECRET", secret).replace("CODE", code);
}
/**
* 获取用户
* @param accessToken
* @param openId
* @return
*/
public String getUserInfo(String accessToken, String openId) {
return userinfo.replace("ACCESS_TOKEN", accessToken).replace("OPENID", openId);
}
}
先请求一下生成链接页面,请求一下,http://localhost/authorizedUrl 路径,会叫我们在微信登录,生成一个链接,我们可以把这个链接直接复制到微信上面,到这里一个基本的微信小程序登录就写好了
// 生成授权链接
@RequestMapping("/authorizedUrl")
public String authorizedUrl() {
System.out.println(weiXinUtils.getAuthorizedUrl());
return "redirect:" + weiXinUtils.getAuthorizedUrl();
}
刚想整合一下微信网页登录,发现我的账号是订阅号,完蛋,中奖了,订阅号好像都不能扫码登录,还是什么毛病,真的不知道了
那直接写微信登录后的回调地址吧
// 微信授权回调地址
@RequestMapping("/callback")
public String callback(String code, HttpServletRequest request) {
// 1.使用Code 获取 access_token
String accessTokenUrl = weiXinUtils.getAccessTokenUrl(code);
System.out.println("根据code获取用户信息"+accessTokenUrl);
JSONObject resultAccessToken = HttpClientUtils.httpGet(accessTokenUrl);
boolean containsKey = resultAccessToken.containsKey("errcode");
if (containsKey) {
request.setAttribute("errorMsg", "系统错误!");
return errorPage;
}
// 2.使用access_token获取用户信息
String accessToken = resultAccessToken.getString("access_token");
String openid = resultAccessToken.getString("openid");
// 3.拉取用户信息(需scope为 snsapi_userinfo)
String userInfoUrl = weiXinUtils.getUserInfo(accessToken, openid);
System.out.println("根据accessToken获取用户信息0"+userInfoUrl);
JSONObject userInfoResult = HttpClientUtils.httpGet(userInfoUrl);
System.out.println("userInfoResult:" + userInfoResult);
request.setAttribute("nickname", userInfoResult.getString("nickname"));
request.setAttribute("city", userInfoResult.getString("city"));
request.setAttribute("headimgurl", userInfoResult.getString("headimgurl"));
return "info";
}
这是控制台打印的信息,我们可以根据这些信息写逻辑
根据code获取用户信息https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx4cdae9126a8640f4&secret=15beb10a813c4fd3a89b870ac71fdce0&code=0112Oo0f1NeQYy0aGf1f1WAx0f12Oo0W&grant_type=authorization_code
根据accessToken获取用户信息0https://api.weixin.qq.com/sns/userinfo?access_token=23_jWejGg2FQAPDUKKfdDdzEDAFxElb3M0ytVxDEE8c_JVJ9QLIb3ZsNBllNK0xr_6Ftrk3djrARBPJoG9QgxEVpQ&openid=ojN7n1Hvilu7Rd7g_uU4myl4BAsk&lang=zh_CN
userInfoResult:{"country":"中国","province":"山东","city":"菏泽","openid":"ojN7n1Hvilu7Rd7g_uU4myl4BAsk","sex":1,"nickname":"晨小晨?","headimgurl":"http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eqCCQbRmjvWnON9HWIZvHU7vfLjXWibfIwCuokByj1lIWJy5zicvKNnMJUlb8jw1n8P9I6aE1o0sDgA/132","language":"zh_CN","privilege":[]}