Przeglądaj źródła

add 新增 对接 maxkey 三方单点登录

疯狂的狮子Li 1 rok temu
rodzic
commit
6df7b34b4a

+ 6 - 0
ruoyi-admin/src/main/resources/application-dev.yml

@@ -184,6 +184,12 @@ justauth:
   enabled: true
   address: http://localhost:80
   type:
+    maxkey:
+      # maxkey 服务器地址
+      server-url: http://localhost:8080
+      client-id: 10**********6
+      client-secret: 1f7d08**********5b7**********29e
+      redirect-uri: ${justauth.address}/social-callback?source=maxkey
     qq:
       client-id: 10**********6
       client-secret: 1f7d08**********5b7**********29e

+ 6 - 0
ruoyi-admin/src/main/resources/application-prod.yml

@@ -186,6 +186,12 @@ justauth:
   enabled: true
   address: http://localhost:80
   type:
+    maxkey:
+      # maxkey 服务器地址
+      server-url: http://localhost:8080
+      client-id: 10**********6
+      client-secret: 1f7d08**********5b7**********29e
+      redirect-uri: ${justauth.address}/social-callback?source=maxkey
     qq:
       client-id: 10**********6
       client-secret: 1f7d08**********5b7**********29e

+ 5 - 0
ruoyi-common/ruoyi-common-social/pom.xml

@@ -21,6 +21,11 @@
             <artifactId>JustAuth</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>org.dromara</groupId>
+            <artifactId>ruoyi-common-json</artifactId>
+        </dependency>
+
         <dependency>
             <groupId>org.dromara</groupId>
             <artifactId>ruoyi-common-redis</artifactId>

+ 5 - 0
ruoyi-common/ruoyi-common-social/src/main/java/org/dromara/common/social/config/properties/SocialLoginConfigProperties.java

@@ -60,4 +60,9 @@ public class SocialLoginConfigProperties {
      */
     private String clientOsType;
 
+    /**
+     * maxkey 服务器地址
+     */
+    private String serverUrl;
+
 }

+ 80 - 0
ruoyi-common/ruoyi-common-social/src/main/java/org/dromara/common/social/maxkey/AuthMaxKeyRequest.java

@@ -0,0 +1,80 @@
+package org.dromara.common.social.maxkey;
+
+import cn.hutool.core.lang.Dict;
+import me.zhyd.oauth.cache.AuthStateCache;
+import me.zhyd.oauth.config.AuthConfig;
+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.request.AuthDefaultRequest;
+import org.dromara.common.core.utils.SpringUtils;
+import org.dromara.common.json.utils.JsonUtils;
+
+/**
+ *  @author 长春叭哥 2023年03月26日
+ */
+public class AuthMaxKeyRequest extends AuthDefaultRequest {
+
+    public static final String SERVER_URL = SpringUtils.getProperty("justauth.type.maxkey.server-url");
+
+    /**
+     * 设定归属域
+     */
+    public AuthMaxKeyRequest(AuthConfig config) {
+        super(config, AuthMaxKeySource.MAXKEY);
+    }
+
+    public AuthMaxKeyRequest(AuthConfig config, AuthStateCache authStateCache) {
+        super(config, AuthMaxKeySource.MAXKEY, authStateCache);
+    }
+
+    @Override
+    protected AuthToken getAccessToken(AuthCallback authCallback) {
+        String body = doPostAuthorizationCode(authCallback.getCode());
+        Dict object = JsonUtils.parseMap(body);
+        // oauth/token 验证异常
+        if (object.containsKey("error")) {
+            throw new AuthException(object.getStr("error_description"));
+        }
+        // user 验证异常
+        if (object.containsKey("message")) {
+            throw new AuthException(object.getStr("message"));
+        }
+        return AuthToken.builder()
+            .accessToken(object.getStr("access_token"))
+            .refreshToken(object.getStr("refresh_token"))
+            .idToken(object.getStr("id_token"))
+            .tokenType(object.getStr("token_type"))
+            .scope(object.getStr("scope"))
+            .build();
+    }
+
+    @Override
+    protected AuthUser getUserInfo(AuthToken authToken) {
+        String body = doGetUserInfo(authToken);
+        Dict object = JsonUtils.parseMap(body);
+        // oauth/token 验证异常
+        if (object.containsKey("error")) {
+            throw new AuthException(object.getStr("error_description"));
+        }
+        // user 验证异常
+        if (object.containsKey("message")) {
+            throw new AuthException(object.getStr("message"));
+        }
+        return AuthUser.builder()
+            .uuid(object.getStr("id"))
+            .username(object.getStr("username"))
+            .nickname(object.getStr("name"))
+            .avatar(object.getStr("avatar_url"))
+            .blog(object.getStr("web_url"))
+            .company(object.getStr("organization"))
+            .location(object.getStr("location"))
+            .email(object.getStr("email"))
+            .remark(object.getStr("bio"))
+            .token(authToken)
+            .source(source.toString())
+            .build();
+    }
+
+}

