프로그래밍/android

[android] JWT 토큰 구하기 (json web token)

-샤리- 2022. 1. 12. 12:15

build.gradle (:app)

api 'io.jsonwebtoken:jjwt-api:0.11.2'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.2'
runtimeOnly('io.jsonwebtoken:jjwt-orgjson:0.11.2') {
    exclude group: 'org.json', module: 'json'//provided by Android natively
}

 

create json web token

// 12시간
val expiration = System.currentTimeMillis() + 12 * (60 * 60 * 1000)
val base64EncodedSecretKey = "your_secretKey_abcdefghijklmn=="

val builder = Jwts.builder()
    .signWith(SignatureAlgorithm.forName("HS256"), base64EncodedSecretKey)
    .setIssuedAt(Date())
    .setExpiration(Date(expiration))
    .claim("userId", userId)
    .claim("name", userName)
    .claim("gender", gender)
    .claim("age", age)
    .claim("userScore", userScore)

val jwt = builder.compact()
tvJwt.text = jwt