基于SpringBoot和OAuth2,實現通過Github授權登錄應用
文章目錄
- 基于SpringBoot和OAuth2,實現通過Github授權登錄應用
- 0. 引言
- 1. 創建Github應用
- 2. 創建SpringBoot測試項目
- 2.1 初始化項目
- 2.2 設置配置文件信息
- 2.3 創建Controller層
- 2.4 創建Html頁面
- 3. 啟動應用
- 4. 其他
0. 引言
在注冊登錄網站或者應用時,通常會有社交方式登錄,例如在登錄CSDN時,會提供多種登陸方式,如下圖。
本文介紹通過SpringBoot和OAuth2,開發自己的應用,并實現通過Github授權登錄。
1. 創建Github應用
- 首先登錄Github,進入到
Settings-Developer Settings
,點擊OAuth Apps
,新建New OAuth App
- 填寫相關信息
點擊注冊應用
- 注冊完成后打開,可以獲得
Client ID
和Client secrets
注意!
Client secrets要注意復制下來保存,不然在進入這個頁面,也獲取不到原來完整的Client secrets了,只能重新生成!
2. 創建SpringBoot測試項目
2.1 初始化項目
初始化項目,同時應包含以下依賴
Spring Web
Thymeleaf
Spring Security
OAuth2 Client
創建完成后,創建Controller
文件和index
文件。最終項目結構目錄如下:
2.2 設置配置文件信息
application.yml:
spring:security:oauth2:client:registration:github:client-id: xxxclient-secret: xxx
將上面生成的client-id和client-secret寫入配置文件
2.3 創建Controller層
IndexController.java
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;/*** @author SaoE* @date 2024/12/29 21:29*/
@Controller
public class IndexController {@GetMapping("/")public String index(Model model,@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authorizedClient,@AuthenticationPrincipal OAuth2User oauth2User) {model.addAttribute("userName", oauth2User.getName());model.addAttribute("clientName", authorizedClient.getClientRegistration().getClientName());model.addAttribute("userAttributes", oauth2User.getAttributes());return "index";}
}
2.4 創建Html頁面
resources/templates/index.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org" xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head><title>Spring Security - OAuth 2.0 Login</title><meta charset="utf-8" />
</head>
<body>
<div style="float: right" th:fragment="logout" sec:authorize="isAuthenticated()"><div style="float:left"><span style="font-weight:bold">User: </span><span sec:authentication="name"></span></div><div style="float:none"> </div><div style="float:right"><form action="#" th:action="@{/logout}" method="post"><input type="submit" value="Logout" /></form></div>
</div>
<h1>OAuth 2.0 Login with Spring Security</h1>
<div>You are successfully logged in <span style="font-weight:bold" th:text="${userName}"></span>via the OAuth 2.0 Client <span style="font-weight:bold" th:text="${clientName}"></span>
</div>
<div> </div>
<div><span style="font-weight:bold">User Attributes:</span><ul><li th:each="userAttribute : ${userAttributes}"><span style="font-weight:bold" th:text="${userAttribute.key}"></span>: <span th:text="${userAttribute.value}"></span></li></ul>
</div>
</body>
</html>
3. 啟動應用
- 在瀏覽器輸入并訪問
http://localhost:8080/
,此時瀏覽器將被重定向到默認的自動生成的登錄頁面,該頁面顯示了一個用于GitHub登錄的鏈接。
點擊授權
- 此時,OAuth客戶端訪問GitHub的獲取用戶信息的接口獲取基本個人資料信息,并建立一個已認證的會話。
4. 其他
在SpringBoot
源碼的CommonOAuth2Provider
類中,默認配置了GOOGLE
、FACEBOOK
、GITHUB
和OKTA
的授權登錄配置
以Github為例,默認配置如下:
GITHUB {public Builder getBuilder(String registrationId) {Builder builder = this.getBuilder(registrationId, ClientAuthenticationMethod.CLIENT_SECRET_BASIC, "{baseUrl}/{action}/oauth2/code/{registrationId}");builder.scope(new String[]{"read:user"});builder.authorizationUri("https://github.com/login/oauth/authorize");builder.tokenUri("https://github.com/login/oauth/access_token");builder.userInfoUri("https://api.github.com/user");builder.userNameAttributeName("id");builder.clientName("GitHub");return builder;}},