package org.visola.spring.security.tokenfilter.jwt;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import javax.inject.Inject;

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import com.nimbusds.jwt.JWTClaimsSet;

import net.minidev.json.JSONObject;

public class UsernamePasswordAuthenticationTokenJwtClaimsSetTransformer implements AuthenticationJwtClaimsSetTransformer {

  private static final String EMPTY_PASSWORD = "";

  private final Optional<String> rolePrefix;

  @Inject
  public UsernamePasswordAuthenticationTokenJwtClaimsSetTransformer(Optional<String> rolePrefix) {
    this.rolePrefix = rolePrefix;
  }

  @Override
  public JWTClaimsSet getClaimsSet(Authentication auth) {
    JSONObject object = new JSONObject();
    object.put("subject", auth.getPrincipal());

    StringBuilder roles = new StringBuilder();
    for (GrantedAuthority authority : auth.getAuthorities()) {
      String role = authority.getAuthority();
      if (rolePrefix.isPresent()) {
        role = role.substring(rolePrefix.get().length(), role.length());
      }
      roles.append(role.toLowerCase());
      roles.append(",");
    }
    roles.setLength(roles.length() - 1);

    object.put("roles", roles.toString());

    try {
      return JWTClaimsSet.parse(object);
    } catch (ParseException e) {
      throw new RuntimeException("Error while parsing JSON for claims set.", e);
    }
  }

  @Override
  public Authentication getAuthentication(JWTClaimsSet claimSet) {
    List<? extends GrantedAuthority> authorities = new ArrayList<>();
    String roles = (String) claimSet.getClaim("roles");
    if (roles != null) {
      authorities = Arrays.asList(roles.replaceAll("\\s*,\\s*", ",").split(",")).stream()
          .map(role -> new SimpleGrantedAuthority(rolePrefix.orElse("")+role.toUpperCase()))
          .collect(Collectors.toList());
    }
    return new UsernamePasswordAuthenticationToken(claimSet.getSubject(), EMPTY_PASSWORD, authorities);
  }

}

@@ -50,41 +50,8 @@
 t;%0A%0A
-import java.text.ParseException;%0A
 impo
@@ -99,16 +99,39 @@
 Arrays;%0A
+import java.util.Date;%0A
 import j
@@ -516,16 +516,75 @@
 thority;
+%0Aimport org.springframework.security.core.userdetails.User;
 %0A%0Aimport
@@ -620,45 +620,8 @@
 t;%0A%0A
-import net.minidev.json.JSONObject;%0A%0A
 publ
@@ -792,16 +792,52 @@
  = %22%22;%0A%0A
+  private final long tokenDuration;%0A
   privat
@@ -952,16 +952,36 @@
 sformer(
+long tokenDuration, 
 Optional
@@ -1037,16 +1037,56 @@
 Prefix;%0A
+    this.tokenDuration = tokenDuration;%0A
   %7D%0A%0A  @
@@ -1160,91 +1160,88 @@
     
-JSONObject object = new JSONObject();%0A    object.put(%22subject%22, auth.getPrincipal()
+User user = (User) auth.getPrincipal();%0A    long now = System.currentTimeMillis(
 );%0A%0A
@@ -1634,218 +1634,227 @@
     
-object.put(%22roles%22, roles.toString());%0A%0A    try %7B%0A      return JWTClaimsSet.parse(object);%0A    %7D catch (ParseException e) %7B%0A      throw new RuntimeException(%22Error while parsing JSON for claims set.%22, e);%0A    %7D
+return new JWTClaimsSet.Builder()%0A        .subject(user.getUsername())%0A        .issueTime(new Date(now))%0A        .expirationTime(new Date(now + tokenDuration))%0A        .claim(%22roles%22, roles.toString())%0A        .build();
 %0A  %7D

Use JWTClaimsSet.Builder to build claims set