簡體   English   中英

如何使用 JWT Authenticator 訪問 Play Silhouette 中受保護的資產

[英]How to access protected assets in Play Silhouette with JWT Authenticator

我使用JWTAuthenticator配置了 Play with Silhouette 身份驗證和授權。 此身份驗證器通過讀取X-Auth-Token header(即jwt )來識別每個請求中的用戶。 這適用於 REST 端點。

現在我有只有所有者才能訪問的圖像,我想在background-image css 屬性中使用這些圖像(這是一個要求)。 在這種情況下,我無法設置請求 header 因此請求將被拒絕。

下一個自然步驟是將令牌作為 url 參數嵌入到background-image url 本身中。 但是我不知道如何在服務器端進行。 如果X-Auth-Token header 不存在,我如何告訴JWTAuthenticator使用 url 中的令牌?

好的,我在這里分享我的發現,希望這可以幫助其他人。 在深入研究剪影代碼時,我發現我們可以輕松地配置我們要從請求的哪個部分(標頭、查詢字符串……)讀取令牌。 默認情況下,jwt 令牌是從X-Auth-Token header 讀取的,但我們可以配置JWTAuthenticator以從查詢字符串變量中讀取令牌:

    val config =
      configuration.underlying
        .as[JWTAuthenticatorSettings]("silhouette.authenticator")
        // this is the important line of code
        .copy(requestParts = Some(Seq(RequestPart.Headers, RequestPart.QueryString)))

    new JWTAuthenticatorService(config, None, encoder, idGenerator, clock)

現在,如果您想使用 controller 后面的受保護資產和silhouette.SecureAction .SecureAction,那么您只需將X-Auth-Token作為查詢字符串參數添加到 url:

.my-class {
  background-image: url("/image?X-Auth-Token=........")
}

暫無
暫無

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

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