# jfinal-justauth-demo
**Repository Path**: xkcoding/jfinal-justauth-demo
## Basic Information
- **Project Name**: jfinal-justauth-demo
- **Description**: Jfinal集成JustAuth的demo
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 13
- **Forks**: 9
- **Created**: 2019-08-19
- **Last Updated**: 2023-03-28
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# Jfinal JustAuth Demo
> 此 demo 主要为了演示 Jfinal 如何通过 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
>
> 如果有小伙伴是基于 ActFramework 的可以参考这个 [**` demo`**](https://github.com/xkcoding/act-justauth-demo)
>
> https://github.com/xkcoding/act-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-16 14:19 */ public class DemoConfig extends JFinalConfig { /** * 配置环境基础信息 */ @Override public void configConstant(Constants constants) { // 设置开发模式,打印请求日志 constants.setDevMode(true); } /** * 配置路由信息 */ @Override public void configRoute(Routes routes) { routes.add("/demo/test", TestController.class); routes.add("/demo/oauth", OauthController.class); } /** * 配置模板引擎相关 */ @Override public void configEngine(Engine engine) { } /** * 配置插件相关 */ @Override public void configPlugin(Plugins plugins) { } /** * 配置拦截器相关 */ @Override public void configInterceptor(Interceptors interceptors) { } @Override public void configHandler(Handlers handlers) { } } ``` ### 4. 添加Controller处理 ```java /** ** Oauth Controller *
* * @author yangkai.shen * @date Created in 2019-07-16 14:39 */ public class OauthController extends Controller { // 加载配置文件 static { PropKit.use("oauth.properties"); } public void index() { // @formatter:off Dict dictType = 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"); // @formatter:on renderJson(dictType); } /** * 参数获取参考:https://www.jfinal.com/doc/3-4 * Jfinal 中路径参数只能取最后一个,分隔符默认为 - 比如 /demo/para0-para1-para2 * 此时可通过 getPara(0) -> para0 ; getPara(1) -> para1 ; getPara(2) -> para2 */ public void login() { String oauthType = getPara(0); AuthRequest authRequest = getAuthRequest(oauthType); redirect(authRequest.authorize(AuthStateUtils.createState())); } /** * 登录成功后的回调 * 坑点: 因为Jfinal不支持 /{oauthType}/callback 这种格式的URL,因此回调地址修改如下 /callback/{oauthType} * * @param callback 携带返回的信息 */ public void callback(@Para("") AuthCallback callback) { String oauthType = getPara(0); AuthRequest authRequest = getAuthRequest(oauthType); AuthResponse response = authRequest.login(callback); renderJson(response); } private AuthRequest getAuthRequest(String oauthType) { AuthSource authSource = AuthSource.valueOf(oauthType.toUpperCase()); switch (authSource) { case QQ: return getQqAuthRequest(); case GITHUB: return getGithubAuthRequest(); case MI: return getMiAuthRequest(); default: throw new RuntimeException("暂不支持的第三方登录"); } } private AuthRequest getQqAuthRequest() { AuthConfig authConfig = AuthConfig.builder().clientId(PropKit.get("qq.clientId")).clientSecret(PropKit.get("qq.clientSecret")).redirectUri(PropKit.get("qq.redirectUri")).build(); return new AuthQqRequest(authConfig); } private AuthRequest getGithubAuthRequest() { AuthConfig authConfig = AuthConfig.builder().clientId(PropKit.get("github.clientId")).clientSecret(PropKit.get("github.clientSecret")).redirectUri(PropKit.get("github.redirectUri")).build(); return new AuthGithubRequest(authConfig); } private AuthRequest getMiAuthRequest() { AuthConfig authConfig = AuthConfig.builder().clientId(PropKit.get("mi.clientId")).clientSecret(PropKit.get("mi.clientSecret")).redirectUri(PropKit.get("mi.redirectUri")).build(); return new AuthMiRequest(authConfig); } } ``` ### 5. 添加配置文件 ```properties qq.clientId=10********85 qq.clientSecret=1f7********************629e qq.redirectUri=http://oauth.xkcoding.com/demo/oauth/callback/qq github.clientId=2***************916 github.clientSecret=e4e8cb602c87cf**********************a6f095b github.redirectUri=http://oauth.xkcoding.com/demo/oauth/callback/github mi.clientId=288****************994 mi.clientSecret=nFe****************= mi.redirectUri=http://oauth.xkcoding.com/demo/oauth/callback/mi ``` ### 6. 添加启动类 ```java /** ** 启动类 *
* * @author yangkai.shen * @date Created in 2019-07-16 14:18 */ public class DemoApplication { public static void main(String[] args) { UndertowServer.start(DemoConfig.class, 8080, true); } } ``` --- Enjoy ~ 访问地址:http://localhost:8080/demo/oauth