소셜 로그인 각각 google, naver, kakao을 구현하는 방식에서 나아가 객체지향적 관점에 맞춰 통합해 보았습니다.
먼저 User 정보를 저장하는 User Domain은 다음과 같습니다.
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "username", nullable = false, columnDefinition = "varchar(255)")
private String username;
@Column(name = "password", nullable = false, columnDefinition = "text")
private String password;
@Column(name="salt",nullable = true)
@Type(type="uuid-char")
private UUID salt;
@Column(name = "roles", nullable = true, columnDefinition = "varchar(36)")
private String roles;
@Column(name = "profile_name", nullable = true, columnDefinition = "varchar(36)")
private String profileName;
@Column(name = "profile_birth", nullable = true, columnDefinition = "varchar (16)")
private String profileBirth;
@Column(name = "profile_image_path", nullable = true, columnDefinition = "text")
private String profileImagePath;
@Column(name = "created_at", nullable = false, columnDefinition = "datetime")
private LocalDateTime createdAt;
@Column(name = "updated_at", nullable = true, columnDefinition = "datetime")
private LocalDateTime updatedAt;
@Comment("소셜 로그인시 갱신됨 (네이버, 카카오, 구글 중 하나)")
@Column(name = "provider", nullable = true, columnDefinition = "varchar(36)")
private String provider;
}
각 소셜 로그인 api에서 공통으로 제공하는 정보를 가져와 보았습니다.
각 kakao, google, naver 서버 마다 응답해주는 사용자 정보 형식이 조금씩 다릅니다..! 그거에 맞게 아래와 같이 구현하면 객체지향의 관점을 잘 살릴 수 있는거 같습니다.
Google의 경우 아래와 같습니다.
public class GoogleUserInfo implements OAuth2UserInfo{
private Map<String, Object> attributes;
public GoogleUserInfo(Map<String, Object> attributes) {
this.attributes = attributes;
}
@Override
public String getProvider() {
return "google";
}
@Override
public String getProviderId() {
return (String) attributes.get("id");
}
@Override
public String getProfileName() {
return (String) attributes.get("name");
}
@Override
public String getProfileImagePath() {
return (String) attributes.get("picture");
}
@Override
public String getProfileBirth() {
return null;
}
@Override
public String getEmail() {
return (String) attributes.get("email");
}
}
카카오, 구글, 네이버 서버로부터 받은 사용자 정보를 OAuth2UserInfo(인터페이스)를 구현한 구현체에 맞게 적용 시키는 class입니다.
소셜 로그인(service 계층)은 아래와 같이 절차마다 한 줄씩 깔끔하게 작성할 수 있었습니다.
이상 소셜 로그인 통합이었습니다..! 읽어 주셔서 감사합니다.^^😊