简体   繁体   English

在play2剪影JWT中达到JWT令牌

[英]Reaching JWT Token in play2 silhouette JWTAuthenticator

Play-silhouette-rest activator template gives a good example how to use REST authentication/sign up with HeaderAuthenticator. Play-silhouette-rest激活器模板提供了一个很好的示例,说明如何使用REST身份验证/通过HeaderAuthenticator进行注册。 Getting sign in request it creates user and returns token in both response body and header 获取登录请求后,它将创建用户并在响应正文和标头中返回令牌

 val response = Ok(Json.toJson(Token(token = authenticator.id, expiresOn = authenticator.expirationDate)))
 env.authenticatorService.init(authenticator, Future.successful(response))

In this example I am getting same tokens in body (being initialized on first line) and in headers (being initialized on second line). 在此示例中,我在正文(在第一行中初始化)和标头(在第二行中初始化)中得到了相同的标记。

Willing to use JWTAuthenticator, I changed code to use this instead (code is the same, only one difference is dependency injection code) so I expected the same appearance. 愿意使用JWTAuthenticator,我更改了代码以改为使用它(代码是相同的,唯一的区别是依赖注入代码),所以我希望外观相同。

But with JWTAuthenticator authenticator.id gives me some another generated ID (based on which real JSON Web Token is generated later) and JSON Web Token is being written only in header, on second code line. 但是使用JWTAuthenticator authenticator.id可以给我另一个生成的ID(基于稍后生成的真实JSON Web令牌),并且JSON Web Token仅写在第二行代码的标头中。

I am willing to return real json web token in reponse body but don't want to read headers after I wrote in them in the same method. 我愿意在响应正文中返回真实的json网络令牌,但在用相同的方法编写标头后,不想读取标头。

Is there any solution? 有什么解决办法吗?

The only solution I see is to init the authenticator with the request instead of the response. 我看到的唯一解决方案是使用请求而不是响应来初始化身份验证器。 This is possible because the authenticator service contains two init methods. 这是可能的,因为身份验证器服务包含两个init方法。 The first can init the authenticator with the response and the second can init the authenticator with the request. 第一个可以使用响应初始化身份验证器,第二个可以使用请求初始化身份验证器。

env.authenticatorService.init(authenticator, request).map { r =>
  r.headers.get("X-Auth-Token") match {
    case Some(token) => Ok(Json.toJson(Token(token = token, expiresOn = authenticator.expirationDate)))
    case None => BadRequest("Couldn't generate token")
  }
}

I'll see if I can add an additional init method which initializes the authenticator and returns the generated value. 我将看看是否可以添加其他init方法来初始化身份验证器并返回生成的值。 You can follow this issue . 您可以关注此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM