package com.technicalrex.springsecurityjwt.auth.jwt;

import com.google.common.base.Preconditions;
import com.technicalrex.springsecurityjwt.support.validation.StringConditions;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.security.core.userdetails.User;

public final class TokenHandler {

    private final String secret;
    private final UserService userService;

    public TokenHandler(String secret, UserService userService) {
        this.secret = StringConditions.checkNotBlank(secret);
        this.userService = Preconditions.checkNotNull(userService);
    }

    public User parseUserFromToken(String token) {
        String username = Jwts.parser()
                .setSigningKey(secret)
                .parseClaimsJws(token)
                .getBody()
                .getSubject();
        return userService.loadUserByUsername(username);
    }

    public String createTokenForUser(User user) {
        return Jwts.builder()
                .setSubject(user.getUsername())
                .signWith(SignatureAlgorithm.HS512, secret)
                .compact();
    }
}

@@ -301,16 +301,41 @@
 ls.User;
+%0Aimport java.util.Base64;
 %0A%0Apublic
@@ -528,16 +528,51 @@
 ecret = 
+Base64.getEncoder().encodeToString(
 StringCo
@@ -601,16 +601,28 @@
 (secret)
+.getBytes())
 ;%0A      

Base64 encode JWT secret