Skip to content

Commit

Permalink
소셜로그인, 일반로그인 여부 구분 및 리다이렉트 추가 (#802)
Browse files Browse the repository at this point in the history
* feat: 소셜 로그인으로 가입한 회원인지 일반 로그인으로 가입한 회원인지 판단하는 Enum 추가

* feat: 일반 회원인 경우 STANDARD로 가입

* feat: 소셜 회원인 경우 SOCIAL로 가입

* feat: 이미 소셜 로그인으로 가입한 회원인 경우 특정 주소로 리다이렉트 하도록 구현

* chore: yml update

* style: sonarqube

* feat: 회원정보 조회 시 소셜로그인/일반가입 필드 추가

* chore: yml update

* feat: 리다이렉트 주소 변경
  • Loading branch information
jschoi-96 authored Dec 15, 2024
1 parent d88c89f commit 690a48b
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
Expand All @@ -20,9 +19,6 @@
@RequiredArgsConstructor
public class CustomSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {

@Value("${urls.redirect}")
private String redirectUrl;

private final JwtTokenProvider jwtTokenProvider;
private final MemberRepository memberRepository;

Expand All @@ -42,6 +38,8 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo

response.addCookie(JwtTokenProvider.createCookie(refreshToken));
response.addCookie(JwtTokenProvider.createAccessCookie(accessToken));

String redirectUrl = customUserDetails.getRedirectUrl();
response.sendRedirect(redirectUrl);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@
public class CustomOAuth2User implements OAuth2User {

private final Oauth2Dto oauth2Dto;
private final String redirectUrl;

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

public String getRedirectUrl() {
return redirectUrl;
}

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
Collection<GrantedAuthority> collection = new ArrayList<>();
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/balancetalk/global/oauth2/dto/Oauth2Dto.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package balancetalk.global.oauth2.dto;



import static balancetalk.member.domain.Role.USER;
import static balancetalk.member.domain.SignupType.SOCIAL;

import balancetalk.member.domain.Member;
import balancetalk.member.domain.Role;
import balancetalk.member.domain.SignupType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -14,13 +20,15 @@ public class Oauth2Dto {
private String name;
private String email;
private Role role;
private SignupType signupType;
private String password;

public Member toEntity() {
return Member.builder()
.nickname(name)
.email(email)
.role(Role.USER)
.role(USER)
.signupType(SOCIAL)
.password(password)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package balancetalk.global.oauth2.service;

import static balancetalk.global.config.SecurityConfig.*;
import static balancetalk.global.config.SecurityConfig.passwordEncoder;
import static balancetalk.member.domain.Role.USER;
import static balancetalk.member.domain.SignupType.STANDARD;

import balancetalk.global.oauth2.dto.CustomOAuth2User;
import balancetalk.global.oauth2.dto.GoogleResponse;
import balancetalk.global.oauth2.dto.KakaoResponse;
Expand All @@ -9,25 +12,28 @@
import balancetalk.global.oauth2.dto.Oauth2Response;
import balancetalk.member.domain.Member;
import balancetalk.member.domain.MemberRepository;
import balancetalk.member.domain.Role;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService;
import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest;
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class CustomOAuth2UserService extends DefaultOAuth2UserService {

@Value("${spring.security.security.oauth2-password}")
private String oauth2Password;

@Value("${urls.firstRegister}")
private String firstRegisterUrl;

@Value("${urls.alreadyRegistered}")
private String alreadyRegisteredUrl;

private final MemberRepository memberRepository;

@Override
Expand All @@ -44,21 +50,21 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic
};

String email = getEmail(oauth2Response);
String provider = getProvider(oauth2Response);
Member findMember = memberRepository.findByEmail(provider + "_" + email).orElse(null);
Member findMember = memberRepository.findByEmail(email).orElse(null);

if (findMember == null) {
String encodedPassword = passwordEncoder().encode(oauth2Password);
Oauth2Dto oauth2Dto = Oauth2Dto.builder()
.name(hideNickname(email))
.email(provider + "_" + email)
.role(Role.USER)
.email(email)
.role(USER)
.signupType(STANDARD)
.password(encodedPassword)
.build();

Member newMember = oauth2Dto.toEntity();
memberRepository.save(newMember);
return new CustomOAuth2User(oauth2Dto);
return new CustomOAuth2User(oauth2Dto, firstRegisterUrl);
}

else { // 회원이 존재할 떄
Expand All @@ -67,7 +73,7 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic
.email(findMember.getEmail())
.role(findMember.getRole())
.build();
return new CustomOAuth2User(oauth2Dto);
return new CustomOAuth2User(oauth2Dto, alreadyRegisteredUrl);
}
}

Expand All @@ -87,10 +93,4 @@ private String getEmail(Oauth2Response oauth2Response) {
.map(Oauth2Response::getEmail)
.orElse("null");
}

private String getProvider(Oauth2Response oauth2Response) {
return Optional.ofNullable(oauth2Response)
.map(Oauth2Response::getProvider)
.orElse("null");
}
}
3 changes: 3 additions & 0 deletions src/main/java/balancetalk/member/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public class Member extends BaseTimeEntity {
@Enumerated(value = EnumType.STRING)
private Role role;

@Enumerated(value = EnumType.STRING)
private SignupType signupType;

private Long profileImgId;

@OneToMany(mappedBy = "member")
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/balancetalk/member/domain/SignupType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package balancetalk.member.domain;

public enum SignupType {
SOCIAL, STANDARD
}
12 changes: 12 additions & 0 deletions src/main/java/balancetalk/member/dto/MemberDto.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package balancetalk.member.dto;


import static balancetalk.member.domain.SignupType.STANDARD;

import balancetalk.member.domain.Member;
import balancetalk.member.domain.Role;
import balancetalk.member.domain.SignupType;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
Expand Down Expand Up @@ -54,12 +58,16 @@ public static class JoinRequest {
@Schema(description = "회원 권한", example = "USER")
private Role role;

@Schema(description = "회원 가입 경로", example = "STANDARD")
private SignupType signupType;

public Member toEntity() {
return Member.builder()
.nickname(nickname)
.email(email)
.password(password)
.role(role)
.signupType(STANDARD)
.profileImgId(profileImgId)
.build();
}
Expand Down Expand Up @@ -116,6 +124,9 @@ public static class MemberResponse {
@Schema(description = "저장한 게시글 수", example = "21")
private int bookmarkedPostsCount;

@Schema(description = "회원 가입 경로", example = "STANDARD")
private SignupType signupType;

public static MemberResponse fromEntity(Member member, String profileImgUrl) {
return MemberResponse.builder()
.id(member.getId())
Expand All @@ -125,6 +136,7 @@ public static MemberResponse fromEntity(Member member, String profileImgUrl) {
.createdAt(member.getCreatedAt())
.postsCount(member.getPostsCount())
.bookmarkedPostsCount(member.getBookmarkedPostsCount())
.signupType(member.getSignupType())
.build();
}
}
Expand Down

0 comments on commit 690a48b

Please sign in to comment.