what is the best way to implement access token in play framework

1.3k Views Asked by At

I am working on a project with play framework 2.5 which provides API service for developers.

I now want to implement access token with play framework in order to control the access of the server APIs.

I have implemented app key and app secret already. Can anyone please advices how to generate the access tokens?

1

There are 1 best solutions below

4
rohith On BEST ANSWER

Use JavaWebTokens(jwt) which will provide you unique key always ,

public String getJWT(){
     Date now = new Date();
     long t = now.getTime();
     Date expirationTime = new Date(t + 1300819380);    

     return Jwts.builder()
          .setSubject("any subject")
          .setIssuedAt(now)
          .setExpiration(expirationTime)
          .signWith(SignatureAlgorithm.HS512, "username")
          .compact();
}
<--! to validate jwt -->
public boolean validateJWT(String jwt){
    try {
        Jwts.parser().setSigningKey("username").parseClaimsJws(jwt);
        return true;
    } catch (Exception e) {
        // TODO: handle exception
        return false;

    }

}