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上申請(qǐng)第三方授權(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上注冊(cè)的保持一致
.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上注冊(cè)的保持一致
.redirectUri("http://127.0.0.1:8080/callback.html?source=github")
.build());
}
return null;
}
}3.登陸頁(yè)面 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)頁(yè)面 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請(qǐng)求
const params = new URLSearchParams(window.location.search);
// 發(fā)起請(qǐng)求
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)首頁(yè) index.html
(放在resources/static/index.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首頁(yè)</title>
</head>
<body>
<div>首頁(yè)</div>
</body>
</html>啟動(dòng)項(xiàng)目訪問(wèn) http://localhost:8080/login.html

點(diǎn)擊Gitee登錄 (會(huì)跳轉(zhuǎn)到Gitee進(jìn)行登錄,登錄成功后攜帶參數(shù)重定向到回調(diào)頁(yè)面,如果Gitee已經(jīng)登陸,則直接攜帶參數(shù)重定向到回調(diào)頁(yè)面)。
callback.html 掛載后,會(huì)攜帶url參數(shù),發(fā)起請(qǐng)求,請(qǐng)求結(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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot 集成Shiro的多realm實(shí)現(xiàn)以及shiro基本入門教程
這篇文章主要介紹了Spring Boot 集成Shiro的多realm實(shí)現(xiàn)以及shiro基本入門,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
Java代碼實(shí)現(xiàn)矩形覆蓋實(shí)例
這篇文章主要介紹了Java代碼實(shí)現(xiàn)矩形覆蓋實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,,需要的朋友可以參考下2019-06-06
JavaMail實(shí)現(xiàn)發(fā)送郵件(QQ郵箱)
這篇文章主要為大家詳細(xì)介紹了JavaMail實(shí)現(xiàn)發(fā)送郵件(QQ郵箱),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
mybatis 自定義實(shí)現(xiàn)攔截器插件Interceptor示例
這篇文章主要介紹了mybatis 自定義實(shí)現(xiàn)攔截器插件Interceptor,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
利用Java實(shí)現(xiàn)讀取WPS?Excel中嵌入的圖片
許多數(shù)據(jù)文件中可能包含嵌入式圖片,這些圖片對(duì)于數(shù)據(jù)分析和可視化非常重要,下面我們就來(lái)看看如何使用Java讀取WPS?Excel中嵌入的圖片吧2024-11-11
如何利用Java輸出鏈表中倒數(shù)第k個(gè)結(jié)點(diǎn)
這篇文章主要給大家介紹了關(guān)于如何利用Java輸出鏈表中倒數(shù)第k個(gè)結(jié)點(diǎn)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-12-12
nacos服務(wù)無(wú)法注冊(cè)到nacos服務(wù)中心問(wèn)題及解決
本文詳細(xì)描述了在Linux服務(wù)器上使用Tomcat啟動(dòng)Java程序時(shí),服務(wù)無(wú)法注冊(cè)到Nacos的排查過(guò)程,通過(guò)一系列排查步驟,發(fā)現(xiàn)問(wèn)題出在Tomcat的啟動(dòng)機(jī)制上,導(dǎo)致無(wú)法自動(dòng)觸發(fā)服務(wù)注冊(cè)事件,最終,通過(guò)實(shí)現(xiàn)`ApplicationRunner`接口,手動(dòng)觸發(fā)服務(wù)注冊(cè)事件,解決了問(wèn)題2025-11-11

