JSONWebTokens on Scalatra

0

I am trying to add security to my web application using JWT in Scalatra. At the moment I am using Scentry and I have the User-Password and RememberMe strategies defined and working. My intention is to change the RememberMe strategy, which currently works with cookies, for an authentication with JWT.

I found this implementation that I can incorporate using the json4s extension ( example ) but I'm not sure how to include it in my code:

  • Can I simply replace the cookie checks with the JWT checks with the functions in the example?

  • How should I access the JWT?

  • asked by Hellzzar 27.03.2016 в 14:16
    source

    1 answer

    0

    According to the documentation you only have to write your own strategy that uses the trait BasicAuthStrategy

    class JWTAuthStrategy(protected override val app: ScalatraBase, realm: String) extends BasicAuthStrategy[User](app, realm) {
    
      protected def validate(token: String): Option[User] = {
        // Aquí usar las funciones de Jwt para validar el token.
        if(Jwt.isValid(token, "secretKey", Seq(JwtAlgorithm.HS256))) Some(User("scalatra"))
        else None
      }
    
      protected def getUserId(user: User): String = user.id
    }
    

    And register your new strategy with the registerAuthStrategies method

    You can take a look at this example: link

        
    answered by 05.04.2016 / 08:26
    source