簡體   English   中英

如何在應用程序位於 SSL 終止代理之后時配置 Spring 引導以使用 OIDC

[英]How to configure Spring Boot to use OIDC while app is behind an SSL termination proxy

我正在嘗試配置 Spring 引導應用程序以使用 OIDC。 服務器位於 SSL 終止代理后面。

以下是我使用的屬性:

spring:
  security:
    oauth2:
      client:
        provider:
          oidc:
            authorization-uri: https://example.org/oidc/oauth2/authorize
            token-uri: https://example.org/oidc/oauth2/access_token
            user-info-uri: https://example.org/oidc/oauth2/userinfo
            jwk-set-uri: https://example.org/oidc/oauth2/connect/jwk_uri
            custom-params: param_name1,param_value1,param_name2,param_value2,nonce,123456789
        registration:
          oidc:
            client-id: myclientid
            client-secret: myclientsecret
            authorization-grant-type: authorization_code
            scope:
              - openid
              - email
              - profile
            redirect-uri: https://mydomain/myapp/login/oauth2/code/oidc

這是出錯的地方:

1. OIDC服務器需要在請求中添加nonce參數URL

我已經通過使用讀取 custom-params 屬性並將這些值附加到請求 URL 的自定義 OAuth2AuthorizationRequest 解決了這個問題

2.OidcAuthorizationCodeAuthenticationProvider拋出invalid_redirect_uri_parameter引起的異常

我已經嘗試了很多方法來解決這個問題。

我嘗試創建一個過濾器,將 X-Forwarded-Proto 添加到請求中(因為代理不處理)。

添加了標題,我還添加了以下屬性:

server:
    forward-headers-strategy: native
    tomcat.protocol-header: x-forwarded-proto

但這似乎不起作用。

OidcAuthorizationCodeAuthenticationProvider 仍然會拋出異常,因為此條件為假:

!authorizationResponse.getRedirectUri().equals(authorizationRequest.getRedirectUri())

我已經調試了代碼,唯一的區別是 http 和另一個 https。

我發現了一個我根本不喜歡的非常hacky的解決方案,這是另一個過濾器,它只針對特定的URL修改了URL。

我更喜歡更優雅的解決方案。

3.使用自定義nonce參數時,OidcAuthorizationCodeAuthenticationProvider拋出異常原因由invalid_nonce

現在我被困住了。 我考慮編寫自己的身份驗證提供程序,但我不能保證我的會在 Spring 提供的 OIDC 之前獲得。

有了隨機數,這是一個陷阱 22:

  • 如果我不使用自定義參數,我找不到讓 Spring 將隨機數添加到請求的方法

  • 如果我使用那個,當它是 JWT 的一部分時,Spring 無法識別它並嚇壞了

任何幫助將不勝感激,因為這讓我瘋了幾天甚至幾周。

謝謝你。

編輯

在案例 2 中比較的 2 個 url 來自:

  • OAuth2AuthorizationRequest
  • OAuth2AuthorizationResponse

OAuth2AuthorizationRequest在以下行的 OAuth2AuthorizationRequestRedirectFilter 中構建:

OAuth2AuthorizationRequest authorizationRequest = this.authorizationRequestResolver.resolve(request);

重定向 uri 內置在 DefaultOAuth2AuthorizationRequestResolver.expandRedirectUri() 中,它調用

UriComponentsBuilder.fromHttpUrl(UrlUtils.buildFullRequestUrl(request))

OAuth2AuthorizationResponse內置在 OAuth2LoginAuthenticationFilter.attemptAuthentication() 中,它也調用

UriComponentsBuilder.fromHttpUrl(UrlUtils.buildFullRequestUrl(request))

接着

OAuth2AuthorizationResponseUtils.convert(params, redirectUri)

我會仔細檢查,但我不記得在構建這些 URL 時調用了 UriComponentsBuilder.adaptFromForwardedHeaders(HttpHeaders headers)。

即使這樣可行,仍然存在隨機數問題:(

我們偶然發現了同樣的問題,問題主要是因為我們的服務器位於反向代理后面,並且似乎代理更改了 url 以某種方式導致此檢查失敗

!authorizationResponse.getRedirectUri().equals(authorizationRequest.getRedirectUri())

在提交時,此行在 spring 安全性的更高版本中被刪除

24500fa3ca23aa23ede86dfcfe02113671d5b8bc

在 github 提交

它於 2019 年 12 月 6 日推出,位於 spring 安全版本 5.1.13

所以解決方案是將 spring 引導升級到至少 2.1.17 以用於 spring 引導 2.1.X 系列版本。

雖然 OP 說他無法升級他的庫,但我希望這可以幫助那些可以升級的人。

我們還完成了 Kavithakaran Kanapathippilla 上面提到的解決方案,並配置了我們的反向代理以添加 X-forwarded-proto http 標頭,我也相信我們配置了 spring 引導應用程序.properties 來檢查它們

spring 用於在代理后面工作的引導文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM