1
0
mirror of synced 2025-11-06 04:21:05 +08:00

justauth-spring-boot-starter 完成~

This commit is contained in:
Yangkai.Shen
2019-07-22 15:23:07 +08:00
commit 78d8dccad8
9 changed files with 581 additions and 0 deletions

19
.editorconfig Normal file
View File

@@ -0,0 +1,19 @@
# JustAuth 开发组IDE 编辑器标准
root = true
# 空格替代Tab缩进在各种编辑工具下效果一致
[*]
indent_style = space
indent_size = 2
charset = utf-8
end_of_line = lf
trim_trailing_whitespace = true
insert_final_newline = true
[*.java]
indent_size = 4
[*.md]
insert_final_newline = false
trim_trailing_whitespace = false

30
.gitignore vendored Normal file
View File

@@ -0,0 +1,30 @@
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rarp
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
# exclude idea files
.idea
*.iml
*.sh
target

120
README.md Normal file
View File

@@ -0,0 +1,120 @@
# justauth-spring-boot-starter
> Spring Boot 集成 JustAuth 的最佳实践~
## 快速开始
- 因为暂时没有发布至中央仓库,因此需要体验的童鞋暂时使用我的私服玩儿吧~ 在 `pom.xml` 中添加以下内容
```xml
<repositories>
<!--阿里云私服-->
<repository>
<id>aliyun</id>
<name>aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</repository>
<!--xkcoding 私服-->
<repository>
<id>xkcoding-nexus</id>
<name>xkcoding nexus</name>
<url>https://nexus.xkcoding.com/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
```
- 引用依赖
```xml
<dependency>
<groupId>com.xkcoding</groupId>
<artifactId>justauth-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
```
- 添加配置,在 `application.yml` 中添加配置配置信息
```yaml
justauth:
enabled: true
type:
QQ:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/qq/callback
```
- 然后就开始玩耍吧~
```java
package com.xkcoding.justauthspringbootstarterdemo;
import com.xkcoding.justauth.AuthRequestFactory;
import lombok.RequiredArgsConstructor;
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.AuthState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* <p>
* 测试 Controller
* </p>
*
* @author yangkai.shen
* @date Created in 2019-07-22 11:17
*/
@RestController
@RequestMapping("/oauth")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class TestController {
private final AuthRequestFactory factory;
@GetMapping("/login/qq")
public void login(HttpServletResponse response) throws IOException {
AuthRequest authRequest = factory.get(AuthSource.QQ);
response.sendRedirect(authRequest.authorize());
}
@RequestMapping("/qq/callback")
public AuthResponse login(AuthCallback callback) {
AuthRequest authRequest = factory.get(AuthSource.QQ);
AuthResponse response = authRequest.login(callback);
// 移除校验通过的state
AuthState.delete(AuthSource.QQ);
return response;
}
}
```
## 附录
`justauth` 配置列表
| 属性名 | 类型 | 默认值 | 可选项 | 描述 |
| ------------------ | ------------------------------------------------------------ | ------ | ---------- | ----------------- |
| `justauth.enabled` | `boolean` | true | true/false | 是否启用 JustAuth |
| `justauth.type` | `java.util.Map<me.zhyd.oauth.config.AuthSource,me.zhyd.oauth.config.AuthConfig>` | 无 | | JustAuth 配置 |
`justauth.type` 配置列表
| 属性名 | 描述 |
| --------------------------- | ------------------------------------------------------------ |
| `justauth.type.keys` | `justauth.type``Map` 格式的key 的取值请参考 [`AuthSource`](https://github.com/zhangyd-c/JustAuth/blob/master/src/main/java/me/zhyd/oauth/config/AuthSource.java) |
| `justauth.type.keys.values` | `justauth.type``Map` 格式的value 的取值请参考 [`AuthConfig`](https://github.com/zhangyd-c/JustAuth/blob/master/src/main/java/me/zhyd/oauth/config/AuthConfig.java) |

138
pom.xml Normal file
View File

@@ -0,0 +1,138 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xkcoding</groupId>
<artifactId>justauth-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<!--maven配置信息-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!--SpringBoot版本-->
<spring-boot.version>2.1.6.RELEASE</spring-boot.version>
<!--JustAuth版本-->
<justauth.version>1.9.2-SNAPSHOT</justauth.version>
</properties>
<dependencies>
<dependency>
<groupId>me.zhyd.oauth</groupId>
<artifactId>JustAuth</artifactId>
<version>${justauth.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<!--https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/html/configuration-metadata.html#configuration-metadata-annotation-processor-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!-- spring boot 版本控制 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<target>${maven.compiler.target}</target>
<source>${maven.compiler.source}</source>
<encoding>UTF-8</encoding>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
<repositories>
<!--阿里云私服-->
<repository>
<id>aliyun</id>
<name>aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</repository>
<!--xkcoding 私服-->
<repository>
<id>xkcoding-nexus</id>
<name>xkcoding nexus</name>
<url>https://nexus.xkcoding.com/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<distributionManagement>
<!-- 发布版私服仓库 -->
<repository>
<id>releases</id>
<name>Nexus Release Repository</name>
<url>https://nexus.xkcoding.com/repository/maven-releases/</url>
</repository>
<!-- 快照版私服仓库 -->
<snapshotRepository>
<id>snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>https://nexus.xkcoding.com/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
</project>

View File

@@ -0,0 +1,80 @@
package com.xkcoding.justauth;
import com.xkcoding.justauth.properties.JustAuthProperties;
import lombok.RequiredArgsConstructor;
import me.zhyd.oauth.config.AuthSource;
import me.zhyd.oauth.request.*;
/**
* <p>
* AuthRequest工厂类
* </p>
*
* @author yangkai.shen
* @date Created in 2019-07-22 14:21
*/
@RequiredArgsConstructor
public class AuthRequestFactory {
private final JustAuthProperties properties;
/**
* 返回AuthRequest对象
*
* @param source {@link AuthSource}
* @return {@link AuthRequest}
*/
public AuthRequest get(AuthSource source) {
switch (source) {
case GITHUB:
return new AuthGithubRequest(properties.getType().get(source));
case WEIBO:
return new AuthWeiboRequest(properties.getType().get(source));
case GITEE:
return new AuthGiteeRequest(properties.getType().get(source));
case DINGTALK:
return new AuthDingTalkRequest(properties.getType().get(source));
case BAIDU:
return new AuthBaiduRequest(properties.getType().get(source));
case CSDN:
return new AuthCsdnRequest(properties.getType().get(source));
case CODING:
return new AuthCodingRequest(properties.getType().get(source));
case TENCENT_CLOUD:
return new AuthTencentCloudRequest(properties.getType().get(source));
case OSCHINA:
return new AuthOschinaRequest(properties.getType().get(source));
case ALIPAY:
return new AuthAlipayRequest(properties.getType().get(source));
case QQ:
return new AuthQqRequest(properties.getType().get(source));
case WECHAT:
return new AuthWeChatRequest(properties.getType().get(source));
case TAOBAO:
return new AuthTaobaoRequest(properties.getType().get(source));
case GOOGLE:
return new AuthGoogleRequest(properties.getType().get(source));
case FACEBOOK:
return new AuthFacebookRequest(properties.getType().get(source));
case DOUYIN:
return new AuthDouyinRequest(properties.getType().get(source));
case LINKEDIN:
return new AuthLinkedinRequest(properties.getType().get(source));
case MICROSOFT:
return new AuthMicrosoftRequest(properties.getType().get(source));
case MI:
return new AuthMiRequest(properties.getType().get(source));
case TOUTIAO:
return new AuthToutiaoRequest(properties.getType().get(source));
case TEAMBITION:
return new AuthTeambitionRequest(properties.getType().get(source));
case RENREN:
return new AuthRenrenRequest(properties.getType().get(source));
case PINTEREST:
return new AuthPinterestRequest(properties.getType().get(source));
case STACK_OVERFLOW:
return new AuthStackOverflowRequest(properties.getType().get(source));
default:
return null;
}
}
}

View File

@@ -0,0 +1,27 @@
package com.xkcoding.justauth;
import com.xkcoding.justauth.properties.JustAuthProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* <p>
* JustAuth 自动装配类
* </p>
*
* @author yangkai.shen
* @date Created in 2019-07-22 10:52
*/
@Configuration
@EnableConfigurationProperties(JustAuthProperties.class)
public class JustAuthAutoConfiguration {
@Bean
@ConditionalOnProperty(prefix = "justauth", value = "enabled", havingValue = "true", matchIfMissing = true)
public AuthRequestFactory authRequestFactory(JustAuthProperties properties) {
return new AuthRequestFactory(properties);
}
}

View File

@@ -0,0 +1,34 @@
package com.xkcoding.justauth.properties;
import lombok.Getter;
import lombok.Setter;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.HashMap;
import java.util.Map;
/**
* <p>
* JustAuth自动装配配置类
* </p>
*
* @author yangkai.shen
* @date Created in 2019-07-22 10:59
*/
@Getter
@Setter
@ConfigurationProperties(prefix = "justauth")
public class JustAuthProperties {
/**
* 是否启用 JustAuth
*/
private boolean enabled;
/**
* JustAuth 配置
*/
private Map<AuthSource, AuthConfig> type = new HashMap<>();
}

View File

@@ -0,0 +1,132 @@
{
"properties": [
{
"name": "justauth.type",
"type": "java.util.Map<me.zhyd.oauth.config.AuthSource,me.zhyd.oauth.config.AuthConfig>",
"description": "JustAuth 配置.",
"sourceType": "com.xkcoding.justauth.properties.JustAuthProperties"
}
],
"hints": [
{
"name": "justauth.type.keys",
"providers": [
{
"name": "handle-as",
"values": [
{
"value": "GITHUB",
"description": "GITHUB."
},
{
"value": "WEIBO",
"description": "WEIBO."
},
{
"value": "GITEE",
"description": "GITEE."
},
{
"value": "DINGTALK",
"description": "DINGTALK."
},
{
"value": "BAIDU",
"description": "BAIDU."
},
{
"value": "CSDN",
"description": "CSDN."
},
{
"value": "CODING",
"description": "CODING."
},
{
"value": "TENCENT_CLOUD",
"description": "TENCENT_CLOUD."
},
{
"value": "OSCHINA",
"description": "OSCHINA."
},
{
"value": "ALIPAY",
"description": "ALIPAY."
},
{
"value": "QQ",
"description": "QQ."
},
{
"value": "WECHAT",
"description": "WECHAT."
},
{
"value": "TAOBAO",
"description": "TAOBAO."
},
{
"value": "GOOGLE",
"description": "GOOGLE."
},
{
"value": "FACEBOOK",
"description": "FACEBOOK."
},
{
"value": "DOUYIN",
"description": "DOUYIN."
},
{
"value": "LINKEDIN",
"description": "LINKEDIN."
},
{
"value": "MICROSOFT",
"description": "MICROSOFT."
},
{
"value": "MI",
"description": "MI."
},
{
"value": "TOUTIAO",
"description": "TOUTIAO."
},
{
"value": "TEAMBITION",
"description": "TEAMBITION."
},
{
"value": "RENREN",
"description": "RENREN."
},
{
"value": "PINTEREST",
"description": "PINTEREST."
},
{
"value": "STACK_OVERFLOW",
"description": "STACK_OVERFLOW."
}
],
"parameters": {
"target": "me.zhyd.oauth.config.AuthSource"
}
}
]
},
{
"name": "justauth.type.values",
"providers": [
{
"name": "handle-as",
"parameters": {
"target": "me.zhyd.oauth.config.AuthConfig"
}
}
]
}
]
}

View File

@@ -0,0 +1 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.xkcoding.justauth.JustAuthAutoConfiguration