✅P222_商城业务-认证服务-社交登录回调
2024/2/14大约 2 分钟
授权认证时序图
修改授权回调页
本文进行微博授权演示,同时也将Gitee授权进行测试了
微博授权
Gitee授权
http://auth.cfmall.com/oauth2.0/gitee/success
修改超链接中的回调地址
cfmall-auth-server/src/main/resources/templates/login.html
微博
<li>
<a href="https://api.weibo.com/oauth2/authorize?client_id=318019123&response_type=code&redirect_uri=http://auth.cfmall.com/oauth/weibo/success">
<img style="width: 50px;height: 18px;" src="/static/login/JD_img/weibo.png" />
<span>微博</span>
</a>
</li>
Gitee
<li>
<a href="https://gitee.com/oauth/authorize?client_id=33bc7c489583b275e20737b0a730f85a768a26aadd4f006195429d254e55911f&redirect_uri=http://auth.cfmall.com/oauth2.0/gitee/success&response_type=code">
<img style="width: 25px;height: 18px" src="/static/login/JD_img/gitee.png" />
</a>
</li>
如果测试微博登录发现重定向地址错误:
有时候浏览器中显示的地址是编码过的,不容易判断回调地址是否设置的正确无误
解决方法:将授权回调页:http://auth.cfmall.com/oauth/weibo/success 地址进行转换,URL 转换工具
将转换后的浏览器中的地址替换到回调处
社交登录接口
SocialUser:将获取Access_Token返回的json数据进行了封装,下一节会有说明;
cfmall-auth-server/src/main/java/com/gyz/cfmall/controller/OAuth2Controller.java
微博登录接口
@GetMapping("/oauth/weibo/success")
public String weibo(@RequestParam("code") String code, HttpSession session) throws Exception {
Map<String, String> headers = new HashMap<>();
Map<String, String> querys = new HashMap<>();
querys.put("client_id", "318019123");
querys.put("client_secret", "3ac160febeab7e1bc959b4820b04512d");
querys.put("grant_type", "authorization_code");
querys.put("redirect_uri", "http://auth.cfmall.com/oauth/weibo/success");
querys.put("code", code);
Map<String, String> bodys = new HashMap<>();
HttpResponse response = HttpUtils.doPost("https://api.weibo.com", "/oauth2/access_token", "post", headers, querys, bodys);
return "redirect:http://cfmall.com";
}
Gitee登录接口
@GetMapping("/oauth2.0/gitee/success")
public String gitee(@RequestParam("code") String code) throws Exception {
Map<String, String> headers = new HashMap<>();
Map<String, String> querys = new HashMap<>();
querys.put("client_id", "2077705774");
querys.put("client_secret", "40af02bd1c7e435ba6a6e9cd3bf799fd");
querys.put("grant_type", "authorization_code");
querys.put("redirect_uri", "http://auth.cfmall.com/oauth2.0/gitee/success");
querys.put("code", code);
Map<String, String> bodys = new HashMap<>();
HttpResponse response = HttpUtils.doPost("https://gitee.com/", "oauth/token", "post", headers, querys, bodys);
return "redirect:http://cfmall.com";
}