Spring Security OAuth 筆記


1  單點登錄

關於單點登錄的原理,我覺得下面這位老哥講的比較清楚,有興趣可以看一下,下面我把其中的重點在此做個筆記總結

https://juejin.cn/post/6844904079274197005

主流的單點登錄都是基於共享 cookie 來實現的

1.1  同域單點登錄

適用場景:都是企業內部系統,所有系統都適用同一個一級域名,並通過不同的二級域名區分

舉個例子:公司有一個一級域名cjs.com,我們有三個系統需要實現單點登錄,分別是門戶系統(sso.cjs.com)、應用系統1(app1.cjs.com)、應用系統2(app2.cjs.com)

核心原理

  1. 門戶系統設置 Cookie 的 domain 為一級域名也就是 cjs.com,這樣就可以共享門戶的 Cookie 給所有的使用該域名(xxx.cjs.com)的系統
  2. 使用 Spring Session 等技術讓所有系統共享 Session
  3. 所有登錄都跳轉到門戶系統去登錄,也就說門戶系統有兩個頁面就夠了:登錄頁(login.html)和首頁(index.html)。通過首頁鏈接可以進入到各子業務系統。
  4. 可以在加一層網關(Spring Cloud Gateway)

1.2  跨域單點登錄

由於域名不一樣不能共享 Cookie 了,這樣就需要通過一個單獨的授權服務(UAA)來做統一登錄,並基於共享UAA的 Cookie 來實現單點登錄。

舉個例子:公司接到一個大項目,把其中部分系統外包給第三方來做,或者直接采購第三方服務商的系統,或者是子業務系統1采購服務商A的系統,子系統2采購B服務商的系統。無論什么情況,總之系統集成就需要單點登錄。

核心原理

  1. 用戶訪問系統1,如果未登錄,則跳轉到UAA系統請求授權,並輸入用戶名/密碼完成登錄
  2. 登錄成功后UAA系統把登錄信息保存到 Session 中,並在瀏覽器寫入域為 sso.com 的 Cookie
  3. 用戶訪問系統2,如未登錄,則跳轉到UAA系統請求授權
  4. 由於是跳轉到UAA系統的域名 sso.com 下,所以能通過瀏覽器中UAA的 Cookie 讀取到 Session 中之前的登錄信息完成單點登錄

1.3  基於OAuth2的跨域單點登錄

1.4  前后端分離的跨域單點登錄

前后端分離的核心概念是后端僅返回前端所需的數據,不再渲染HTML頁面,前端HTML頁面通過AJAX調用后端的RESTFUL API接口並使用JSON數據進行交互

跨域間的前后端分離項目也是基於共享統一授權服務(UAA)的cookie來實現單點登錄的,但是與非前后分離不一樣的是存在以下問題需要解決

  1. 沒有過濾器/攔截器,需要在前端判斷登錄狀態
  2. 需要自己實現oauth2的授權碼模式交互邏輯
  3. 需要解決安全性問題,oauth2的clientSecret參數放在前端不安全

補充:前端獲取授權碼

  • redirect_uri寫前端地址
  • 重定向到前端頁面,頁面獲取到授權碼code,拿code換token

示例參考:

http://localhost:9000/callback.html?code=xxx

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="text/javascript" src="jquery-3.6.0.min.js"></script>
    <script>
    	/**
         * 獲取指定請求參數的值
         * @param name  請求參數名稱
         * @returns {string|null}
         */
        function getQueryParameter(name) {
            let queryString = window.location.search.substring(1);
            let params = queryString.split("&");
            for (let i = 0; i < params.length; i++) {
                let pair = params[i].split("=");
                if (name == pair[0]) {
                    return pair[1];
                }
            }
            return null;
        }
        /**
         * 獲取指定請求參數的值
         * @param name  請求參數名稱
         * @returns {string|null}
         */
        function getUrlParameter(name) {
            let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            let queryString = window.location.search.substring(1);
            let result = queryString.match(reg);
            if (null != result) {
                return unescape(result[2])
            }
            return null;
        }
        let authorizationCode = getUrlParameter("code");
	$.post("http://localhost:8081/auth/oauth/token", {
	    grant_type: "authorization_code",
	    code: authorizationCode,
	    redirect_uri: "http://localhost:9000/callback.html"
	}, function (resp) {
	    console.log(resp);
	    sessionStorage.setItem("token", resp.access_token);
	});
    </script>
