Commit de210341869dc0593b27f106ada60702c43d44ea

Authored by timfruit
1 parent 68c98db1

增加小程序授权登录

yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/compent/justauth/AuthExtendSource.java 0 → 100644
  1 +package cn.iocoder.yudao.coreservice.modules.system.compent.justauth;
  2 +
  3 +import me.zhyd.oauth.config.AuthSource;
  4 +
  5 +public enum AuthExtendSource implements AuthSource {
  6 +
  7 + /**
  8 + * 微信小程序授权登录
  9 + */
  10 + WECHAT_MINI_PROGRAM{
  11 + @Override
  12 + public String authorize() {
  13 + // https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html
  14 + throw new UnsupportedOperationException("不支持获取授权url, 请使用小程序内置函数wx.login()登录获取code");
  15 + }
  16 +
  17 + @Override
  18 + public String accessToken() {
  19 + // https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html
  20 + // 获取openid, unionid , session_key
  21 + return "https://api.weixin.qq.com/sns/jscode2session";
  22 + }
  23 +
  24 + @Override
  25 + public String userInfo() {
  26 + //https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserProfile.html
  27 + throw new UnsupportedOperationException("不支持获取用户信息url, 请使用小程序内置函数wx.getUserProfile()获取用户信息");
  28 + }
  29 + }
  30 +
  31 + ;
  32 +
  33 +}
... ...
yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/compent/justauth/AuthExtendToken.java 0 → 100644
  1 +package cn.iocoder.yudao.coreservice.modules.system.compent.justauth;
  2 +
  3 +import lombok.*;
  4 +import me.zhyd.oauth.model.AuthToken;
  5 +
  6 +/**
  7 + * @author timfruit
  8 + * @date 2021-10-29
  9 + */
  10 +@Getter
  11 +@Setter
  12 +@NoArgsConstructor
  13 +@AllArgsConstructor
  14 +public class AuthExtendToken extends AuthToken {
  15 + /**
  16 + * 微信小程序 会话密钥
  17 + */
  18 + private String miniSessionKey;
  19 +
  20 +
  21 +}
... ...
yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/compent/justauth/AuthWeChatMiniProgramRequest.java 0 → 100644
  1 +package cn.iocoder.yudao.coreservice.modules.system.compent.justauth;
  2 +
  3 +import com.alibaba.fastjson.JSONObject;
  4 +import me.zhyd.oauth.cache.AuthStateCache;
  5 +import me.zhyd.oauth.config.AuthConfig;
  6 +import me.zhyd.oauth.exception.AuthException;
  7 +import me.zhyd.oauth.model.AuthCallback;
  8 +import me.zhyd.oauth.model.AuthToken;
  9 +import me.zhyd.oauth.model.AuthUser;
  10 +import me.zhyd.oauth.request.AuthDefaultRequest;
  11 +import me.zhyd.oauth.utils.HttpUtils;
  12 +import me.zhyd.oauth.utils.UrlBuilder;
  13 +
  14 +/**
  15 + * 微信小程序
  16 + *
  17 + * @author timfruit
  18 + * @date 2021-10-29
  19 + */
  20 +public class AuthWeChatMiniProgramRequest extends AuthDefaultRequest {
  21 + public AuthWeChatMiniProgramRequest(AuthConfig config) {
  22 + super(config, AuthExtendSource.WECHAT_MINI_PROGRAM);
  23 + }
  24 +
  25 + public AuthWeChatMiniProgramRequest(AuthConfig config, AuthStateCache authStateCache) {
  26 + super(config, AuthExtendSource.WECHAT_MINI_PROGRAM, authStateCache);
  27 + }
  28 +
  29 + @Override
  30 + protected AuthToken getAccessToken(AuthCallback authCallback) {
  31 + // https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html
  32 + String response = new HttpUtils(config.getHttpConfig()).get(accessTokenUrl(authCallback.getCode()));
  33 + JSONObject accessTokenObject = JSONObject.parseObject(response);
  34 +
  35 + this.checkResponse(accessTokenObject);
  36 +
  37 + AuthExtendToken token=new AuthExtendToken();
  38 + token.setMiniSessionKey(accessTokenObject.getString("session_key"));
  39 + token.setOpenId(accessTokenObject.getString("openid"));
  40 + token.setUnionId(accessTokenObject.getString("unionid"));
  41 + return token;
  42 + }
  43 +
  44 + @Override
  45 + protected AuthUser getUserInfo(AuthToken authToken) {
  46 + // https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserProfile.html
  47 + // 如果需要用户信息,需要在小程序调用函数后传给后端
  48 + return AuthUser.builder()
  49 + .uuid(authToken.getOpenId())
  50 + //TODO 是使用默认值,还是有小程序获取用户信息 和 code 一起传过来
  51 + .nickname("")
  52 + .avatar("")
  53 + .token(authToken)
  54 + .source(source.toString())
  55 + .build();
  56 + }
  57 +
  58 + /**
  59 + * 检查响应内容是否正确
  60 + *
  61 + * @param object 请求响应内容
  62 + */
  63 + private void checkResponse(JSONObject object) {
  64 + int code = object.getIntValue("errcode");
  65 + if(code != 0){
  66 + throw new AuthException(object.getIntValue("errcode"), object.getString("errmsg"));
  67 + }
  68 + }
  69 +
  70 + /**
  71 + * 返回获取accessToken的url
  72 + *
  73 + * @param code 授权码
  74 + * @return 返回获取accessToken的url
  75 + */
  76 + @Override
  77 + protected String accessTokenUrl(String code) {
  78 + return UrlBuilder.fromBaseUrl(source.accessToken())
  79 + .queryParam("appid", config.getClientId())
  80 + .queryParam("secret", config.getClientSecret())
  81 + .queryParam("js_code", code)
  82 + .queryParam("grant_type", "authorization_code")
  83 + .build();
  84 + }
  85 +}
... ...
yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/enums/social/SysSocialTypeEnum.java
... ... @@ -20,21 +20,28 @@ public enum SysSocialTypeEnum implements IntArrayValuable {
20 20  
21 21 GITEE(10, "GITEE"), // https://gitee.com/api/v5/oauth_doc#/
22 22 DINGTALK(20, "DINGTALK"), // https://developers.dingtalk.com/document/app/obtain-identity-credentials
  23 + /**
  24 + * 企业微信
  25 + */
23 26 WECHAT_ENTERPRISE(30, "WECHAT_ENTERPRISE"), // https://xkcoding.com/2019/08/06/use-justauth-integration-wechat-enterprise.html
24   - // TODO @timfruit:微信平台,从 30 开始递增哈。另外,尽量不要出现,先 12,然后 11,有序,嘿嘿。
25 27 /**
26   - * 微信公众平台 - H5
  28 + * 微信公众平台 - 移动端H5
  29 + */
  30 + WECHAT_MP(31, "WECHAT_MP"), // https://www.cnblogs.com/juewuzhe/p/11905461.html
  31 + /**
  32 + * 微信开放平台 - 网站应用 pc端扫码授权登录
27 33 */
28   - WECHAT_MP(12, "WECHAT_MP"), // https://www.cnblogs.com/juewuzhe/p/11905461.html
  34 + 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
29 35 /**
30   - * 微信开放平台 - 小程序
  36 + * 微信小程序
31 37 */
32   - WECHAT_OPEN(11, "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
  38 + WECHAT_MINI_PROGRAM(33, "WECHAT_MINI_PROGRAM"), // https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html
33 39 ;
34 40  
35 41 public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(SysSocialTypeEnum::getType).toArray();
36 42  
37   - public static final List<Integer> WECHAT_ALL = ListUtil.toList(WECHAT_ENTERPRISE.type, WECHAT_MP.type, WECHAT_OPEN.type);
  43 + public static final List<Integer> WECHAT_ALL = ListUtil.toList(WECHAT_ENTERPRISE.type, WECHAT_MP.type, WECHAT_OPEN.type,
  44 + WECHAT_MINI_PROGRAM.type);
38 45  
39 46 /**
40 47 * 类型
... ...
yudao-user-server/src/main/resources/application-local.yaml
... ... @@ -158,10 +158,19 @@ justauth:
158 158 client-secret: 1wTb7hYxnpT2TUbIeHGXGo7T0odav1ic10mLdyyATOw
159 159 agent-id: 1000004
160 160 ignore-check-redirect-uri: true
161   - WECHAT_MP: # 微信公众平台 - H5 https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index
  161 + WECHAT_MP: # 微信公众平台 - 移动端H5 https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index
162 162 client-id: wxa5a05b85ac361f96
163 163 client-secret: 247073c7cebb67f27f0e769195c2a57e
164 164 ignore-check-redirect-uri: true
  165 + extend:
  166 + enum-class: cn.iocoder.yudao.coreservice.modules.system.compent.justauth.AuthExtendSource
  167 + config:
  168 + WECHAT_MINI_PROGRAM: # 微信小程序 扩展
  169 + request-class: cn.iocoder.yudao.coreservice.modules.system.compent.justauth.AuthWeChatMiniProgramRequest
  170 + client-id: wx44d047d87e6284d8
  171 + client-secret: 21c3b7a8a51ee1b8f5cf875848ed4466
  172 + ignore-check-redirect-uri: true
  173 + ignore-check-state: true
165 174 cache:
166 175 type: REDIS
167 176 prefix: 'social_auth_state:' # 缓存前缀,目前只对 Redis 缓存生效,默认 JUSTAUTH::STATE::
... ...