OpenAuth 的 Spring Boot 自动配置封装,通过 application.yml 配置即可快速集成第三方 OAuth2 授权登录。
- 零代码接入:
application.yml配置平台参数即可使用 - 自动装配:Spring Boot 自动配置,注入
ThirdAuthRequestFactory - Redis State 缓存:可选的 Redis 缓存,适用于分布式部署
- 自定义扩展:支持通过配置注册自定义平台
<dependency>
<groupId>cn.ken</groupId>
<artifactId>thirdauth-spring-boot-starter</artifactId>
<version>2.0.1-SNAPSHOT</version>
</dependency>thirdauth:
type:
github:
client-id: your-client-id
client-secret: your-client-secret
redirect-uri: http://localhost:8080/callback/github
qq:
client-id: your-app-id
client-secret: your-app-key
redirect-uri: http://localhost:8080/callback/qq
cache:
type: redis # 可选,默认使用内存缓存@RestController
@RequiredArgsConstructor
public class AuthController {
private final ThirdAuthRequestFactory factory;
@GetMapping("/login/{platform}")
public String login(@PathVariable String platform) {
AuthRequest authRequest = factory.get(platform);
return "redirect:" + authRequest.authorizeUrl();
}
@GetMapping("/callback/{platform}")
public AuthUserInfo callback(@PathVariable String platform, AuthCallback callback) {
AuthRequest authRequest = factory.get(platform);
AuthResponse<AuthToken> tokenResp = authRequest.getAccessToken(callback);
AuthResponse<AuthUserInfo> userResp = authRequest.getUserInfo(tokenResp.getData());
return userResp.getData();
}
}| 配置 | 说明 | 默认值 |
|---|---|---|
thirdauth.enabled |
是否启用 | true |
thirdauth.type.{platform} |
各平台 OAuth2 配置 | — |
thirdauth.cache.type |
State 缓存类型(default / redis) |
default |
- Java 17
- Spring Boot 3.0
- OpenAuth 核心库
- Redis(可选)