我具有使用JWTAuthenticator
配置的Play with Silhouette身份验证和授权。该身份验证器通过读取X-Auth-Token
标头(即jwt
)工作,以识别每个请求中的用户。这对于REST终结点来说很好用。
现在,我的图像只有所有者可以访问,并且我想在background-image
css属性中使用这些图像(这是必需的)。在这种情况下,我无法设置请求标头,因此请求将被拒绝。
下一步自然是将令牌作为URL参数嵌入到background-image
URL本身中。但是我不知道该如何在服务器端进行操作。如果不存在JWTAuthenticator
标头,如何告诉X-Auth-Token
在网址中使用令牌?
[好,我在这里分享我的发现,因此希望这可以对其他人有所帮助。在深入研究剪影代码时,我发现我们可以轻松地配置要从哪一部分请求(标头,查询字符串等)中读取令牌。默认情况下,从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=........")
}