feat(user-service): Naver Oauth 로그인 추가.

Naver Oauth 로그인 추가.
This commit is contained in:
hoon7566
2022-02-16 11:48:06 +09:00
parent 496711e47e
commit d16de3c3cc
8 changed files with 573 additions and 24 deletions

View File

@@ -0,0 +1,66 @@
package com.justpickup.userservice.domain.user.dto;
import com.justpickup.userservice.domain.user.entity.AuthType;
import com.justpickup.userservice.domain.user.entity.Customer;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Map;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class OAuthAttributeDto {
private Map<String, Object> attributes; // OAuth2 반환하는 유저정보 MAP
private String nameAttributeKey;
private String name;
private String email;
private AuthType authType;
public OAuthAttributeDto(Map<String, Object> attributes, String nameAttributeKey, String name, String email) {
this.attributes = attributes;
this.nameAttributeKey = nameAttributeKey;
this.name = name;
this.email = email;
}
public static OAuthAttributeDto of(String registrationId, String userNameAttributeName, Map<String, Object> attributes){
// 여기서 네이버와 카카오 등 구분 (ofNaver, ofKakao)
if("naver".equals(registrationId))
return ofNaver(userNameAttributeName , attributes);
return ofGoogle(userNameAttributeName, attributes);
}
private static OAuthAttributeDto ofNaver(String userNameAttributeName, Map<String, Object> attributes) {
Map<String, Object> response = (Map<String, Object>) attributes.get("response");
return OAuthAttributeDto.builder()
.name((String) response.get("name"))
.email((String) response.get("email"))
.nameAttributeKey("id")
.attributes(response)
.authType(AuthType.NAVER)
.build();
}
private static OAuthAttributeDto ofGoogle(String userNameAttributeName, Map<String, Object> attributes) {
return OAuthAttributeDto.builder()
.name((String) attributes.get("name"))
.email((String) attributes.get("email"))
.nameAttributeKey(userNameAttributeName)
.attributes(attributes)
.authType(AuthType.GOOGLE)
.build();
}
public Customer toEntity(OAuthAttributeDto attributeDto){
return new Customer(email,"temp",name,null, attributeDto.getAuthType());
}
}

View File

@@ -0,0 +1,14 @@
package com.justpickup.userservice.domain.user.entity;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@Getter
@RequiredArgsConstructor
public enum Role {
GUEST("ROLE_GUEST", "손님"),
USER("ROLE_USER", "일반 사용자");
private final String key;
private final String title;
}

View File

@@ -1,23 +1,14 @@
package com.justpickup.userservice.domain.user.service;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.justpickup.userservice.domain.user.dto.CustomerDto;
import com.justpickup.userservice.domain.user.dto.OAuthAttributeDto;
import com.justpickup.userservice.domain.user.entity.AuthType;
import com.justpickup.userservice.domain.user.entity.Customer;
import com.justpickup.userservice.domain.user.exception.NotExistUserException;
import com.justpickup.userservice.domain.user.repository.CustomerRepository;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;
import org.springframework.data.annotation.ReadOnlyProperty;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService;
import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest;
@@ -27,14 +18,10 @@ import org.springframework.security.oauth2.core.user.DefaultOAuth2User;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Optional;
import java.util.function.Function;
@Service
@RequiredArgsConstructor
@@ -43,7 +30,7 @@ import java.util.function.Function;
public class UserServiceImpl implements UserService {
private final CustomerRepository customerRepository;
private final HttpSession httpSession;
private final HttpServletResponse response;
private final Environment env;
@Override
@@ -98,7 +85,7 @@ public class UserServiceImpl implements UserService {
.orElse(attributeDto.toEntity(attributeDto))
);
httpSession.setAttribute("user", new SessionCustomer(customer)); // SessionUser (직렬화된 dto 클래스 사용)
// TODO: 2022/02/16 Response에 token 담아 보내기
return new DefaultOAuth2User(
Collections.singleton(new SimpleGrantedAuthority(customer.getRole().getKey()))
@@ -108,11 +95,11 @@ public class UserServiceImpl implements UserService {
}
@Getter
public class SessionCustomer implements Serializable {
public static class UserPayload implements Serializable {
private String name;
private String email;
public SessionCustomer(Customer user){
public UserPayload(Customer user){
this.name = user.getName();
this.email = user.getEmail();
}

View File

@@ -24,6 +24,7 @@ public class WebSecurity extends WebSecurityConfigurerAdapter {
.logoutSuccessUrl("/")
.and()
.oauth2Login()
.defaultSuccessUrl("http://just-pickup.com:8000/customer-frontend-service/")
.userInfoEndpoint()
.userService(userService);