# act-justauth-demo
**Repository Path**: peiwisdom/act-justauth-demo
## Basic Information
- **Project Name**: act-justauth-demo
- **Description**: ActFramework 集成 JustAuth 的 demo
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 1
- **Created**: 2022-09-12
- **Last Updated**: 2022-09-12
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# act-justauth-demo
> 此 demo 主要为了演示 ActFramework 如何通过 JustAuth 快速集成第三方平台的登录。
>
> 如果有小伙伴是基于 Spring Boot 的可以参考这个 [**` demo`**](https://github.com/xkcoding/spring-boot-demo/tree/master/spring-boot-demo-social)
>
> https://github.com/xkcoding/spring-boot-demo/tree/master/spring-boot-demo-social
>
> 如果有小伙伴是基于 JFinal 的可以参考这个 [**` demo`**](https://github.com/xkcoding/jfinal-justauth-demo)
>
> https://github.com/xkcoding/jfinal-justauth-demo
## 步骤
### 0. 环境搭建
参考 [**`环境准备`**](https://github.com/xkcoding/spring-boot-demo/tree/master/spring-boot-demo-social#1-%E7%8E%AF%E5%A2%83%E5%87%86%E5%A4%87)
### 1. 创建工程
使用 idea 或者 eclipse 创建一个最简单的 maven 工程
### 2. 添加依赖
```xml
* 第三方登录请求处理 *
* * @author yangkai.shen * @date Created in 2019-07-22 18:30 */ @UrlContext("/oauth") @SuppressWarnings("unused") public class OAuthAction { /** * 登录类型 */ @GetAction @ResponseContentType(H.MediaType.JSON) public Dict loginType(H.Response response) { return Dict.create().set("QQ登录", "http://oauth.xkcoding.com/demo/oauth/login/qq").set("GitHub登录", "http://oauth.xkcoding.com/demo/oauth/login/github").set("小米登录", "http://oauth.xkcoding.com/demo/oauth/login/mi"); } /** * 认证页面 * * @param source 来源 {@link AuthSource} * @param response response */ @GetAction("login/{source}") public void renderAuth(AuthSource source, H.Response response) { AuthRequest authRequest = getAuthRequest(source); response.sendRedirect(authRequest.authorize(AuthStateUtils.createState())); } /** * 登录成功后的回调 * * @param source 来源 {@link AuthSource} * @param callback 携带返回的信息 {@link AuthCallback} * @return 登录成功后的信息 */ @GetAction("{source}/callback") @ResponseContentType(H.MediaType.JSON) public AuthResponse login(AuthSource source, AuthCallback callback) { AuthRequest authRequest = getAuthRequest(source); AuthResponse response = authRequest.login(callback); return response; } /** * 获取AuthRequest * * @param source 来源 {@link AuthSource} * @return {@link AuthRequest} */ private AuthRequest getAuthRequest(AuthSource source) { AuthConfig config = getAuthConfig(source); switch (source) { case QQ: return new AuthQqRequest(config); case GITHUB: return new AuthGithubRequest(config); case MI: return new AuthMiRequest(config); default: throw new RuntimeException("暂不支持的第三方登录"); } } private AuthConfig getAuthConfig(AuthSource source) { AppConfig appConfig = Act.appConfig(); String type = source.name().toLowerCase(); String clientId = $.convert(appConfig.get("oauth." + type + ".client-id")).toString(); String clientSecret = $.convert(appConfig.get("oauth." + type + ".client-secret")).toString(); String redirectUri = $.convert(appConfig.get("oauth." + type + ".redirect-uri")).toString(); return AuthConfig.builder().clientId(clientId).clientSecret(clientSecret).redirectUri(redirectUri).build(); } } ``` ### 4. 添加配置文件 ```properties # all properties please see: https://github.com/actframework/archetype-support/blob/act-archetype-support-1.8.7.0/src/main/resources/archetype-resources/src/main/resources/conf/app.properties http.port=8080 url.context=/demo oauth.qq.client-id=10*******85 oauth.qq.client-secret=1f7d0*********************3cc2d629e oauth.qq.redirect-uri=http://oauth.xkcoding.com/demo/oauth/qq/callback oauth.github.client-id=2d2**************d5f01086 oauth.github.client-secret=5a291************************7871306d1 oauth.github.redirect-uri=http://oauth.xkcoding.com/demo/oauth/github/callback oauth.mi.client-id=28823**************2994 oauth.mi.client-secret=nFeT***************** oauth.mi.redirect-uri=http://oauth.xkcoding.com/demo/oauth/mi/callback ``` ### 5. 程序入口类 ```java package com.xkcoding.demo; import act.Act; import org.osgl.http.H; import org.osgl.mvc.annotation.GetAction; /** ** Act 程序入口 *
* * @author yangkai.shen * @date Created in 2019-07-22 17:36 */ @SuppressWarnings("unused") public class AppEntry { @GetAction("/test") public void hello(H.Response response) { response.writeText("hello, act-framework!"); } public static void main(String[] args) throws Exception { Act.start("act-justauth-demo"); } } ``` Enjoy ~ 访问地址:http://localhost:8080/demo/oauth