溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何實(shí)現(xiàn)易水公共組件的SSO功能

發(fā)布時(shí)間:2021-10-09 14:01:06 來源:億速云 閱讀:146 作者:柒染 欄目:大數(shù)據(jù)

如何實(shí)現(xiàn)易水公共組件的SSO功能,相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

如今,隨著互聯(lián)網(wǎng)技術(shù)的發(fā)展,網(wǎng)絡(luò)用戶規(guī)模越來越大,假如公司的每一個(gè)應(yīng)用都建立一個(gè)用戶系統(tǒng),不僅極大的增加了開發(fā)的工作量,而且容易形成了信息孤島,用戶在使用公司的每個(gè)產(chǎn)品時(shí)都需要重復(fù)注冊一次。因此許多公司為了統(tǒng)一管理,建立了統(tǒng)一認(rèn)證中心,其他的應(yīng)用需要通過單點(diǎn)登錄即可獲取用戶信息,用戶登錄該公司的其他應(yīng)用時(shí)也不需要在重新注冊,大大節(jié)省公司用戶導(dǎo)入成本,也提高用戶使用體驗(yàn)。

對(duì)于單點(diǎn)登錄(SSO)而言,現(xiàn)在技術(shù)比較成熟,網(wǎng)上各種教程也比較多,但是很多文章都講述的不怎么全面,在這里基于spring security簡單明了的介紹下時(shí)如何實(shí)現(xiàn)單點(diǎn)登錄功能。

在使用單點(diǎn)登錄之前,需要先搭建一個(gè)認(rèn)證中心,例如通過易水公共組件快速搭建一個(gè)自己的認(rèn)證中心,也可以使用現(xiàn)在網(wǎng)絡(luò)上現(xiàn)成的認(rèn)證中心,如 谷歌 或 github.

一 在pom文件中加入依賴

完整的pom文件內(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.2.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.yishuifengxiao.sso-client</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-oauth3-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
      

<!--        <dependency>-->
<!--            <groupId>org.springframework.security.oauth.boot</groupId>-->
<!--            <artifactId>spring-security-oauth3-autoconfigure</artifactId>-->
<!--            <version>2.2.0.RELEASE</version>-->
<!--        </dependency>-->

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

二 加入配置信息

在配置文件中加入以下信息

spring.security.oauth3.client.registration.yishui.provider=yishui
# 登錄界面上顯示的登錄類型的名字,可不填
spring.security.oauth3.client.registration.yishui.client-name=custom
# 該用戶對(duì)應(yīng)的clientId
spring.security.oauth3.client.registration.yishui.client-id=zhiyubujian
# 該用戶對(duì)應(yīng)的clientSecret
spring.security.oauth3.client.registration.yishui.client-secret=zhiyubujian
# 這里是定值為authorization_code,表示使用授權(quán)碼模式模式從授權(quán)服務(wù)器中獲取token
spring.security.oauth3.client.registration.yishui.authorization-grant-type=authorization_code
# 回調(diào)地址,應(yīng)該和授權(quán)服務(wù)器中登記的回調(diào)地址一模一樣,否則會(huì)出錯(cuò),支持通配符,也可以想下面那樣配置為完整的地址
spring.security.oauth3.client.registration.yishui.redirect-uri={baseUrl}/login/oauth3/code/{registrationId}
#spring.security.oauth3.client.registration.yishui.redirect-uri=http://192.168.0.172:8006/oauth3/code

# 授權(quán)服務(wù)器的authorization地址
spring.security.oauth3.client.provider.yishui.authorization-uri=http://192.168.0.172:8000/oauth/authorize
# 授權(quán)服務(wù)器獲取token的地址
spring.security.oauth3.client.provider.yishui.token-uri=http://192.168.0.172:8000/oauth/token
# 授權(quán)服務(wù)器中獲取登錄用戶信息的地址
spring.security.oauth3.client.provider.yishui.user-info-uri=http://192.168.0.172:8000/me
#定值,必須配置,否則會(huì)出錯(cuò)
spring.security.oauth3.client.provider.yishui.user-name-attribute=userAuthentication

注意:

上述配置中的yishui可以為任意值,但是一般最好為有標(biāo)識(shí)意義的值,例如使用google賬號(hào)登錄時(shí),可以進(jìn)行如下配置:

spring:
  security:
    oauth3:
      client:
        registration:
          google:
            client-id: google-client-id
            client-secret: google-client-secret

三 配置啟動(dòng)文件

@SpringBootApplication
@RestController
//@EnableOAuth3Sso
public class DemoApplication {

    @GetMapping("/me")
    public Authentication user(Authentication authentication) {

        return authentication;
    }


    @GetMapping("/")
    public String index(Model model,
                        @RegisteredOAuth3AuthorizedClient OAuth3AuthorizedClient authorizedClient,
                        @AuthenticationPrincipal OAuth3User oauth3User) {
        model.addAttribute("userName", oauth3User.getName());
        model.addAttribute("clientName", authorizedClient.getClientRegistration().getClientName());
        model.addAttribute("userAttributes", oauth3User.getAttributes());

        return "index";
    }

    public static void main(String[] args) {

        SpringApplication.run(DemoApplication.class, args);

    }

}

四 使用單點(diǎn)登錄

完成上述配置以后,我們就可以使用單點(diǎn)登錄功能了。

例如當(dāng)我們想要訪問 http://localhost:8080/me 時(shí),系統(tǒng)會(huì)重定向到到默認(rèn)的登錄頁面 如何實(shí)現(xiàn)易水公共組件的SSO功能

注意:這里顯示的是默認(rèn)的登錄頁面,如果用戶想要顯示自定義登錄界面,可以參見 易水公共組件里面的相關(guān)章節(jié)進(jìn)行修改和美化。

點(diǎn)擊上面的登錄鏈接,會(huì)跳轉(zhuǎn)到授權(quán)服務(wù)器的登錄頁面

如何實(shí)現(xiàn)易水公共組件的SSO功能

在此頁面輸入用戶名和密碼登錄成功后,請求會(huì)跳轉(zhuǎn)到原始等請求地址

響應(yīng)結(jié)果如下:

如何實(shí)現(xiàn)易水公共組件的SSO功能

看完上述內(nèi)容,你們掌握如何實(shí)現(xiàn)易水公共組件的SSO功能的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

sso
AI