Spring Security / JWT 에 대한 자료를 찾아보다 아래와 같은 코드를 발견
public enum OAuthAttributes {
GITHUB("github", (attributes) -> {
return new UserProfile(
String.valueOf(attributes.get("id")),
(String) attributes.get("name"),
(String) attributes.get("email"),
(String) attributes.get("avatar_url")
);
}),
GOOGLE("google", (attributes) -> {
return new UserProfile(
String.valueOf(attributes.get("sub")),
(String) attributes.get("name"),
(String) attributes.get("email"),
(String) attributes.get("picture")
);
}),
NAVER("naver", (attributes) -> {
Map<String, Object> response = (Map<String, Object>) attributes.get("response");
return new UserProfile(
(String) response.get("id"),
(String) response.get("name"),
(String) response.get("email"),
(String) response.get("profile_image")
);
});
private final String registrationId;
private final Function<Map<String, Object>, UserProfile> of;
OAuthAttributes(String registrationId, Function<Map<String, Object>, UserProfile> of) {
this.registrationId = registrationId;
this.of = of;
}
public static UserProfile extract(String registrationId, Map<String, Object> attributes) {
return Arrays.stream(values())
.filter(provider -> registrationId.equals(provider.registrationId))
.findFirst()
.orElseThrow(IllegalArgumentException::new)
.of.apply(attributes);
}
}