Skip to content

justauth/jfinal-justauth-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

5 Commits

Repository files navigation

Jfinal JustAuth Demo

Này demo chủ yếu vì biểu thị Jfinal như thế nào thông qua JustAuth nhanh chóng tổng thể kẻ thứ ba ngôi cao đăng nhập.

Nếu có tiểu đồng bọn là căn cứ vào Spring Boot có thể tham khảo cái nàydemo

https://github /xkcoding/spring-boot-demo/tree/master/spring-boot-demo-social

Nếu có tiểu đồng bọn là căn cứ vào ActFramework có thể tham khảo cái nàydemo

https://github /xkcoding/act-justauth-demo

Bước đi

0. Hoàn cảnh dựng

Tham khảoHoàn cảnh chuẩn bị

1. Sáng tạo công trình

Sử dụng idea hoặc là eclipse sáng tạo một cái đơn giản nhất maven công trình

2. Tăng thêm ỷ lại

<?xmlversion="1.0"encoding="UTF-8"?>

<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http:// 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 sử dụng JustAuth nhanh chóng tổng thể kẻ thứ ba đăng nhập
</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mavenpiler.source>1.8</mavenpiler.source>
<mavenpiler.target>1.8</mavenpiler.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>

3. Tăng thêm phối trí loại

/**
* <p>
* phối trí loại
* </p>
*
* @author yangkai.shen
* @date Created in 2019-07-16 14:19
*/
publicclassDemoConfigextendsJFinalConfig{
/**
* phối trí hoàn cảnh cơ sở tin tức
*/
@Override
publicvoidconfigConstant(Constantsconstants) {
// thiết trí khai phá hình thức, đóng dấu thỉnh cầu nhật ký
constants.setDevMode(true);
}

/**
* phối trí lộ từ tin tức
*/
@Override
publicvoidconfigRoute(Routesroutes) {
routes.add("/demo/test",TestController.class);
routes.add("/demo/oauth",OauthController.class);
}

/**
* phối trí khuôn mẫu động cơ tương quan
*/
@Override
publicvoidconfigEngine(Engineengine) {

}

/**
* phối trí cắm kiện tương quan
*/
@Override
publicvoidconfigPlugin(Pluginsplugins) {

}

/**
* phối trí chặn lại khí tương quan
*/
@Override
publicvoidconfigInterceptor(Interceptorsinterceptors) {

}

@Override
publicvoidconfigHandler(Handlershandlers) {

}
}

4. Tăng thêm Controller xử lý

/**
* <p>
* Oauth Controller
* </p>
*
* @author yangkai.shen
* @date Created in 2019-07-16 14:39
*/
publicclassOauthControllerextendsController{
// thêm tái phối trí văn kiện
static{
PropKit.use("oauth.properties");
}

publicvoidindex() {
// @formatter:off
DictdictType=Dict.create()
.set("QQ đăng nhập","http://oauth.xkcoding /demo/oauth/login/qq")
.set("GitHub đăng nhập","http://oauth.xkcoding /demo/oauth/login/github")
.set("Gạo kê đăng nhập","http://oauth.xkcoding /demo/oauth/login/mi");
// @formatter:on
renderJson(dictType);
}

/**
* tham số thu hoạch tham khảo: https:// jfinal /doc/3-4
* Jfinal trung lộ kính tham số chỉ có thể lấy cuối cùng một cái, phân cách phù cam chịu vì - tỷ như /demo/para0-para1-para2
* lúc này nhưng thông qua getPara(0) -> para0; getPara(1) -> para1; getPara(2) -> para2
*/
publicvoidlogin() {
StringoauthType=getPara(0);
AuthRequestauthRequest=getAuthRequest(oauthType);
redirect(authRequest.authorize(AuthStateUtils.createState()));
}

/**
* đăng nhập thành công sau hồi điều
* hố điểm: Bởi vì Jfinal không duy trì /{oauthType}/callback loại này cách thức URL, bởi vậy hồi điều địa chỉ sửa chữa như sau /callback/{oauthType}
*
* @param callback mang theo phản hồi tin tức
*/
publicvoidcallback(@Para("")AuthCallbackcallback) {
StringoauthType=getPara(0);
AuthRequestauthRequest=getAuthRequest(oauthType);
AuthResponseresponse=authRequest.login(callback);
renderJson(response);
}

privateAuthRequestgetAuthRequest(StringoauthType) {
AuthSourceauthSource=AuthSource.valueOf(oauthType.toUpperCase());
switch(authSource) {
caseQQ:
returngetQqAuthRequest();
caseGITHUB:
returngetGithubAuthRequest();
caseMI:
returngetMiAuthRequest();
default:
thrownewRuntimeException("Tạm không duy trì kẻ thứ ba đăng nhập");
}
}

privateAuthRequestgetQqAuthRequest() {
AuthConfigauthConfig=AuthConfig.builder().clientId(PropKit.get("qq.clientId")).clientSecret(PropKit.get("qq.clientSecret")).redirectUri(PropKit.get("qq.redirectUri")).build();
returnnewAuthQqRequest(authConfig);
}

privateAuthRequestgetGithubAuthRequest() {
AuthConfigauthConfig=AuthConfig.builder().clientId(PropKit.get("github.clientId")).clientSecret(PropKit.get("github.clientSecret")).redirectUri(PropKit.get("github.redirectUri")).build();
returnnewAuthGithubRequest(authConfig);
}

privateAuthRequestgetMiAuthRequest() {
AuthConfigauthConfig=AuthConfig.builder().clientId(PropKit.get("mi.clientId")).clientSecret(PropKit.get("mi.clientSecret")).redirectUri(PropKit.get("mi.redirectUri")).build();
returnnewAuthMiRequest(authConfig);
}
}

5. Tăng thêm phối trí văn kiện

qq.clientId=10********85
qq.clientSecret=1f7********************629e
qq.redirectUri=http://oauth.xkcoding /demo/oauth/callback/qq

github.clientId=2***************916
github.clientSecret=e4e8cb602c87cf**********************a6f095b
github.redirectUri=http://oauth.xkcoding /demo/oauth/callback/github

mi.clientId=288****************994
mi.clientSecret=nFe****************=
mi.redirectUri=http://oauth.xkcoding /demo/oauth/callback/mi

6. Tăng thêm khởi động loại

/**
* <p>
* khởi động loại
* </p>
*
* @author yangkai.shen
* @date Created in 2019-07-16 14:18
*/
publicclassDemoApplication{
publicstaticvoidmain(String[]args) {
UndertowServer.start(DemoConfig.class,8080,true);
}
}

Enjoy ~

Phỏng vấn địa chỉ:http://localhost:8080/demo/oauth

Releases

No releases published

Packages

No packages published

Languages