Prechádzať zdrojové kódy

fix 集成第三方登录

Administrator 4 rokov pred
rodič
commit
30f06f0bdf

+ 7 - 0
boman-auth/pom.xml

@@ -15,6 +15,13 @@
     </description>
     
     <dependencies>
+
+        <!--集成第三方登录-->
+        <dependency>
+            <groupId>com.xkcoding.justauth</groupId>
+            <artifactId>justauth-spring-boot-starter</artifactId>
+            <version>1.4.0</version>
+        </dependency>
         
         <!-- SpringCloud Ailibaba Nacos -->
         <dependency>

+ 74 - 0
boman-auth/src/main/java/com/boman/auth/controller/OauthController.java

@@ -0,0 +1,74 @@
+package com.boman.auth.controller;
+
+import cn.hutool.core.util.StrUtil;
+import com.alibaba.fastjson.JSON;
+import com.boman.common.core.utils.JSONArrayUtils;
+import com.boman.common.core.utils.StringUtils;
+import com.fasterxml.jackson.annotation.JsonFormat;
+import com.xkcoding.justauth.AuthRequestFactory;
+import io.micrometer.core.instrument.util.JsonUtils;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import me.zhyd.oauth.config.AuthSource;
+import me.zhyd.oauth.model.AuthCallback;
+import me.zhyd.oauth.model.AuthResponse;
+import me.zhyd.oauth.request.AuthRequest;
+import me.zhyd.oauth.utils.AuthStateUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * @author tjf
+ * @Date: 2021/05/24/11:41
+ */
+@Slf4j
+@RestController
+@RequestMapping("/oauth")
+@RequiredArgsConstructor(onConstructor_ = @Autowired)
+public class OauthController {
+    private final AuthRequestFactory factory;
+
+    @GetMapping
+    public List<String> list() {
+        return factory.oauthList();
+    }
+
+    @GetMapping("/login/{type}")
+    public void login(@PathVariable String type, HttpServletResponse response) throws IOException {
+        AuthRequest authRequest = factory.get(type);
+        //生成授权地址,并重定向
+        String authorize = authRequest.authorize(type + "::" + AuthStateUtils.createState());
+        response.sendRedirect(authorize);
+    }
+
+    /**
+     * 回调登录
+     * @param type
+     * @param callback
+     * @return
+     */
+    @RequestMapping("/{type}/callback")
+    public AuthResponse login(@PathVariable String type, AuthCallback callback) {
+        AuthRequest authRequest = factory.get(getAuthSource(type));
+        AuthResponse response = authRequest.login(callback);
+        log.info("【response】= {}", JSON.toJSONString(response));
+        return response;
+    }
+
+    private String getAuthSource(String type) {
+        if (StrUtil.isNotBlank(type)) {
+            return type.toUpperCase();
+        } else {
+            throw new RuntimeException("类型为空");
+        }
+    }
+}

+ 11 - 0
boman-auth/src/main/resources/bootstrap.yml

@@ -23,3 +23,14 @@ spring:
         # 共享配置
         shared-configs:
           - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
+justauth:
+  enabled: true
+  type:
+    WECHAT_OPEN:
+      client-id: wxbd7b37548b5433ea
+      client-secret: 1a3c713279e80163ae1e39b0713ca711
+      redirect-uri: http://tx.xhsoftware.cn:9070/oauth/WECHAT_OPEN/callback
+  cache:
+    type: redis
+    prefix: 'SOCIAL::STATE::'
+    timeout: 1h

+ 55 - 0
boman-common/boman-common-core/src/main/java/com/boman/common/core/enums/MiniAppProperties.java

@@ -0,0 +1,55 @@
+package com.boman.common.core.enums;
+
+/**
+ * @author tjf
+ * @Date: 2021/05/24/11:16
+ */
+public enum MiniAppProperties {
+    //https://api.weixin.qq.com/sns/oauth2/access_token?
+    // appid=APPID&
+    // secret=SECRET&
+    // code=CODE&
+    // grant_type=authorization_code
+    WeChat("https://api.weixin.qq.com/sns/oauth2/access_token?appid=","WeChat","","","","authorization_code");
+
+    private final String platForm;
+    private final String url;
+    private final String appId;
+    //应用密钥AppSecret,在微信开放平台提交应用审核通过后获得
+    private final String secret;
+    private final String code;
+    private final String grantType;
+
+    MiniAppProperties(String platForm, String url, String appId, String secret, String code, String grantType) {
+        this.platForm = platForm;
+        this.url = url;
+        this.appId = appId;
+        this.secret = secret;
+        this.code = code;
+        this.grantType = grantType;
+    }
+
+    public String getPlatForm() {
+        return platForm;
+    }
+
+    public String getUrl() {
+        return url;
+    }
+
+    public String getAppId() {
+        return appId;
+    }
+
+    public String getSecret() {
+        return secret;
+    }
+
+    public String getCode() {
+        return code;
+    }
+
+    public String getGrantType() {
+        return grantType;
+    }
+}