commit 78d8dccad80635168fc61707d63f55bcdbde9fa1 Author: Yangkai.Shen <237497819@qq.com> Date: Mon Jul 22 15:23:07 2019 +0800 :sparkles: justauth-spring-boot-starter 完成~ diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..3d1c15e --- /dev/null +++ b/.editorconfig @@ -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 + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ae3f179 --- /dev/null +++ b/.gitignore @@ -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 \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..95e60c0 --- /dev/null +++ b/README.md @@ -0,0 +1,120 @@ +# justauth-spring-boot-starter + +> Spring Boot 集成 JustAuth 的最佳实践~ + +## 快速开始 + +- 因为暂时没有发布至中央仓库,因此需要体验的童鞋暂时使用我的私服玩儿吧~ 在 `pom.xml` 中添加以下内容 + +```xml + + + + aliyun + aliyun + http://maven.aliyun.com/nexus/content/groups/public + + + + xkcoding-nexus + xkcoding nexus + https://nexus.xkcoding.com/repository/maven-public/ + + true + + + true + + + +``` + +- 引用依赖 + +```xml + + com.xkcoding + justauth-spring-boot-starter + 0.0.1-SNAPSHOT + +``` + +- 添加配置,在 `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; + +/** + *

+ * 测试 Controller + *

+ * + * @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` | 无 | | 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) | + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..350a6c6 --- /dev/null +++ b/pom.xml @@ -0,0 +1,138 @@ + + + 4.0.0 + + com.xkcoding + justauth-spring-boot-starter + 0.0.1-SNAPSHOT + + + + UTF-8 + 1.8 + 1.8 + + 2.1.6.RELEASE + + 1.9.2-SNAPSHOT + + + + + me.zhyd.oauth + JustAuth + ${justauth.version} + + + org.springframework.boot + spring-boot-autoconfigure + + + + org.springframework.boot + spring-boot-configuration-processor + true + + + org.projectlombok + lombok + true + + + + + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + + + + ${project.artifactId} + + + maven-compiler-plugin + 3.8.0 + + ${maven.compiler.target} + ${maven.compiler.source} + UTF-8 + true + + + + maven-source-plugin + 2.4 + + + package + + jar-no-fork + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + ${spring-boot.version} + + + + repackage + + + + + + + + + + + + aliyun + aliyun + http://maven.aliyun.com/nexus/content/groups/public + + + + xkcoding-nexus + xkcoding nexus + https://nexus.xkcoding.com/repository/maven-public/ + + true + + + true + + + + + + + + releases + Nexus Release Repository + https://nexus.xkcoding.com/repository/maven-releases/ + + + + snapshots + Nexus Snapshot Repository + https://nexus.xkcoding.com/repository/maven-snapshots/ + + + + \ No newline at end of file diff --git a/src/main/java/com/xkcoding/justauth/AuthRequestFactory.java b/src/main/java/com/xkcoding/justauth/AuthRequestFactory.java new file mode 100644 index 0000000..87136f8 --- /dev/null +++ b/src/main/java/com/xkcoding/justauth/AuthRequestFactory.java @@ -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.*; + +/** + *

+ * AuthRequest工厂类 + *

+ * + * @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; + } + } +} diff --git a/src/main/java/com/xkcoding/justauth/JustAuthAutoConfiguration.java b/src/main/java/com/xkcoding/justauth/JustAuthAutoConfiguration.java new file mode 100644 index 0000000..3bc00df --- /dev/null +++ b/src/main/java/com/xkcoding/justauth/JustAuthAutoConfiguration.java @@ -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; + +/** + *

+ * JustAuth 自动装配类 + *

+ * + * @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); + } + +} diff --git a/src/main/java/com/xkcoding/justauth/properties/JustAuthProperties.java b/src/main/java/com/xkcoding/justauth/properties/JustAuthProperties.java new file mode 100644 index 0000000..bc64b35 --- /dev/null +++ b/src/main/java/com/xkcoding/justauth/properties/JustAuthProperties.java @@ -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; + +/** + *

+ * JustAuth自动装配配置类 + *

+ * + * @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 type = new HashMap<>(); + +} diff --git a/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/src/main/resources/META-INF/additional-spring-configuration-metadata.json new file mode 100644 index 0000000..e0c7c4e --- /dev/null +++ b/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -0,0 +1,132 @@ +{ + "properties": [ + { + "name": "justauth.type", + "type": "java.util.Map", + "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" + } + } + ] + } + ] +} \ No newline at end of file diff --git a/src/main/resources/META-INF/spring.factories b/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..aa36d57 --- /dev/null +++ b/src/main/resources/META-INF/spring.factories @@ -0,0 +1 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.xkcoding.justauth.JustAuthAutoConfiguration \ No newline at end of file