mirror of
https://gitee.com/yadong.zhang/JustAuth.git
synced 2025-12-06 16:58:24 +08:00
Merge branch 'dev' of github.com:justauth/JustAuth into dev
This commit is contained in:
commit
44d77f3d6a
10
README.md
10
README.md
@ -55,11 +55,11 @@ JustAuth 集成了诸如:Github、Gitee、支付宝、新浪微博、微信、
|
|||||||
|
|
||||||
## 有哪些功能?
|
## 有哪些功能?
|
||||||
|
|
||||||
- 集成国内外数十家第三方平台,实现快速接入。<a href="https://www.justauth.cn/quickstart/how-to-use.html" target="_blank">参考文档</a>
|
- 集成国内外数十家第三方平台,实现快速接入。<a href="https://www.justauth.cn/guide/" target="_blank">参考文档</a>
|
||||||
- 自定义 State 缓存,支持各种分布式缓存组件。<a href="https://www.justauth.cn/features/customize-the-state-cache.html" target="_blank">参考文档</a>
|
- 自定义 State 缓存,支持各种分布式缓存组件。<a href="https://www.justauth.cn/features/using-state/" target="_blank">参考文档</a>
|
||||||
- 自定义 OAuth 平台,更容易适配自有的 OAuth 服务。<a href="https://www.justauth.cn/features/customize-the-oauth.html" target="_blank">参考文档</a>
|
- 自定义 OAuth 平台,更容易适配自有的 OAuth 服务。<a href="https://www.justauth.cn/features/customize-the-oauth/" target="_blank">参考文档</a>
|
||||||
- 自定义 Http 实现,选择权完全交给开发者,不会单独依赖某一具体实现。<a href="https://www.justauth.cn/quickstart/how-to-use.html#%E4%BD%BF%E7%94%A8%E6%96%B9%E5%BC%8F" target="_blank">参考文档</a>
|
- 自定义 Http 实现,选择权完全交给开发者,不会单独依赖某一具体实现。<a href="https://www.justauth.cn/guide/quickstart/how-to-use/#%E4%BD%BF%E7%94%A8%E6%AD%A5%E9%AA%A4" target="_blank">参考文档</a>
|
||||||
- 自定义 Scope,支持更完善的授权体系。<a href="https://www.justauth.cn/features/customize-scopes.html" target="_blank">参考文档</a>
|
- 自定义 Scope,支持更完善的授权体系。<a href="https://www.justauth.cn/features/customize-scopes/#%E5%85%B3%E4%BA%8E-scope" target="_blank">参考文档</a>
|
||||||
- 更多...<a href="https://www.justauth.cn" target="_blank">参考文档</a>
|
- 更多...<a href="https://www.justauth.cn" target="_blank">参考文档</a>
|
||||||
|
|
||||||
## 快速开始
|
## 快速开始
|
||||||
|
|||||||
@ -1464,6 +1464,35 @@ public enum AuthDefaultSource implements AuthSource {
|
|||||||
public Class<? extends AuthDefaultRequest> getTargetClass() {
|
public Class<? extends AuthDefaultRequest> getTargetClass() {
|
||||||
return AuthWechatMiniProgramRequest.class;
|
return AuthWechatMiniProgramRequest.class;
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* QQ小程序授权登录
|
||||||
|
*/
|
||||||
|
QQ_MINI_PROGRAM {
|
||||||
|
@Override
|
||||||
|
public String authorize() {
|
||||||
|
// 参见 https://q.qq.com/wiki/develop/miniprogram/frame/open_ability/open_userinfo.html 文档
|
||||||
|
throw new UnsupportedOperationException("不支持获取授权 url,请使用小程序内置函数 qq.login() 登录获取 code");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String accessToken() {
|
||||||
|
// 参见 https://q.qq.com/wiki/develop/miniprogram/server/open_port/port_login.html 文档
|
||||||
|
// 获取 openid, unionId , session_key 等字段
|
||||||
|
return "https://api.q.qq.com/sns/jscode2session";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String userInfo() {
|
||||||
|
// 参见 https://q.qq.com/wiki/develop/miniprogram/API/open_port/port_userinfo.html 文档
|
||||||
|
throw new UnsupportedOperationException("不支持获取用户信息 url,请使用小程序内置函数 qq.getUserInfo() 获取用户信息");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<? extends AuthDefaultRequest> getTargetClass() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,100 @@
|
|||||||
|
package me.zhyd.oauth.request;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.alibaba.fastjson.annotation.JSONField;
|
||||||
|
import lombok.Data;
|
||||||
|
import me.zhyd.oauth.cache.AuthStateCache;
|
||||||
|
import me.zhyd.oauth.config.AuthConfig;
|
||||||
|
import me.zhyd.oauth.config.AuthDefaultSource;
|
||||||
|
import me.zhyd.oauth.exception.AuthException;
|
||||||
|
import me.zhyd.oauth.model.AuthCallback;
|
||||||
|
import me.zhyd.oauth.model.AuthToken;
|
||||||
|
import me.zhyd.oauth.model.AuthUser;
|
||||||
|
import me.zhyd.oauth.utils.HttpUtils;
|
||||||
|
import me.zhyd.oauth.utils.UrlBuilder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* QQ小程序登陆 Request 请求
|
||||||
|
* <p>
|
||||||
|
* 参照微信小程序实现
|
||||||
|
*
|
||||||
|
* @author hurentian
|
||||||
|
* @since 2024-10-08
|
||||||
|
*/
|
||||||
|
public class AuthQQMiniProgramRequest extends AuthDefaultRequest {
|
||||||
|
public AuthQQMiniProgramRequest(AuthConfig config) {
|
||||||
|
super(config, AuthDefaultSource.QQ_MINI_PROGRAM);
|
||||||
|
}
|
||||||
|
|
||||||
|
public AuthQQMiniProgramRequest(AuthConfig config, AuthStateCache authStateCache) {
|
||||||
|
super(config, AuthDefaultSource.QQ_MINI_PROGRAM, authStateCache);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AuthToken getAccessToken(AuthCallback authCallback) {
|
||||||
|
// 参见 https://q.qq.com/wiki/develop/miniprogram/server/open_port/port_login.html#code2session 文档
|
||||||
|
// 使用 code 获取对应的 openId、unionId 等字段
|
||||||
|
String response = new HttpUtils(config.getHttpConfig()).get(accessTokenUrl(authCallback.getCode())).getBody();
|
||||||
|
JSCode2SessionResponse accessTokenObject = JSONObject.parseObject(response, JSCode2SessionResponse.class);
|
||||||
|
assert accessTokenObject != null;
|
||||||
|
checkResponse(accessTokenObject);
|
||||||
|
// 拼装结果
|
||||||
|
return AuthToken.builder()
|
||||||
|
.openId(accessTokenObject.getOpenid())
|
||||||
|
.unionId(accessTokenObject.getUnionId())
|
||||||
|
.accessToken(accessTokenObject.getSessionKey())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AuthUser getUserInfo(AuthToken authToken) {
|
||||||
|
// 参见 https://q.qq.com/wiki/develop/game/API/open-port/user-info.html#qq-getuserinfo 文档
|
||||||
|
// 如果需要用户信息,需要在小程序调用函数后传给后端
|
||||||
|
return AuthUser.builder()
|
||||||
|
.username("")
|
||||||
|
.nickname("")
|
||||||
|
.avatar("")
|
||||||
|
.uuid(authToken.getOpenId())
|
||||||
|
.token(authToken)
|
||||||
|
.source(source.toString())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查响应内容是否正确
|
||||||
|
*
|
||||||
|
* @param response 请求响应内容
|
||||||
|
*/
|
||||||
|
private void checkResponse(JSCode2SessionResponse response) {
|
||||||
|
if (response.getErrorCode() != 0) {
|
||||||
|
throw new AuthException(response.getErrorCode(), response.getErrorMsg());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String accessTokenUrl(String code) {
|
||||||
|
return UrlBuilder.fromBaseUrl(source.accessToken())
|
||||||
|
.queryParam("appid", config.getClientId())
|
||||||
|
.queryParam("secret", config.getClientSecret())
|
||||||
|
.queryParam("js_code", code)
|
||||||
|
.queryParam("grant_type", "authorization_code")
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@SuppressWarnings("SpellCheckingInspection")
|
||||||
|
private static class JSCode2SessionResponse {
|
||||||
|
|
||||||
|
@JSONField(name = "errcode")
|
||||||
|
private int errorCode;
|
||||||
|
@JSONField(name = "errmsg")
|
||||||
|
private String errorMsg;
|
||||||
|
@JSONField(name = "session_key")
|
||||||
|
private String sessionKey;
|
||||||
|
private String openid;
|
||||||
|
@JSONField(name = "unionid")
|
||||||
|
private String unionId;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -107,6 +107,12 @@ public class AuthRequestBuilderTest {
|
|||||||
System.out.println(value.getTargetClass());
|
System.out.println(value.getTargetClass());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
case QQ_MINI_PROGRAM: {
|
||||||
|
// 小程序不支持获取调用 authorize
|
||||||
|
AuthRequest authRequest = new AuthQQMiniProgramRequest(config);
|
||||||
|
System.out.println(value.getTargetClass());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
AuthRequest authRequest = AuthRequestBuilder.builder()
|
AuthRequest authRequest = AuthRequestBuilder.builder()
|
||||||
.source(value.getName())
|
.source(value.getName())
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user