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ày
demo
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ày
demo
Tham khảoHoàn cảnh chuẩn bị
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
<?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>
/**
* <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) {
}
}
/**
* <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);
}
}
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
/**
* <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