๐Ÿ’ญ ๋ฌธ์ œ ์ธ์‹

๊ธฐ์กด Security ์—†์ด ๋กœ๊ทธ์ธ๊ณผ์ •์„ ๊ตฌํ˜„ํ–ˆ์ง€๋งŒ Security์˜ ํ•„์š”์„ฑ์„ ๋Š๊ปด ๊ตฌํ˜„ํ•˜๋˜ ๋„์ค‘ Authentication ๊ณผ์ •์—์„œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋‹ค.

Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException

์›๋ž˜๋Œ€๋กœ๋ผ๋ฉด,

ํšŒ์›๋กœ๊ทธ์ธ โ†’ AuthService โ†’ UserDetailsService โ†’ JwtFilter Authorization โ†’ AccessToken, RefreshToken ์‘๋‹ต

๊ณผ์ •์„ ๊ฑฐ์ณ Authentication/Authorization ๊ณผ์ •์ด ์ด๋ฃจ์–ด์ง€๋Š”๋ฐ UserDetailsService์—์„œ ๋ฌด์Šจ ์ด์œ ์ธ์ง€ NumberFormatException์ด ๋ฐœ์ƒํ•˜์˜€๋‹ค.

๐Ÿ› ๏ธ ๋ฌธ์ œ ํ•ด๊ฒฐ์‹œ๋„

๐Ÿ“์ฒซ ๋ฒˆ์งธ๋กœ, ๋กœ๊ทธ๋ฅผ ํ™•์ธํ•ด๋ณด์•˜๋‹ค.

๋กœ๊ทธ๋ฅผ ํ™•์ธํ•ด๋ณด๋‹ˆ, User Class์—์„œ Name์„ String์—์„œ Long Type์œผ๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค๊ณ  ๋‚˜์™€์žˆ์—ˆ๋‹ค.

๐Ÿ“๋‘ ๋ฒˆ์จฐ๋กœ, ๋””๋ฒ„๊น…์„ ํ•ด๋ณด์•˜๋‹ค.

๋ฌธ์ œ๊ฐ€ User Class์—์„œ ์ผ์–ด๋‚ฌ์œผ๋‹ˆ, ํ•ด๋‹น ๋ถ€๋ถ„์„ ์ค‘๋‹จ์ ์„ ์ฐ์–ด ๋””๋ฒ„๊น…ํ•ด๋ณด์•˜๋‹ค.

Authentication ์ด ์ง„ํ–‰๋  ๋•Œ, User Class์— ๋“ค์–ด๊ฐ€๋Š” username ๋ถ€๋ถ„ ๊ฐ’์„ ํ™•์ธํ•ด๋ณด๋‹ˆ, memberId๊ฐ€ ์•„๋‹ˆ๋ผ email์ด ๋“ค์–ด๊ฐ”์—ˆ๋‹ค..

์ฒ˜์Œ์—๋Š”, memberId๋กœ Authentication์„ ๊ตฌํ˜„ํ•˜๋ คํ•˜์˜€์ง€๋งŒ, ์ธ์ฆ ํ›„ ์ธ์ฆ์ด ํ•„์š”ํ•œ API ํ˜ธ์ถœ์—์„œ ๋กœ๊ทธ์ธํ•œ ํšŒ์›์˜ ์ •๋ณด๋ฅผ ์“ฐ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์œผ๋ฏ€๋กœ CustomUserDetails Class๋กœ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•ด์•ผํ•  ํ•„์š”์„ฑ์„ ๋Š๊ผˆ๋‹ค..!

โœ… ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

๐Ÿ“ CustomUserDetails ์ž‘์„ฑ.

๋‹ค๋ฅธ ํด๋ž˜์Šค์—์„œ๋„ ์ธ์ฆ๋œ ์‚ฌ์šฉ์ž์— ๋Œ€ํ•œ email, memberId, role, social ์ •๋ณด๊ฐ€ ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, CustomUser๋ฅผ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•˜์˜€๋‹ค. ์•„๋ž˜๋Š” ํ•ด๋‹น CustomUser ํด๋ž˜์Šค์ด๋‹ค..

๐Ÿ› ๏ธ User๋ฅผ ์ƒ์†๋ฐ›๋Š” MemberDetails

package com.devcv.auth.details;

import com.devcv.member.domain.Member;
import lombok.Getter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Getter
public class MemberDetails extends User {

    private final Member member;

    public MemberDetails(Member member, List<? extends GrantedAuthority> authorities) {
        super(member.getEmail(), member.getPassword(), authorities);
        this.member = member;
    }

    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        return Collections.singleton(new SimpleGrantedAuthority("ROLE_"+(member.getMemberRole().name())));
    }

    @Override
    public String getPassword() {
        return member.getPassword();
    }

    @Override
    public String getUsername() {
        return String.valueOf(member.getMemberId());
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        MemberDetails that = (MemberDetails) o;
        return Objects.equals(member, that.member);
    }

    @Override
    public int hashCode() {
        return Objects.hash(member);
    }

}