JFinal 快速集成第三方登录功能

前言

> 此 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

1. 环境准备

参考 Spring Boot 快速集成第三方登录功能 - 环境准备 这一章

2. 主要代码

2.1. pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?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>jfinal-justauth-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>jfinal-justauth-demo</name>

<description>
JFinal 使用 JustAuth 快速集成第三方登录
</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<justauth.version>1.9.5</justauth.version>
</properties>

<dependencies>
<dependency>
<groupId>com.jfinal</groupId>
<artifactId>jfinal-undertow</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>com.jfinal</groupId>
<artifactId>jfinal</artifactId>
<version>4.3</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.5.16</version>
</dependency>
<dependency>
<groupId>me.zhyd.oauth</groupId>
<artifactId>JustAuth</artifactId>
<version>${justauth.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<finalName>jfinal-justauth-demo</finalName>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

2.2. DemoConfig.java

此类为 JFinal 的配置类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* <p>
* 配置类
* </p>
*
* @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) {

}
}

2.3. OauthController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* <p>
* Oauth Controller
* </p>
*
* @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);
}
}

2.4. oauth.properties

1
2
3
4
5
6
7
8
9
10
11
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

2.5. DemoApplication.java

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* <p>
* 启动类
* </p>
*
* @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);
}
}

3. 运行方式

打开浏览器,输入 http://oauth.xkcoding.com/demo/oauth ,点击各个登录方式自行测试。

参考

  1. Spring Boot快速集成第三方登录功能:https://xkcoding.com/2019/05/22/spring-boot-login-with-oauth.html
  2. JFinal官方文档:https://www.jfinal.com/doc
  3. JustAuth 项目地址:https://github.com/zhangyd-c/JustAuth
  4. QQ互联文档:http://wiki.connect.qq.com/准备工作_oauth2-0
  5. GitHub第三方登录文档:https://developer.github.com/apps/building-oauth-apps/
  6. 小米开放平台账号服务文档:https://dev.mi.com/console/doc/detail?pId=707
-------------本文结束  感谢您的阅读-------------
xkcoding wechat
欢迎来我的公众号「xkcoding小凯扣丁」逛逛
o(╯□╰)o 赞助一杯咖啡 ~~