admin管理员组文章数量:1646328
之前我们完成了基于Spring的Github第三方登录--通用化的第三方登陆实现以及基于Spring的新浪微博第三方登录实现,接下来我们来学习一下如何实现QQ第三方登录。
完善个人信息
在实现QQ第三方登录功能之前,需要前往QQ互联完善个人信息,并添加你的第三方应用的相关信息。
访问QQ互联,点击【管理中心】,进入应用开发者完善界面:
创建第三方应用
按要求注册应用者开发信息后,进入应用管理中心,点击【创建应用】,选择网站,按要求填写相关信息,与微博一样,QQ也需要填写网站地址以及验证是否是网站所有者,验证方法和微博一样,都是在首页添加一行代码,同时,还需要填写回调地址:
创建成功后我们就能得到相应的appId
以及appKey
。
第三方登录通用架构
同样,我们将添加基于Spring的Github第三方登录--通用化的第三方登陆实现中的通用化架构:
QQApi
首先,添加QQApi,为OAuthService提供进行OAuth验证的各个地址,相关的地址可以在QQ OAuth2.0开发文档中得到,最终的QQApi代码如下:
public class QQApi extends DefaultApi20 {
private static final String AUTHORIZE_URL = "https://graph.qq/oauth2.0/authorize?response_type=code&client_id=%s&redirect_uri=%s&state=%s";
private static final String SCOPED_AUTHORIZE_URL = AUTHORIZE_URL + "&scope=%s";
private static final String ACCESS_TOKEN_URL = "https://graph.qq/oauth2.0/token?grant_type=authorization_code&state=%s";
private final String qqState;
public QQApi(String state){
this.qqState = state;
}
@Override
public String getAuthorizationUrl(OAuthConfig config) {
if (config.hasScope()){
return String.format(SCOPED_AUTHORIZE_URL, config.getApiKey(), OAuthEncoder.encode(config.getCallback()),
qqState, OAuthEncoder.encode(config.getScope()));
}
else{
return String.format(AUTHORIZE_URL, config.getApiKey(), OAuthEncoder.encode(config.getCallback()), qqState);
}
}
@Override
public String getAccessTokenEndpoint() {
return String.format(ACCESS_TOKEN_URL, qqState);
}
}
QQOAuthService
接下来我们来添加QQOAuthService
,查看文档《QQ 如何获取用户open_id》,通过地址https://graph.qq/oauth2.0/me
获取用户的open id,并将其转换成OAuthUser
:
public class QQOAuthService extends OAuthServiceDeractor {
private static final String PROTECTED_RESOURCE_URL = "https://graph.qq/oauth2.0/me";
public QQOAuthService(OAuthService oAuthService) {
super(oAuthService, OAuthTypes.QQ);
}
@Override
public OAuthUser getOAuthUser(Token accessToken) {
OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
this.signRequest(accessToken, request);
Response response = request.send();
OAuthUser oAuthUser = new OAuthUser();
oAuthUser.setoAuthType(getoAuthType());
oAuthUser.setoAuthId(response.getBody());
oAuthUser.setUser(new User());
return oAuthUser;
}
}
配置QQOAuthService
最后添加QQOAuthService
的相关配置:
@Configuration
public class OAuthConfig {
private static final String CALLBACK_URL = "http://tianmaying/oauth/%s/callback";
@Value("${oAuth.qq.state}") String state;
@Value("${oAuth.qq.appId}") String qqAppId;
@Value("${oAuth.qq.appKey}") String qqAppKey;
@Bean
public QQApi qqApi(){
return new QQApi(state);
}
@Bean
public OAuthServiceDeractor getQQOAuthService(){
return new QQOAuthService(new ServiceBuilder()
.provider(qqApi())
.apiKey(qqAppId)
.apiSecret(qqAppKey)
.callback(String.format(CALLBACK_URL, OAuthTypes.QQ))
.build());
}
}
修改hosts
与新浪微博一样,修改hosts一遍进行本地调试。
windows系统hosts文件一般在C:\WINDOWS\system32\drivers\etc
mac系统hosts文件地址一般为:/etc/hosts
在hosts文件添加以下一行:
127.0.0.1 tianmaying
调试
进入根目录,运行sudo mvn spring-boot:run
命令,访问http://tianmaying (之前填写应用信息以及修改hosts时所填写的域名,这三个域名必须一致),由于必须通过域名进行访问,所以我们需要监听80端口,运行时需要超级管理员权限。
注意:将代码上线后,还需要经过QQ的审核,进入QQ 应用管理中心,按要求进行填写即可。
版权声明:本文标题:基于Spring的QQ第三方登录实现 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1729447173a1201517.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论