๊ธฐ์กด 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๋ก ๋ง๋ค์ด ์ฌ์ฉํด์ผํ ํ์์ฑ์ ๋๊ผ๋ค..!
๋ค๋ฅธ ํด๋์ค์์๋ ์ธ์ฆ๋ ์ฌ์ฉ์์ ๋ํ 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);
}
}