JustAuth-第三方Oauth2登錄方式
JustAuth-第三方Oauth2登錄
JustAuth官網(wǎng): https://www.justauth.cn/
JustAuth整合Springboot
1.引入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.7.15</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.26</version>
</dependency>
<dependency>
<groupId>me.zhyd.oauth</groupId>
<artifactId>JustAuth</artifactId>
<version>1.16.6</version>
</dependency>2.Controller
(到Gitee或GitHub上申請第三方授權(quán),修改為自己的clientId、clientSecret)。
這里url中的 {source} 是為了接口和方法的復(fù)用。
@RestController
@RequestMapping("/oauth")
public class OauthController {
@RequestMapping("/{source}")
public void renderAuth(@PathVariable("source") String source,HttpServletResponse response) throws IOException {
AuthRequest authRequest = getAuthRequest(source);
response.sendRedirect(authRequest.authorize(AuthStateUtils.createState()));
}
@RequestMapping("/callback")
public String login(@RequestParam("source") String source, AuthCallback callback) {
AuthRequest authRequest = getAuthRequest(source);
// 返回用戶的基本信息
AuthResponse authResponse = authRequest.login(callback);
// 返回Token
return UUID.randomUUID().toString();
}
private AuthRequest getAuthRequest(String source) {
if ("gitee".equals(source)) {
return new AuthGiteeRequest(AuthConfig.builder()
.clientId("***************************")
.clientSecret("****************************")
// 回調(diào)地址與Gitee上注冊的保持一致
.redirectUri("http://127.0.0.1:8080/callback.html?source=gitee")
.build());
}else if ("github".equals(source)){
return new AuthGithubRequest(AuthConfig.builder()
.clientId("**********")
.clientSecret("*********")
// 回調(diào)地址與Github上注冊的保持一致
.redirectUri("http://127.0.0.1:8080/callback.html?source=github")
.build());
}
return null;
}
}3.登陸頁面 login.html
(放在resources/static/index.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="http://localhost:8080/oauth/gitee" rel="external nofollow" >Gitee登錄</a>
<a href="http://localhost:8080/oauth/github" rel="external nofollow" >Github登錄</a>
</body>
</html>4.回調(diào)頁面 callback.html
(放在resources/static/callback.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>callback</title>
<style>
.overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
z-index: 9999;
display: flex;
justify-content: center;
align-items: center;
}
.loader {
font-size: 24px;
color: white;
}
</style>
</head>
<body>
<div id="loading-overlay" class="overlay">
<div class="loader">Loading</div>
</div>
<script>
window.onload = async function () {
showLoadingOverlay()
// 獲取 source、code、state參數(shù)發(fā)起fetch請求
const params = new URLSearchParams(window.location.search);
// 發(fā)起請求
try {
const res = await fetch("/oauth/callback", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: params.toString(),
}).then(res => res.text())
localStorage.setItem("token", res)
location.href = "/index.html"
} finally {
hideLoadingOverlay()
}
}
// 顯示遮罩
function showLoadingOverlay() {
document.getElementById("loading-overlay").style.display = "flex";
}
// 隱藏遮罩
function hideLoadingOverlay() {
document.getElementById("loading-overlay").style.display = "none";
}
</script>
</body>
</html>5.登錄成功后跳轉(zhuǎn)首頁 index.html
(放在resources/static/index.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首頁</title>
</head>
<body>
<div>首頁</div>
</body>
</html>啟動項目訪問 http://localhost:8080/login.html

點擊Gitee登錄 (會跳轉(zhuǎn)到Gitee進行登錄,登錄成功后攜帶參數(shù)重定向到回調(diào)頁面,如果Gitee已經(jīng)登陸,則直接攜帶參數(shù)重定向到回調(diào)頁面)。
callback.html 掛載后,會攜帶url參數(shù),發(fā)起請求,請求結(jié)束之后保存返回的token,并跳轉(zhuǎn)到index.html。
Gitee的回調(diào)URL:
http://127.0.0.1:8080/callback.html?source=gitee&code=19c26e280bc9a83de9df6c9698b802a61e210e4fce67b0867b8166eef990c053&state=f40f8a38c9dfed67ee912960016f8f69

index.html

總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot 集成Shiro的多realm實現(xiàn)以及shiro基本入門教程
這篇文章主要介紹了Spring Boot 集成Shiro的多realm實現(xiàn)以及shiro基本入門,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10
JavaMail實現(xiàn)發(fā)送郵件(QQ郵箱)
這篇文章主要為大家詳細介紹了JavaMail實現(xiàn)發(fā)送郵件(QQ郵箱),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-08-08
mybatis 自定義實現(xiàn)攔截器插件Interceptor示例
這篇文章主要介紹了mybatis 自定義實現(xiàn)攔截器插件Interceptor,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
利用Java實現(xiàn)讀取WPS?Excel中嵌入的圖片
許多數(shù)據(jù)文件中可能包含嵌入式圖片,這些圖片對于數(shù)據(jù)分析和可視化非常重要,下面我們就來看看如何使用Java讀取WPS?Excel中嵌入的圖片吧2024-11-11
如何利用Java輸出鏈表中倒數(shù)第k個結(jié)點
這篇文章主要給大家介紹了關(guān)于如何利用Java輸出鏈表中倒數(shù)第k個結(jié)點的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2021-12-12
nacos服務(wù)無法注冊到nacos服務(wù)中心問題及解決
本文詳細描述了在Linux服務(wù)器上使用Tomcat啟動Java程序時,服務(wù)無法注冊到Nacos的排查過程,通過一系列排查步驟,發(fā)現(xiàn)問題出在Tomcat的啟動機制上,導(dǎo)致無法自動觸發(fā)服務(wù)注冊事件,最終,通過實現(xiàn)`ApplicationRunner`接口,手動觸發(fā)服務(wù)注冊事件,解決了問題2025-11-11

