java.lang.IllegalStateException: No primary or single unique constructor found

한지연·2023년 7월 3일
0

문제상황

oauth 로그인 후 UserDetails를 가져오려 하면 예외가 터짐


코드

  • controller
    @GetMapping("/social/oauth/login")
    public PrincipalDetails loginOauth(@AuthenticationPrincipal PrincipalDetails principalDetails){


        if(principalDetails != null) {

        return userService.loadUserByUsername(principalDetails.getUser().getUserId());
        }

        return null;
    }
  • service
    @Override
    public PrincipalDetails loadUserByUsername(String userId) throws UsernameNotFoundException {
        PrincipalDetails principalDetails = new PrincipalDetails(userRepository.findByUserStrId(userId));
        return principalDetails;
    }
  • PrincipalDetails
@Data
public class PrincipalDetails implements UserDetails, OAuth2User{

    private User user;
    private Map<String ,Object> attributes;

    public PrincipalDetails(User user) {
        this.user = user;
    }

    public PrincipalDetails(User user, Map<String, Object> attributes) {
        this.user = user;
        this.attributes = attributes;
    }

    @Override
    public Map<String, Object> getAttributes() {
        return attributes;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }

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

    @Override
    public String getUsername() {
        return user.getUsername();
    }

    @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 String getName() {
        return null;
    }
}

문제 해결

PrincipalDetails에 @NoArgsConstructor를 붙여준다.

@Data
@NoArgsConstructor
public class PrincipalDetails implements UserDetails, OAuth2User{
profile
배우고 활용하는 것을 즐기는 개발자, 한지연입니다!

0개의 댓글