如何使用JWT Authenticator访问Play Silhouette中的受保护资产

问题描述 投票:0回答:1

我具有使用JWTAuthenticator配置的Play with Silhouette身份验证和授权。该身份验证器通过读取X-Auth-Token标头(即jwt)工作,以识别每个请求中的用户。这对于REST终结点来说很好用。

现在,我的图像只有所有者可以访问,并且我想在background-image css属性中使用这些图像(这是必需的)。在这种情况下,我无法设置请求标头,因此请求将被拒绝。

下一步自然是将令牌作为URL参数嵌入到background-image URL本身中。但是我不知道该如何在服务器端进行操作。如果不存在JWTAuthenticator标头,如何告诉X-Auth-Token在网址中使用令牌?

scala playframework silhouette
1个回答
0
投票

[好,我在这里分享我的发现,因此希望这可以对其他人有所帮助。在深入研究剪影代码时,我发现我们可以轻松地配置要从哪一部分请求(标头,查询字符串等)中读取令牌。默认情况下,从X-Auth-Token标头读取jwt令牌,但是我们可以配置JWTAuthenticator从查询字符串变量中读取令牌also

    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)

现在,如果要在带有silhouette.SecureAction的控制器后面使用受保护的资产,则只需将X-Auth-Token作为查询字符串参数添加到url:

.my-class {
  background-image: url("/image?X-Auth-Token=........")
}
© www.soinside.com 2019 - 2024. All rights reserved.