Home > Software engineering >  How to use Jwts to generate Jwt Token ? Can anyone share me code..?
How to use Jwts to generate Jwt Token ? Can anyone share me code..?

Time:04-19

private String dogenerateToken(Map<String, Object> map, String username) {
    return null;
}

I am having only null method...Can you write logic to generate jwt token...

CodePudding user response:

Use the below logic. Before that get the username and password from the user and authenticate it ...and send the authenticationto the generate token method and generate token like this.

 public String generateToken(Authentication authentication) {
        Details user = (Details) authentication.getPrincipal();

        return Jwts.builder().setClaims(null).setSubject(user.getUsername()).setSubject(user.getEmail())
                .setIssuedAt(new Date())
                .setExpiration(new Date((new Date()).getTime()   tokenExpirationTime))
                .signWith(SignatureAlgorithm.HS512, secretKey).compact();
    }
  • Related