国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Spring Boot整合OAuth2實現(xiàn)GitHub第三方登錄

這篇具有很好參考價值的文章主要介紹了Spring Boot整合OAuth2實現(xiàn)GitHub第三方登錄。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

Github OAuth 第三方登錄示例

1、第三方登錄原理

第三方登錄的原理是借助OAuth授權(quán)來實現(xiàn),首先用戶先向客戶端提供第三方網(wǎng)站的數(shù)據(jù)證明自己的身份獲取授權(quán)碼,然后客戶端拿著授權(quán)碼與授權(quán)服務(wù)器建立連接獲得一個Access Token,之后客戶端就可以通過Access Token來與資源服務(wù)器進行交互。

使用OAuth的好處是提供給用戶一個特定的密鑰,用戶持有這個密鑰可以訪問應(yīng)用中的任何信息,而不需要向網(wǎng)站提供用戶名&密碼,可以實現(xiàn)跨系統(tǒng)共享用戶授權(quán)協(xié)議。

通過控制用戶持有的密鑰,可以很方便的控制用戶可以訪問的資源,以及控制密鑰的過期時間。

以下是來自維基百科對于OAuth的介紹

開放授權(quán)(OAuth)是一個開放標準,允許用戶讓第三方應(yīng)用訪問該用戶在某一網(wǎng)站上存儲的私密的資源(如照片,視頻,聯(lián)系人列表),而無需將用戶名和密碼提供給第三方應(yīng)用。

OAuth允許用戶提供一個令牌,而不是用戶名和密碼來訪問他們存放在特定服務(wù)提供者的數(shù)據(jù)。每一個令牌授權(quán)一個特定的網(wǎng)站(例如,視頻編輯網(wǎng)站)在特定的時段(例如,接下來的2小時內(nèi))內(nèi)訪問特定的資源(例如僅僅是某一相冊中的視頻)。這樣,OAuth讓用戶可以授權(quán)第三方網(wǎng)站訪問他們存儲在另外服務(wù)提供者的某些特定信息,而非所有內(nèi)容。

OAuth是OpenID的一個補充,但是完全不同的服務(wù)。

交互流程如下:

2、GitHub實現(xiàn)第三方登錄

首先需要在github中對應(yīng)用進行登記,讓Github知道誰在發(fā)送請求。

訪問這個網(wǎng)址,填寫登記表

提交成功之后,GitHub會返回Client ID & Client Secrets ,這是應(yīng)用的身份識別碼

創(chuàng)建一個SpringBoot工程,pom.xml文件內(nèi)容如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.17</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>org.pp</groupId>
    <artifactId>springboot-oauth2-api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-oauth2-api</name>
    <description>springboot整合oauth2,實現(xiàn)GitHub第三方登錄</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

將ID和密鑰添加到配置文件application.yml中:

# 項目端口號
server:
  port: 8080
#  GitHub認證相關(guān)參數(shù)
github:
    client:
      id: xxx
      secret: xxx

創(chuàng)建一個實體類,用于映射授權(quán)成功產(chǎn)生的Token令牌:

import com.fasterxml.jackson.annotation.JsonProperty;
/**
 *
 * Token令牌 - 響應(yīng)參數(shù)
 *
 * @author supanpan
 * @date 2023/10/25
 */
public class AccessTokenResponse {

    @JsonProperty("access_token")
    private String accessToken;

    public String getAccessToken() {
        return accessToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }
}

OAuthController如下:

**
 * @author supanpan
 * @date 2023/10/25
 */
@Controller
public class OAuthController {

    @Value("${github.client.id}")
    private String clientId;

    @Value("${github.client.secret}")
    private String clientSecret;