+ 52 - 0
ruoyi-common/ruoyi-common-social/src/main/java/org/dromara/common/social/maxkey/AuthMaxKeySource.java

@@ -0,0 +1,52 @@
+package org.dromara.common.social.maxkey;
+
+import me.zhyd.oauth.config.AuthSource;
+import me.zhyd.oauth.request.AuthDefaultRequest;
+
+/**
+ * Oauth2 默认接口说明
+ *
+ * @author 长春叭哥 2023年03月26日
+ *
+ */
+public enum AuthMaxKeySource implements AuthSource {
+
+    /**
+     * 自己搭建的 maxkey 私服
+     */
+    MAXKEY {
+
+        /**
+         * 授权的api
+         */
+        @Override
+        public String authorize() {
+            return AuthMaxKeyRequest.SERVER_URL + "/sign/authz/oauth/v20/authorize";
+        }
+
+        /**
+         * 获取accessToken的api
+         */
+        @Override
+        public String accessToken() {
+            return AuthMaxKeyRequest.SERVER_URL + "/sign/authz/oauth/v20/token";
+        }
+
+        /**
+         * 获取用户信息的api
+         */
+        @Override
+        public String userInfo() {
+            return AuthMaxKeyRequest.SERVER_URL + "/sign/api/oauth/v20/me";
+        }
+
+        /**
+         * 平台对应的 AuthRequest 实现类,必须继承自 {@link AuthDefaultRequest}
+         */
+        @Override
+        public Class<? extends AuthDefaultRequest> getTargetClass() {
+            return AuthMaxKeyRequest.class;
+        }
+
+    }
+}

+ 2 - 0
ruoyi-common/ruoyi-common-social/src/main/java/org/dromara/common/social/utils/SocialUtils.java

@@ -11,6 +11,7 @@ import org.dromara.common.core.domain.model.LoginBody;
 import org.dromara.common.core.utils.SpringUtils;
 import org.dromara.common.social.config.properties.SocialLoginConfigProperties;
 import org.dromara.common.social.config.properties.SocialProperties;
+import org.dromara.common.social.maxkey.AuthMaxKeyRequest;
 
 /**
  * 认证授权工具类
@@ -61,6 +62,7 @@ public class SocialUtils  {
             case "gitlab" -> new AuthGitlabRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret).redirectUri(redirectUri).build(), STATE_CACHE);
             case "wechat_mp" -> new AuthWeChatMpRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret).redirectUri(redirectUri).build(), STATE_CACHE);
             case "aliyun" -> new AuthAliyunRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret).redirectUri(redirectUri).build(), STATE_CACHE);
+            case "maxkey" -> new AuthMaxKeyRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret).redirectUri(redirectUri).build(), STATE_CACHE);
             default -> throw new AuthException("未获取到有效的Auth配置");
         };
     }