</head>
<body>

</body>
</html>

2  Spring Security OAuth 2.0遷移指南

從 Spring Security 5.2.x 開始,OAuth 2.0 Clients 和 Resource Servers 已經從 Security OAuth 2.x 遷移到 從 Spring Security,而且 Spring Security 不再提供 Authorization Server 的支持。

總之呢,Spring Security OAuth這個項目以后就處於維護狀態了,不會再更新了,建議使用Spring Security

遷移以后,很多地方都不一樣了,就我注意到的說下幾點變化

首先,以前單點登錄使用@EnableOAuth2Sso注解,現在推薦使用oauth2Login()方法

其次,授權服務器的寫法不一樣了

默認的端點都變成 /oauth2 開頭了

更多變化可以閱讀源碼,亦可參見 OAuth 2.0 Features Matrix 查看二者支持的特性

3  @EnableOAuth2Sso的作用

@EnableOAuth2Sso: 標記服務作為一個OAuth 2.0客戶端。這意味着它將負責將資源所有者(最終用戶)重定向到授權服務器,在那里用戶必須輸入他們的憑據。完成后,用戶被重定向回客戶端,並攜帶授權碼。然后,客戶端獲取授權碼,並通過調用授權服務器以獲取訪問令牌。只有在此之后,客戶端才能使用訪問令牌調用資源服務器。

4  補充:根據pid遞歸查找子機構


package com.soa.supervision.gateway.service.impl;

import com.alibaba.fastjson.JSON;
import com.soa.supervision.gateway.entity.SysDept;
import com.soa.supervision.gateway.repository.SysDeptRepository;
import com.soa.supervision.gateway.service.SysDeptService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 機構表 服務實現類
 *
 * @author ChengJianSheng
 * @since 2022-03-08
 */
@Service
public class SysDeptServiceImpl implements SysDeptService {
    private static final String CACHE_PREFIX = "DEPT:";
    @Resource
    private SysDeptRepository sysDeptRepository;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 遞歸 向下查找當前機構的所有子機構
     */
    @Override
    public List
  
  
  
          
            getAllByPid(Integer pid, List 
           
             list) { List 
            
              subDeptIdList = sysDeptRepository.findIdByPid(pid); if (CollectionUtils.isEmpty(subDeptIdList)) { return new ArrayList<>(); } else { list.addAll(subDeptIdList); subDeptIdList.forEach(e->{ getAllByPid(e, list); }); } return list; } @Override public String getSubDeptIdListByPid(Integer pid) { String key = CACHE_PREFIX + pid; String val = stringRedisTemplate.opsForValue().get(key); if (StringUtils.isBlank(val)) { synchronized (SysDept.class) { if (StringUtils.isBlank(val)) { List 
             
               deptIds = getAllByPid(pid, new ArrayList<>()); deptIds.add(pid); val = JSON.toJSONString(deptIds); stringRedisTemplate.opsForValue().set(key, val, 1, TimeUnit.HOURS); } } } return val; } } package com.soa.supervision.gateway.repository; import com.soa.supervision.gateway.entity.SysDept; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import java.util.List; /** * @Author ChengJianSheng * @Date 2022/3/8 */ public interface SysDeptRepository extends JpaRepository 
              
                { @Query(value = "SELECT id FROM sys_dept WHERE pid = :pid", nativeQuery = true) List 
               
                 findIdByPid(@Param("pid") Integer pid); } 
                
               
              
             
            
          

5  有用的文檔

Spring Security相關

Spring Boot OAuth相關


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM