- update jwt
This commit is contained in:
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@@ -7,6 +7,6 @@
|
|||||||
|
|
||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.1-all.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-all.zip
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
@@ -11,9 +11,12 @@ plugins {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
// https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt-api
|
// https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt-api
|
||||||
implementation("io.jsonwebtoken:jjwt-api:0.12.3")
|
implementation("io.jsonwebtoken:jjwt-api:0.12.5")
|
||||||
|
|
||||||
// https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt-impl
|
// https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt-impl
|
||||||
runtimeOnly("io.jsonwebtoken:jjwt-impl:0.12.3")
|
runtimeOnly("io.jsonwebtoken:jjwt-impl:0.12.5")
|
||||||
runtimeOnly("io.jsonwebtoken:jjwt-jackson:0.12.3")
|
runtimeOnly("io.jsonwebtoken:jjwt-jackson:0.12.5")
|
||||||
|
|
||||||
|
// another library
|
||||||
|
implementation("com.auth0:java-jwt:4.4.0")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ import java.util.Date;
|
|||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class JwtSample {
|
public class JwsSampleOld {
|
||||||
private static final byte[] key;
|
private static final byte[] key;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2024. Elex. All Rights Reesrved.
|
||||||
|
* https://www.elex-project.com/
|
||||||
|
*/
|
||||||
|
|
||||||
|
package kr.pe.elex.examples;
|
||||||
|
|
||||||
|
import com.auth0.jwt.JWT;
|
||||||
|
import com.auth0.jwt.algorithms.Algorithm;
|
||||||
|
import com.auth0.jwt.interfaces.DecodedJWT;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class Jwt0Sample {
|
||||||
|
private static final byte[] key;
|
||||||
|
|
||||||
|
static {
|
||||||
|
// HMACSHA256을 사용하므로 키의 길이는 32바이트이다.
|
||||||
|
key = new byte[32];
|
||||||
|
new Random().nextBytes(key);
|
||||||
|
}
|
||||||
|
public static String genToken(){
|
||||||
|
return JWT.create()
|
||||||
|
.withIssuer("Elex")
|
||||||
|
.withSubject("Hello")
|
||||||
|
.sign(Algorithm.HMAC256(key));
|
||||||
|
}
|
||||||
|
public static DecodedJWT parseToken(String token){
|
||||||
|
return JWT.require(Algorithm.HMAC256(key))
|
||||||
|
.withIssuer("Elex")
|
||||||
|
.build()
|
||||||
|
.verify(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String... args){
|
||||||
|
String token = genToken();
|
||||||
|
System.out.println(token);
|
||||||
|
|
||||||
|
DecodedJWT jwt = parseToken(token);
|
||||||
|
System.out.println(jwt.getSubject());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2024. Elex. All Rights Reesrved.
|
||||||
|
* https://www.elex-project.com/
|
||||||
|
*/
|
||||||
|
|
||||||
|
package kr.pe.elex.examples;
|
||||||
|
|
||||||
|
import io.jsonwebtoken.Header;
|
||||||
|
import io.jsonwebtoken.JwtException;
|
||||||
|
import io.jsonwebtoken.Jwts;
|
||||||
|
|
||||||
|
import javax.crypto.SecretKey;
|
||||||
|
import java.security.KeyPair;
|
||||||
|
import java.security.PrivateKey;
|
||||||
|
import java.security.PublicKey;
|
||||||
|
import java.time.ZonedDateTime;
|
||||||
|
import java.util.Base64;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https://github.com/jwtk/jjwt
|
||||||
|
*/
|
||||||
|
public class JwteSample {
|
||||||
|
|
||||||
|
public static SecretKey generateSecretKey() {
|
||||||
|
return Jwts.ENC.A256GCM.key().build();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static String generateToken(final SecretKey key) {
|
||||||
|
return Jwts.builder()
|
||||||
|
.header().type(Header.JWT_TYPE)
|
||||||
|
.and()
|
||||||
|
.id(UUID.randomUUID().toString())
|
||||||
|
.issuedAt(Date.from(ZonedDateTime.now().toInstant()))
|
||||||
|
.issuer("Elex Company")
|
||||||
|
.subject("Hello")
|
||||||
|
.audience().add("Charlie")
|
||||||
|
.and()
|
||||||
|
.expiration(Date.from(ZonedDateTime.now().plusHours(2).toInstant()))
|
||||||
|
.notBefore(Date.from(ZonedDateTime.now().toInstant()))
|
||||||
|
.encryptWith(key, Jwts.ENC.A256GCM)
|
||||||
|
.compact();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static String parseToken(final String token, final SecretKey key) throws JwtException {
|
||||||
|
try {
|
||||||
|
return Jwts.parser().decryptWith(key).build()
|
||||||
|
.parseEncryptedClaims(token)
|
||||||
|
.getPayload().getSubject();
|
||||||
|
} catch (JwtException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String... args) {
|
||||||
|
SecretKey key = generateSecretKey();
|
||||||
|
System.out.println(Base64.getEncoder().encodeToString(key.getEncoded()));
|
||||||
|
|
||||||
|
String token = generateToken(key);
|
||||||
|
System.out.println(token);
|
||||||
|
|
||||||
|
String subject = parseToken(token, key);
|
||||||
|
System.out.println(subject);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
105
json-web-token/src/main/java/kr/pe/elex/examples/JwtsSample.java
Normal file
105
json-web-token/src/main/java/kr/pe/elex/examples/JwtsSample.java
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2024. Elex. All Rights Reesrved.
|
||||||
|
* https://www.elex-project.com/
|
||||||
|
*/
|
||||||
|
|
||||||
|
package kr.pe.elex.examples;
|
||||||
|
|
||||||
|
import io.jsonwebtoken.Header;
|
||||||
|
import io.jsonwebtoken.JwtException;
|
||||||
|
import io.jsonwebtoken.Jwts;
|
||||||
|
|
||||||
|
import javax.crypto.SecretKey;
|
||||||
|
import java.security.KeyPair;
|
||||||
|
import java.security.PrivateKey;
|
||||||
|
import java.security.PublicKey;
|
||||||
|
import java.time.ZonedDateTime;
|
||||||
|
import java.util.Base64;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https://github.com/jwtk/jjwt
|
||||||
|
*/
|
||||||
|
public class JwtsSample {
|
||||||
|
|
||||||
|
public static SecretKey generateSecretKey() {
|
||||||
|
return Jwts.SIG.HS256.key().build();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static KeyPair generateKeyPair() {
|
||||||
|
return Jwts.SIG.RS512.keyPair().build();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static String generateToken(final SecretKey key) {
|
||||||
|
return Jwts.builder()
|
||||||
|
.header().type(Header.JWT_TYPE)
|
||||||
|
.and()
|
||||||
|
.id(UUID.randomUUID().toString())
|
||||||
|
.issuedAt(Date.from(ZonedDateTime.now().toInstant()))
|
||||||
|
.issuer("Elex Company")
|
||||||
|
.subject("Hello")
|
||||||
|
.audience().add("Charlie")
|
||||||
|
.and()
|
||||||
|
.expiration(Date.from(ZonedDateTime.now().plusHours(2).toInstant()))
|
||||||
|
.notBefore(Date.from(ZonedDateTime.now().toInstant()))
|
||||||
|
.signWith(key)
|
||||||
|
.compact();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String generateToken(final PrivateKey key) {
|
||||||
|
return Jwts.builder()
|
||||||
|
.header().type(Header.JWT_TYPE)
|
||||||
|
.and()
|
||||||
|
.id(UUID.randomUUID().toString())
|
||||||
|
.issuedAt(Date.from(ZonedDateTime.now().toInstant()))
|
||||||
|
.issuer("Elex Company")
|
||||||
|
.subject("Hello")
|
||||||
|
.audience().add("Charlie")
|
||||||
|
.and()
|
||||||
|
.expiration(Date.from(ZonedDateTime.now().plusHours(2).toInstant()))
|
||||||
|
.notBefore(Date.from(ZonedDateTime.now().toInstant()))
|
||||||
|
.signWith(key, Jwts.SIG.RS512)
|
||||||
|
.compact();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String parseToken(final String token, final SecretKey key) throws JwtException {
|
||||||
|
try {
|
||||||
|
return Jwts.parser().verifyWith(key).build()
|
||||||
|
.parseSignedClaims(token)
|
||||||
|
.getPayload().getSubject();
|
||||||
|
} catch (JwtException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String parseToken(final String token, final PublicKey key) throws JwtException {
|
||||||
|
try {
|
||||||
|
return Jwts.parser()
|
||||||
|
.verifyWith(key)
|
||||||
|
.requireIssuer("Elex")
|
||||||
|
.build()
|
||||||
|
.parseSignedClaims(token)
|
||||||
|
.getPayload().getSubject();
|
||||||
|
} catch (JwtException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String... args) {
|
||||||
|
SecretKey key = generateSecretKey();
|
||||||
|
System.out.println(Base64.getEncoder().encodeToString(key.getEncoded()));
|
||||||
|
|
||||||
|
String token = generateToken(key);
|
||||||
|
System.out.println(token);
|
||||||
|
|
||||||
|
String subject = parseToken(token, key);
|
||||||
|
System.out.println(subject);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -72,7 +72,9 @@ public class MarkdownParser {
|
|||||||
PARSER = Parser.builder(options).build();
|
PARSER = Parser.builder(options).build();
|
||||||
RENDERER = HtmlRenderer.builder(options).build();
|
RENDERER = HtmlRenderer.builder(options).build();
|
||||||
|
|
||||||
YAML_VISITOR = new AbstractYamlFrontMatterVisitor();
|
YAML_VISITOR = new AbstractYamlFrontMatterVisitor(){
|
||||||
|
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public MarkdownParser(final String md) {
|
public MarkdownParser(final String md) {
|
||||||
|
|||||||
Reference in New Issue
Block a user