[英]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.