code review 社交登陆的代码

This commit is contained in:
YunaiV 2021-10-30 09:12:17 +08:00
parent de21034186
commit cba9c609d7
4 changed files with 35 additions and 29 deletions

View File

@ -4,6 +4,7 @@ import lombok.*;
import me.zhyd.oauth.model.AuthToken; import me.zhyd.oauth.model.AuthToken;
/** /**
* TODO @timfruit类注释
* @author timfruit * @author timfruit
* @date 2021-10-29 * @date 2021-10-29
*/ */
@ -12,10 +13,10 @@ import me.zhyd.oauth.model.AuthToken;
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
public class AuthExtendToken extends AuthToken { public class AuthExtendToken extends AuthToken {
/** /**
* 微信小程序 会话密钥 * 微信小程序 会话密钥
*/ */
private String miniSessionKey; private String miniSessionKey;
} }

View File

@ -11,13 +11,17 @@ import me.zhyd.oauth.request.AuthDefaultRequest;
import me.zhyd.oauth.utils.HttpUtils; import me.zhyd.oauth.utils.HttpUtils;
import me.zhyd.oauth.utils.UrlBuilder; import me.zhyd.oauth.utils.UrlBuilder;
// TODO @timfruit新建一个 yudao-spring-boot-starter-biz-social 把这个拓展拿进去哈另外可以思考下
// 1. application-local.yaml 的配置里justauth.extend.enum-class 能否不配置而是自动配置好
// 2. application-local.yaml 的配置里justauth.extend.extend.config.WECHAT_MINI_PROGRAM 有办法和 justauth.type.WECHAT_MP 持平
/** /**
* 微信小程序 * 微信小程序登陆
* *
* @author timfruit * @author timfruit
* @date 2021-10-29 * @date 2021-10-29
*/ */
public class AuthWeChatMiniProgramRequest extends AuthDefaultRequest { public class AuthWeChatMiniProgramRequest extends AuthDefaultRequest {
public AuthWeChatMiniProgramRequest(AuthConfig config) { public AuthWeChatMiniProgramRequest(AuthConfig config) {
super(config, AuthExtendSource.WECHAT_MINI_PROGRAM); super(config, AuthExtendSource.WECHAT_MINI_PROGRAM);
} }
@ -30,11 +34,11 @@ public class AuthWeChatMiniProgramRequest extends AuthDefaultRequest {
protected AuthToken getAccessToken(AuthCallback authCallback) { protected AuthToken getAccessToken(AuthCallback authCallback) {
// https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html // https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html
String response = new HttpUtils(config.getHttpConfig()).get(accessTokenUrl(authCallback.getCode())); String response = new HttpUtils(config.getHttpConfig()).get(accessTokenUrl(authCallback.getCode()));
JSONObject accessTokenObject = JSONObject.parseObject(response); JSONObject accessTokenObject = JSONObject.parseObject(response); // TODO @timfruit使用 JsonUtils项目尽量避免直接使用某个 json
this.checkResponse(accessTokenObject); this.checkResponse(accessTokenObject);
AuthExtendToken token=new AuthExtendToken(); AuthExtendToken token = new AuthExtendToken();
token.setMiniSessionKey(accessTokenObject.getString("session_key")); token.setMiniSessionKey(accessTokenObject.getString("session_key"));
token.setOpenId(accessTokenObject.getString("openid")); token.setOpenId(accessTokenObject.getString("openid"));
token.setUnionId(accessTokenObject.getString("unionid")); token.setUnionId(accessTokenObject.getString("unionid"));
@ -62,16 +66,16 @@ public class AuthWeChatMiniProgramRequest extends AuthDefaultRequest {
*/ */
private void checkResponse(JSONObject object) { private void checkResponse(JSONObject object) {
int code = object.getIntValue("errcode"); int code = object.getIntValue("errcode");
if(code != 0){ if(code != 0){ // TODO @timfruitif (code != 0) { 注意空格
throw new AuthException(object.getIntValue("errcode"), object.getString("errmsg")); throw new AuthException(object.getIntValue("errcode"), object.getString("errmsg"));
} }
} }
/** /**
* 返回获取accessToken的url * 返回获取 accessToken url
* *
* @param code 授权码 * @param code 授权码
* @return 返回获取accessToken的url * @return 返回获取 accessToken url
*/ */
@Override @Override
protected String accessTokenUrl(String code) { protected String accessTokenUrl(String code) {
@ -82,4 +86,5 @@ public class AuthWeChatMiniProgramRequest extends AuthDefaultRequest {
.queryParam("grant_type", "authorization_code") .queryParam("grant_type", "authorization_code")
.build(); .build();
} }
} }

View File

@ -18,24 +18,37 @@ import java.util.List;
@AllArgsConstructor @AllArgsConstructor
public enum SysSocialTypeEnum implements IntArrayValuable { public enum SysSocialTypeEnum implements IntArrayValuable {
GITEE(10, "GITEE"), // https://gitee.com/api/v5/oauth_doc#/ /**
DINGTALK(20, "DINGTALK"), // https://developers.dingtalk.com/document/app/obtain-identity-credentials * Gitee
* 文档链接https://gitee.com/api/v5/oauth_doc#/
*/
GITEE(10, "GITEE"),
/**
* 钉钉
* 文档链接https://developers.dingtalk.com/document/app/obtain-identity-credentials
*/
DINGTALK(20, "DINGTALK"),
/** /**
* 企业微信 * 企业微信
* 文档链接https://xkcoding.com/2019/08/06/use-justauth-integration-wechat-enterprise.html
*/ */
WECHAT_ENTERPRISE(30, "WECHAT_ENTERPRISE"), // https://xkcoding.com/2019/08/06/use-justauth-integration-wechat-enterprise.html WECHAT_ENTERPRISE(30, "WECHAT_ENTERPRISE"),
/** /**
* 微信公众平台 - 移动端H5 * 微信公众平台 - 移动端 H5
* 文档链接https://www.cnblogs.com/juewuzhe/p/11905461.html
*/ */
WECHAT_MP(31, "WECHAT_MP"), // https://www.cnblogs.com/juewuzhe/p/11905461.html WECHAT_MP(31, "WECHAT_MP"),
/** /**
* 微信开放平台 - 网站应用 pc端扫码授权登录 * 微信开放平台 - 网站应用 PC 端扫码授权登录
* 文档链接https://justauth.wiki/guide/oauth/wechat_open/#_2-申请开发者资质认证
*/ */
WECHAT_OPEN(32, "WECHAT_OPEN"), // https://justauth.wiki/guide/oauth/wechat_open/#_2-%E7%94%B3%E8%AF%B7%E5%BC%80%E5%8F%91%E8%80%85%E8%B5%84%E8%B4%A8%E8%AE%A4%E8%AF%81 WECHAT_OPEN(32, "WECHAT_OPEN"),
/** /**
* 微信小程序 * 微信小程序
* 文档链接https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html
*/ */
WECHAT_MINI_PROGRAM(33, "WECHAT_MINI_PROGRAM"), // https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html WECHAT_MINI_PROGRAM(33, "WECHAT_MINI_PROGRAM"),
; ;
public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(SysSocialTypeEnum::getType).toArray(); public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(SysSocialTypeEnum::getType).toArray();

View File

@ -145,19 +145,6 @@ yudao:
justauth: justauth:
enabled: true enabled: true
type: type:
GITEE: # Gitee
client-id: ee61f0374a4c6c404a8717094caa7a410d76950e45ff60348015830c519ba5c1
client-secret: 7c044a5671be3b051414db0cf2cec6ad702dd298d2416ba24ceaf608e6fa26f9
ignore-check-redirect-uri: true
DINGTALK: # 钉钉
client-id: dingvrnreaje3yqvzhxg
client-secret: i8E6iZyDvZj51JIb0tYsYfVQYOks9Cq1lgryEjFRqC79P3iJcrxEwT6Qk2QvLrLI
ignore-check-redirect-uri: true
WECHAT_ENTERPRISE: # 企业微信
client-id: wwd411c69a39ad2e54
client-secret: 1wTb7hYxnpT2TUbIeHGXGo7T0odav1ic10mLdyyATOw
agent-id: 1000004
ignore-check-redirect-uri: true
WECHAT_MP: # 微信公众平台 - 移动端H5 https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index WECHAT_MP: # 微信公众平台 - 移动端H5 https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index
client-id: wxa5a05b85ac361f96 client-id: wxa5a05b85ac361f96
client-secret: 247073c7cebb67f27f0e769195c2a57e client-secret: 247073c7cebb67f27f0e769195c2a57e
@ -174,4 +161,4 @@ justauth:
cache: cache:
type: REDIS type: REDIS
prefix: 'social_auth_state:' # 缓存前缀,目前只对 Redis 缓存生效,默认 JUSTAUTH::STATE:: prefix: 'social_auth_state:' # 缓存前缀,目前只对 Redis 缓存生效,默认 JUSTAUTH::STATE::
timeout: 24h # 超时时长,目前只对 Redis 缓存生效,默认 3 分钟 timeout: 24h # 超时时长,目前只对 Redis 缓存生效,默认 3 分钟