package com.thirdPartyLogin.adapter;
import com.alibaba.fastjson.JSONObject;
import com.thirdPartyLogin.pojo.UserInfo;
import com.thirdPartyLogin.service.UserService;
import com.thirdPartyLogin.utils.HttpClientUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* @Author YZK
* @Date 2024/3/14
* @Desc
*/
@Component
public class Login3rdAdapter extends UserService implements Login3rdTarget {
@Value("${gitee.state}")
private String giteeState;
@Value("${gitee.token.url}")
private String giteeTokenUrl;
@Value("${gitee.user.url}")
private String giteeUserUrl;
@Value("${gitee.user.prefix}")
private String giteeUserPrefix;
@Value("${gitee.client_id}")
private String giteeClient_id;
@Value("${gitee.client_secret}")
private String giteeClient_secret;
@Value("${gitee.callback}")
private String giteeCallBack;
@Override
public String loginByGitee(String code, String state) {
//进行state判断,state的值是前端与后端商定好的,前端将State传给Gitee平台,Gitee平台回传state给回调接口
// if (!giteeState.equals(state)) {
// throw new UnsupportedOperationException("Invalid state!");
// }
//请求Gitee平台获取token,并携带code
String replace = giteeTokenUrl
.replace("client_id=?", "client_id=" + giteeClient_id)
.replace("client_secret=?", "client_secret=" + giteeClient_secret)
.replace("redirect_uri=?", "redirect_uri=" + giteeCallBack);
String tokenUrl = replace.concat(code);
JSONObject tokenResponse = HttpClientUtils.execute(tokenUrl, HttpMethod.POST);
String token = String.valueOf(tokenResponse.get("access_token"));
System.out.println(token);
//请求用户信息,并携带 token
String userUrl = giteeUserUrl.concat(token);
JSONObject userInfoResponse = HttpClientUtils.execute(userUrl, HttpMethod.GET);
//获取用户信息,userName添加前缀 GITEE@, 密码保持与userName一致。讨论过程请参见2.3小节
String userName = giteeUserPrefix.concat(String.valueOf(userInfoResponse.get("name")));
String password = userName;
//“自动注册” 和登录功能,此处体现了方法的 “复用”
return autoRegister3rdAndLogin(userName, password);
}
private String autoRegister3rdAndLogin(String userName, String password) {
//如果第三方账号已经登录过,则直接登录
if (super.checkUserExists(userName)) {
return super.login(userName, password);
}
UserInfo userInfo = new UserInfo();
userInfo.setUserName(userName);
userInfo.setUserPassword(password);
userInfo.setCreateDate(new Date());
//如果第三方账号是第一次登录,先进行“自动注册”
super.register(userInfo);
//自动注册完成后,进行登录
return super.login(userName, password);
}
@Override
public String loginByWechat(String... params) {
return null;
}
@Override
public String loginByQQ(String... params) {
return null;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Java实现第三方登录
共32个文件
xml:9个
java:9个
class:8个
1 下载量 52 浏览量
2024-03-15
11:47:02
上传
评论
收藏 23KB RAR 举报
温馨提示
本文实现的源代码
资源推荐
资源详情
资源评论
收起资源包目录
thirdPartyLogin.rar (32个子文件)
thirdPartyLogin
HELP.md 1KB
thirdPartyLogin.iml 489B
pom.xml 3KB
src
test
java
com
designpattern
thirdPartyLogin
BridgingAndAdapterApplicationTests.java 232B
main
resources
application.properties 825B
templates
static
java
com
thirdPartyLogin
repo
UserRepository.java 511B
adapter
Login3rdTarget.java 238B
Login3rdAdapter.java 3KB
controller
UserController.java 1KB
utils
HttpClientUtils.java 1KB
pojo
UserInfo.java 693B
ThirdPartyLoginApplication.java 456B
service
UserService.java 1KB
.idea
jarRepositories.xml 870B
uiDesigner.xml 9KB
workspace.xml 4KB
misc.xml 428B
inspectionProfiles
Project_Default.xml 818B
compiler.xml 727B
modules.xml 277B
.gitignore 0B
encodings.xml 191B
target
classes
application.properties 825B
com
thirdPartyLogin
repo
UserRepository.class 709B
adapter
Login3rdTarget.class 385B
Login3rdAdapter.class 3KB
ThirdPartyLoginApplication.class 854B
controller
UserController.class 2KB
utils
HttpClientUtils.class 2KB
pojo
UserInfo.class 4KB
service
UserService.class 2KB
generated-sources
annotations
.gitignore 395B
共 32 条
- 1
资源评论
笑的像个child
- 粉丝: 1822
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功