    @GetMapping("/oauth/redirect")
    public String handleRedirect(@RequestParam("code") String requestToken, Model model) {
        // 使用RestTemplate來發(fā)送HTTP請求
        RestTemplate restTemplate = new RestTemplate();

        // 獲取Token的Url
        String tokenUrl = "https://github.com/login/oauth/access_token" +
                "?client_id=" + clientId +
                "&client_secret=" + clientSecret +
                "&code=" + requestToken;

        // 使用restTemplate向GitHub發(fā)送請求,獲取Token
        AccessTokenResponse tokenResponse = restTemplate.postForObject(tokenUrl, null, AccessTokenResponse.class);

        // 從響應(yīng)體中獲取Token數(shù)據(jù)
        String accessToken = tokenResponse.getAccessToken();

        // 攜帶Token向GitHub發(fā)送請求
        String apiUrl = "https://api.github.com/user";
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "token " + accessToken);
        HttpEntity<String> entity = new HttpEntity<>("parameters", headers);
        ResponseEntity<String> response = restTemplate.exchange(apiUrl, HttpMethod.GET, entity, String.class);
        model.addAttribute("userData", response.getBody());

        return "welcome";
    }
}

SpringBoot啟動器

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootOauth2ApiApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootOauth2ApiApplication.class, args);
    }

}

還需要編寫兩個html頁面,index.html和welcome.html

index.html

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>OAuth2 Demo</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>
<a id="login">Login with GitHub</a>

<script>
    const client_id = 'xxxx';

    const authorize_uri = 'https://github.com/login/oauth/authorize';
    const redirect_uri = 'http://localhost:8080/oauth/redirect';

    const link = document.getElementById('login');
    link.href = `${authorize_uri}?client_id=${client_id}&redirect_uri=${redirect_uri}`;
</script>

</body>

</html>

welcome.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Hello</title>
</head>

<body>
    <h1>Welcome</h1>
    <div th:text="${userData}"></div>
</body>

</html>

啟動項目,瀏覽器訪問localhost:8080,會跳轉(zhuǎn)到index頁面,點擊鏈接會跳轉(zhuǎn)到GitHub應(yīng)用授權(quán)頁面

點擊跳轉(zhuǎn)到GitHub授權(quán)之后,GitHub會詢問示例代碼正在請求數(shù)據(jù),您是否同意授權(quán)。

用戶同意授權(quán), GitHub 就會跳轉(zhuǎn)到redirect_uri指定的跳轉(zhuǎn)網(wǎng)址,并且?guī)鲜跈?quán)碼,跳轉(zhuǎn)回來的 URL 就是下面的樣子

// code參數(shù)就是授權(quán)碼
http://localhost:8080/oauth/redirect?code:4ea423f2ec1e04c6376a

如下是服務(wù)的響應(yīng)數(shù)據(jù):

access token: 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
{
  login: 'AtwoodPa',
  id: 110728122,
  node_id: 'U_kgDOBpmTug',
  avatar_url: 'https://avatars.githubusercontent.com/u/110728122?v=4',
  gravatar_id: '',
  url: 'https://api.github.com/users/AtwoodPa',
  html_url: 'https://github.com/AtwoodPa',
  followers_url: 'https://api.github.com/users/AtwoodPa/followers',
  following_url: 'https://api.github.com/users/AtwoodPa/following{/other_user}',
  gists_url: 'https://api.github.com/users/AtwoodPa/gists{/gist_id}',
  starred_url: 'https://api.github.com/users/AtwoodPa/starred{/owner}{/repo}',
  subscriptions_url: 'https://api.github.com/users/AtwoodPa/subscriptions',
  organizations_url: 'https://api.github.com/users/AtwoodPa/orgs',
  repos_url: 'https://api.github.com/users/AtwoodPa/repos',
  events_url: 'https://api.github.com/users/AtwoodPa/events{/privacy}',
  received_events_url: 'https://api.github.com/users/AtwoodPa/received_events',
  type: 'User',
  site_admin: false,
  name: null,
  company: null,
  blog: '',
  location: null,
  email: null,
  hireable: null,
  bio: null,
  twitter_username: null,
  public_repos: 6,
  public_gists: 0,
  followers: 0,
  following: 3,
  created_at: '2022-08-06T13:02:16Z',
  updated_at: '2023-09-03T00:15:55Z'
}
authorization code: 4ea423f2ec1e04c6376a

成功執(zhí)行上述流程,最終展示示例的welcome頁面

到這里,Spring Boot整合GitHub實現(xiàn)第三方登錄的實現(xiàn)就結(jié)束了,以此類推其他廠商的第三方登錄實現(xiàn)流程也大概是這樣。文章來源地址http://www.zghlxwxcb.cn/news/detail-711405.html

到了這里,關(guān)于Spring Boot整合OAuth2實現(xiàn)GitHub第三方登錄的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • Spring Boot 最新版3.x 集成 OAuth 2.0實現(xiàn)認證授權(quán)服務(wù)、第三方應(yīng)用客戶端以及資源服務(wù)

    Spring Boot 最新版3.x 集成 OAuth 2.0實現(xiàn)認證授權(quán)服務(wù)、第三方應(yīng)用客戶端以及資源服務(wù)

    Spring Boot 3 已經(jīng)發(fā)布一段時間,網(wǎng)上關(guān)于 Spring Boot 3 的資料不是很多,本著對新技術(shù)的熱情,學(xué)習和研究了大量 Spring Boot 3 新功能和新特性,感興趣的同學(xué)可以參考 Spring 官方資料全面詳細的新功能/新改進介紹 Spring 版本升級到6.x JDK版本至少17+ … 新特性有很多,本文主要針對

    2024年02月02日
    瀏覽(97)
  • OAuth 2.0 協(xié)議介紹【實現(xiàn) GitHub 第三方登錄】

    OAuth 2.0 協(xié)議介紹【實現(xiàn) GitHub 第三方登錄】

    OAuth(是 Open Authorization 開放授權(quán)的縮寫),在全世界得到廣泛應(yīng)用,目前的版本是2.0版。 本文會對OAuth 2.0的設(shè)計思路和運行流程,做一個簡明通俗的解釋,主要參考材料為RFC 6749。 OAuth 2.0 是一個開放標準,用于授權(quán)用戶訪問另一個應(yīng)用程序的資源,而無需將用戶的憑據(jù)(比如

    2024年02月20日
    瀏覽(27)
  • 【Oauth2.0 單點登錄 + 第三方授權(quán)認證】用戶認證、授權(quán)模式

    【Oauth2.0 單點登錄 + 第三方授權(quán)認證】用戶認證、授權(quán)模式

    本文主要對 SpringSecurity Oauth 2.0 用戶認證,授權(quán)碼授權(quán)模式、密碼授權(quán)模式,以及授權(quán)流程進行講解 開發(fā)中,有些功能只有管理員才有,普通用戶是沒有的。即需要對用戶的身份進行認證,是管理員還是普通用戶。認證方式有兩種: 身份認證: 用戶在訪問相應(yīng)資源時對用戶

    2023年04月08日
    瀏覽(25)
  • spring boot整合第三方微信開發(fā)工具 weixin-java-miniapp 實現(xiàn)小程序微信登錄

    spring boot整合第三方微信開發(fā)工具 weixin-java-miniapp 實現(xiàn)小程序微信登錄

    有時候項目需要用到微信登錄或獲取用戶的手機號碼,weixin-java-miniapp是一個好用的第三方工具,不用我們自己寫httpcline調(diào)用。 導(dǎo)入jar包 添加一個resource.properties文件,寫上小程序的appid和secret 添加兩個配置文件 WxMaProperties.java WxMaConfiguration.java 如何使用 小程序給微信發(fā)送消息

    2024年02月16日
    瀏覽(94)
  • Spring Cloud Gateway 整合OAuth2.0 實現(xiàn)統(tǒng)一認證授權(quán)

    Spring Cloud Gateway 整合OAuth2.0 實現(xiàn)統(tǒng)一認證授權(quán) GateWay——向其他服務(wù)傳遞參數(shù)數(shù)據(jù) https://blog.csdn.net/qq_38322527/article/details/126530849 @EnableAuthorizationServer Oauth2ServerConfig 驗證簽名 網(wǎng)關(guān)服務(wù)需要RSA的公鑰來驗證簽名是否合法,所以認證服務(wù)需要有個接口把公鑰暴露出來 接下來搭建網(wǎng)

    2024年02月13日
    瀏覽(23)
  • SpringCloud整合spring security+ oauth2+Redis實現(xiàn)認證授權(quán)

    SpringCloud整合spring security+ oauth2+Redis實現(xiàn)認證授權(quán)

    在微服務(wù)構(gòu)建中,我們一般用一個父工程來通知管理依賴的各種版本號信息。父工程pom文件如下: 在SpringCloud微服務(wù)體系中服務(wù)注冊中心是一個必要的存在,通過注冊中心提供服務(wù)的注冊和發(fā)現(xiàn)。具體細節(jié)可以查看我之前的博客,這里不再贅述。我們開始構(gòu)建一個eureka注冊中

    2024年02月06日
    瀏覽(25)
  • Spring Boot 中如何使用 Spring Security OAuth2 來實現(xiàn)單點登錄

    Spring Boot 中如何使用 Spring Security OAuth2 來實現(xiàn)單點登錄

    在現(xiàn)代 Web 應(yīng)用程序中,單點登錄(Single Sign-On,簡稱 SSO)是一個非常重要的功能。Spring Security OAuth2 是 Spring Security 框架的一個擴展,它提供了一種簡單的方式來實現(xiàn) SSO。在本文中,我們將介紹如何在 Spring Boot 應(yīng)用程序中使用 Spring Security OAuth2 來實現(xiàn)單點登錄。 在開始之前

    2024年02月06日
    瀏覽(24)
  • Spring Authorization Server 1.1 擴展實現(xiàn) OAuth2 密碼模式與 Spring Cloud 的整合實戰(zhàn)

    Spring Authorization Server 1.1 擴展實現(xiàn) OAuth2 密碼模式與 Spring Cloud 的整合實戰(zhàn)

    項目源碼 :youlai-mall 通過 Spring Cloud Gateway 訪問認證中心進行認證并獲取得到訪問令牌。 再根據(jù)訪問令牌 access_token 獲取當前登錄的用戶信息。 Spring Security OAuth2 的最終版本是2.5.2,并于2022年6月5日正式宣布停止維護。Spring 官方為此推出了新的替代產(chǎn)品,即 Spring Authorization

    2024年02月04日
    瀏覽(25)
  • SpringCloud微服務(wù)整合Spring Security OAuth2

    SpringCloud微服務(wù)整合Spring Security OAuth2

    首先得了解什么是OAuth2,這個的話可以參見博客: https://blog.csdn.net/weixin_42272869/article/details/112260123 https://www.bilibili.com/video/BV1D94y1Z7t1?p=33vd_source=bf9d70f3d2a451db07f40b6407c95a77 本文采用的是使用最廣泛的、安全性最高的 授權(quán)碼模式 進行講解。 單獨創(chuàng)建一個鑒權(quán)微服務(wù)auth,負責整個

    2024年02月09日
    瀏覽(41)
  • spring-security -oauth2 整合 JWT

    spring-security -oauth2 整合 JWT

    在這個基礎(chǔ)上,進行整合。 spring security oauth2學(xué)習 -- 快速入門_本郡主是喵的博客-CSDN博客 先把? reids,common-pools? 等依賴刪掉。 刪掉redis的下相關(guān)配置 1.1?導(dǎo)入依賴 1.2 核心代碼 創(chuàng)建 jwtTokenConfig.java 在 AuthenticationServer.java 里面新增這些。 ?運行,啟動! ?復(fù)制這個token去官網(wǎng)解析

    2024年02月09日
    瀏覽(23)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包