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>啟動(dòng)項(xiàng)目訪問 http://localhost:8080/login.html

點(diǎn)擊Gitee登錄 (會跳轉(zhuǎn)到Gitee進(jìn)行登錄,登錄成功后攜帶參數(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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SSO單點(diǎn)登錄系統(tǒng)實(shí)現(xiàn)原理及流程圖解
這篇文章主要介紹了SSO單點(diǎn)登錄系統(tǒng)實(shí)現(xiàn)原理及流程圖解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12
IntelliJ?IDEA?2021.3?正式發(fā)布之支持遠(yuǎn)程開發(fā)、IDE故障排查等多項(xiàng)優(yōu)化改進(jìn)
IntelliJ?IDEA?2021.3?正式發(fā)布:支持遠(yuǎn)程開發(fā)、IDE故障排查等多項(xiàng)優(yōu)化改進(jìn)問題,在這個(gè)版本中的遠(yuǎn)程開發(fā)還不是一個(gè)正式版本,而是BETA版,但通過這個(gè)BETA版本,也可以體驗(yàn)IDEA“遠(yuǎn)程開發(fā)”給我們帶來的全新體驗(yàn)2021-12-12
java實(shí)現(xiàn)雙色球抽獎(jiǎng)算法
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)雙色球抽獎(jiǎng)算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
Java實(shí)現(xiàn)SHA1加密代碼實(shí)例
這篇文章給大家分享了Java實(shí)現(xiàn)SHA1加密的相關(guān)實(shí)例代碼,有興趣的朋友可以測試參考下。2018-07-07
PowerJob的DatabaseMonitorAspect源碼流程
這篇文章主要為大家介紹了PowerJob的DatabaseMonitorAspect源碼流程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
File的API和常用方法詳解_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了File的API和常用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
Spring的UnsatisfiedDependencyException異常的解決
在使用Spring框架開發(fā)應(yīng)用程序時(shí),我們經(jīng)常會遇到各種異常,本文主要介紹了Spring的UnsatisfiedDependencyException異常的解決,感興趣的可以了解一下2023-11-11

