前言
> 此 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
|
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
|
public class OauthController extends Controller { static { PropKit.use("oauth.properties"); }
public void index() { 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"); renderJson(dictType); }
public void login() { String oauthType = getPara(0); AuthRequest authRequest = getAuthRequest(oauthType); redirect(authRequest.authorize(AuthStateUtils.createState())); }
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
|
public class DemoApplication { public static void main(String[] args) { UndertowServer.start(DemoConfig.class, 8080, true); } }
|
3. 运行方式
打开浏览器,输入 http://oauth.xkcoding.com/demo/oauth ,点击各个登录方式自行测试。
参考
- Spring Boot快速集成第三方登录功能:https://xkcoding.com/2019/05/22/spring-boot-login-with-oauth.html
- JFinal官方文档:https://www.jfinal.com/doc
- JustAuth 项目地址:https://github.com/zhangyd-c/JustAuth
- QQ互联文档:http://wiki.connect.qq.com/准备工作_oauth2-0
- GitHub第三方登录文档:https://developer.github.com/apps/building-oauth-apps/
- 小米开放平台账号服务文档:https://dev.mi.com/console/doc/detail?pId=707