Skip to content

Instantly share code, notes, and snippets.

@mike-neck
Last active October 27, 2020 00:30
Show Gist options
  • Save mike-neck/6557b856be1cb965825be719217c60b8 to your computer and use it in GitHub Desktop.
Save mike-neck/6557b856be1cb965825be719217c60b8 to your computer and use it in GitHub Desktop.
@Grab(group='io.jsonwebtoken', module='jjwt-api', version='0.11.2')
@Grab(group='io.jsonwebtoken', module='jjwt-impl', version='0.11.2')
@Grab(group='io.jsonwebtoken', module='jjwt-jackson', version='0.11.2')
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;
import io.jsonwebtoken.security.SignatureException;
import io.jsonwebtoken.SignatureAlgorithm
import java.time.Instant
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.time.Duration
import java.util.stream.Collectors
import java.nio.ByteBuffer
def random = new Random()
def buffer = ByteBuffer.allocate(8 * 4)
random.longs()
.filter { it >= 0 }
.limit(4)
.forEach { buffer.putLong(it) }
def bytes = buffer.array()
def now = OffsetDateTime.now(ZoneOffset.UTC)
def exp = (now + Duration.ofHours(2)).toInstant()
Jwts.builder()
.setSubject('user')
.setExpiration(Date.from(exp))
.setIssuedAt(Date.from(now.toInstant()))
.setIssuer('https://example.com')
.signWith(SignatureAlgorithm.RS256, bytes)
.compact()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment