본문 바로가기

카테고리 없음

24.01.19 인가4

val token = jwtPlugin.generateToken(member)

 

여기에 생긴 빨간줄은 generatetoken, member인데 먼저 jwtPlugin으로 가서

package com.teamsparta.todo.infra.security.jwt

import io.jsonwebtoken.Claims
import io.jsonwebtoken.Jws
import io.jsonwebtoken.Jwts
import io.jsonwebtoken.security.Keys
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component
import java.nio.charset.StandardCharsets
import java.time.Instant
import java.util.*
import java.time.Duration


@Component
class JwtPlugin(
    @Value("\${auth.jwt.issuer}") private val issuer: String,
    @Value("\${auth.jwt.secret}") private val secret: String,
    @Value("\${auth.jwt.accessTokenExpirationHour}") private val accessTokenExpirationHour: Long,
) {

    fun validateToken(jwt: String): Result<Jws<Claims>> {
        return kotlin.runCatching {
            val key = Keys.hmacShaKeyFor(secret.toByteArray(StandardCharsets.UTF_8))
            Jwts.parser().verifyWith(key).build().parseSignedClaims(jwt)
        }
    }

    fun generateAccessToken(subject: String, role: String): String {
        return generateToken(subject, role, Duration.ofHours(accessTokenExpirationHour))
    }


    private fun generateToken(subject: String, role: String, expirationPeriod: Duration): String {
        val claims: Claims = Jwts.claims().add(mapOf("role" to role)).build()

        val now = Instant.now()
        val key = Keys.hmacShaKeyFor(secret.toByteArray(StandardCharsets.UTF_8))

        return Jwts.builder()
            .subject(subject)
            .issuer(issuer)
            .issuedAt(Date.from(now))
            .expiration(Date.from(now.plus(expirationPeriod)))
            .claims(claims)
            .signWith(key)
            .compact()
    }

}

여기에서

public fun generateToken(subject: String, role: String, expirationPeriod: Duration): String

 

이렇게 하고, member은 member로 가서

package com.teamsparta.todo.domain.model

import jakarta.persistence.*


@Entity
class Member(
    var username: String,
    var password: String,
    var email: String
) {
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Long? = null

    fun update(username: String, password: String, email: String) {
        this.username = username
        this.password = password
        this.email = email
    }
}

 

 

 

 

회원가입은

{
  "username": "나",
  "password": "비번",
  "email": "suh75321"
} 이랬는데 

 

로그인이

{
  "id": 1,
  "email": "",
  "password": "suh75321"
}

순서가 바뀌어있으면

data class MemberDto(
    val id: Long?,
    val email: String,
    val password: String,
) {
    companion object {
        fun from(member: Member): MemberDto {
            return MemberDto(member.id, member.email, member.password)
        }
    }
}

 

이걸 순서를 바꾸면 된다.

그런데

 

이게 떠야되는데 안뜨는데 이유가 있나? aop를 안넣어서?? 그리고 이상하게 이메일이 대신 암호화가 된다?? 왜??? 왜?????? 거기다가 절 되던 녀석이 401 에러가 뜨질 않나 자격증명도 꼬인 느낌이다. 주말에 해야하나? 분명 잘 됬는데!!

 

 

PS C:\Users\asdf\Desktop\뉴스피드꺼\TODO> git push origin main
To https://github.com/suh75321/Todo.git
! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'https://github.com/suh75321/Todo.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

 

이거뜨면 git pull origin main부터!!!!

깃허브에서 직접 수정한다음 git push해서 안됬는데 그래서 pull부터 하면 된다.

 

이번 과제는 여러모로 시간이 부족했다. 강의도 많고, 물론 저번보단 적었지만 과제까지 있는데다 하루를 잡아먹혀서 더 짧게 느껴졌다. 역시 가면 갈수록 더 힘들어진다. 오늘은 지쳤다. 수고쉽다.