feat:实现justauth第三方登录
This commit is contained in:
@@ -65,5 +65,11 @@
|
||||
<artifactId>schisandra-cloud-storage-auth-api</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.xkcoding</groupId>
|
||||
<artifactId>justauth-spring-boot-starter</artifactId>
|
||||
<version>1.1.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
@@ -0,0 +1,92 @@
|
||||
package com.schisandra.auth.application.controller;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.xkcoding.justauth.AuthRequestFactory;
|
||||
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;
|
||||
|
||||
/**
|
||||
* ClassName:SchisandraOauthController
|
||||
* Package:com.schisandra.auth.application.controller
|
||||
* Description:实现justauth第三方登录
|
||||
*
|
||||
* @Author:fanyang
|
||||
* @Create:2024/5/9 - 21:30
|
||||
* @Version: v1.0
|
||||
*/
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/oauth")
|
||||
@RequiredArgsConstructor(onConstructor_ = @Autowired)
|
||||
public class SchisandraOauthController {
|
||||
|
||||
private final AuthRequestFactory factory;
|
||||
|
||||
/**
|
||||
* 登录类型
|
||||
*/
|
||||
@GetMapping
|
||||
public Map<String, String> loginType() {
|
||||
List<String> oauthList = factory.oauthList();
|
||||
return oauthList.stream().collect(Collectors
|
||||
.toMap(oauth -> oauth.toLowerCase() + "登录",
|
||||
oauth -> "http://127.0.0.1:3000/oauth/login/" + oauth.toLowerCase()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录
|
||||
*
|
||||
* @param oauthType 第三方登录类型
|
||||
* @param response response
|
||||
* @throws IOException
|
||||
*/
|
||||
@RequestMapping("/login/{oauthType}")
|
||||
public void renderAuth(@PathVariable String oauthType, HttpServletResponse response)
|
||||
throws IOException {
|
||||
AuthRequest authRequest = factory.get(getAuthSource(oauthType));
|
||||
response.sendRedirect(authRequest.authorize(oauthType + "::" + AuthStateUtils.createState()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录成功后的回调
|
||||
*
|
||||
* @param oauthType 第三方登录类型
|
||||
* @param callback 携带返回的信息
|
||||
* @return 登录成功后的信息
|
||||
*/
|
||||
@RequestMapping("/{oauthType}/callback")
|
||||
public AuthResponse login(@PathVariable String oauthType, AuthCallback callback) {
|
||||
AuthRequest authRequest = factory.get(getAuthSource(oauthType));
|
||||
AuthResponse response = authRequest.login(callback);
|
||||
log.info("【response】= {}", JSONUtil.toJsonStr(response));
|
||||
return response;
|
||||
}
|
||||
|
||||
private AuthSource getAuthSource(String type) {
|
||||
if (StrUtil.isNotBlank(type)) {
|
||||
return AuthSource.valueOf(type.toUpperCase());
|
||||
} else {
|
||||
throw new RuntimeException("不支持的类型");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -100,5 +100,33 @@
|
||||
<artifactId>sms4j-Email-core</artifactId>
|
||||
<version>3.2.1</version>
|
||||
</dependency>
|
||||
|
||||
<!-- oauth工具类 -->
|
||||
<dependency>
|
||||
<groupId>com.xkcoding</groupId>
|
||||
<artifactId>justauth-spring-boot-starter</artifactId>
|
||||
<version>1.1.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- lombok插件 -->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
<version>5.3.8</version>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>29.0-jre</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
@@ -103,4 +103,15 @@ sa-token:
|
||||
# #您的sdkAppId
|
||||
# sdk-app-id: 您的sdkAppId
|
||||
|
||||
justauth:
|
||||
enabled: true
|
||||
type:
|
||||
gitee:
|
||||
client-id: 31b0e97dbf04c99ef1850d9ba50966a0fcd5cf84ee976d838b4894c2248dbce9
|
||||
client-secret: bae8d25c7ef11b3a4b409a5069d029a20eb393e3179d13b38ab20ee376a21127
|
||||
redirect-uri: http://127.0.0.1:3000/oauth/gitee/callback
|
||||
cache:
|
||||
type: redis
|
||||
prefix: 'SOCIAL::STATE::'
|
||||
timeout: 1h
|
||||
|
||||
|
Reference in New Issue
Block a user