Skip to content
Advertisement

How to use Jwts to generate Jwt Token ? Can anyone share me code..?

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…

Advertisement

Answer

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();
    }
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement