From dd6219dc5382f1dfc11e488987f36591c1b6082a Mon Sep 17 00:00:00 2001 From: yeonjy Date: Tue, 19 Mar 2024 17:42:44 +0900 Subject: [PATCH 1/5] =?UTF-8?q?feat:=20AuthService=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/member/query/AuthService.java | 32 +++++++++++++++++++ .../backend/global/query/QueryService.java | 16 ++++++++++ 2 files changed, 48 insertions(+) create mode 100644 backend/core/src/main/java/com/rollthedice/backend/domain/member/query/AuthService.java create mode 100644 backend/core/src/main/java/com/rollthedice/backend/global/query/QueryService.java diff --git a/backend/core/src/main/java/com/rollthedice/backend/domain/member/query/AuthService.java b/backend/core/src/main/java/com/rollthedice/backend/domain/member/query/AuthService.java new file mode 100644 index 00000000..9b7a8662 --- /dev/null +++ b/backend/core/src/main/java/com/rollthedice/backend/domain/member/query/AuthService.java @@ -0,0 +1,32 @@ +package com.rollthedice.backend.domain.member.query; + +import com.rollthedice.backend.domain.member.entity.Member; +import com.rollthedice.backend.domain.member.repository.MemberRepository; +import com.rollthedice.backend.global.jwt.service.JwtService; +import com.rollthedice.backend.global.query.QueryService; +import jakarta.persistence.EntityNotFoundException; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.userdetails.UserDetails; + +@Slf4j +@QueryService +@RequiredArgsConstructor +public class AuthService { + private final MemberRepository memberRepository; + private final JwtService jwtService; + + public Long getMemberId() { + return getMember().getId(); + } + + public Member getMember() { + + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + UserDetails userDetails = (UserDetails) authentication.getPrincipal(); + return memberRepository.findByEmail(userDetails.getUsername()).orElseThrow(EntityNotFoundException::new); + } + +} diff --git a/backend/core/src/main/java/com/rollthedice/backend/global/query/QueryService.java b/backend/core/src/main/java/com/rollthedice/backend/global/query/QueryService.java new file mode 100644 index 00000000..724ae589 --- /dev/null +++ b/backend/core/src/main/java/com/rollthedice/backend/global/query/QueryService.java @@ -0,0 +1,16 @@ +package com.rollthedice.backend.global.query; + +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Service +@Transactional(readOnly = true) +public @interface QueryService { +} From ec9305bbe7cddc62a03a24de44bd415b765065dd Mon Sep 17 00:00:00 2001 From: yeonjy Date: Tue, 19 Mar 2024 17:43:51 +0900 Subject: [PATCH 2/5] =?UTF-8?q?refactor:=20=EC=82=AC=EC=9A=A9=EC=9E=90?= =?UTF-8?q?=EA=B0=80=20nickname=20=EC=A7=80=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../member/controller/LoginController.java | 23 +++++++++++++++++++ .../backend/domain/member/entity/Role.java | 2 +- .../global/oauth2/OAuthAttributes.java | 2 +- 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 backend/core/src/main/java/com/rollthedice/backend/domain/member/controller/LoginController.java diff --git a/backend/core/src/main/java/com/rollthedice/backend/domain/member/controller/LoginController.java b/backend/core/src/main/java/com/rollthedice/backend/domain/member/controller/LoginController.java new file mode 100644 index 00000000..219f3c68 --- /dev/null +++ b/backend/core/src/main/java/com/rollthedice/backend/domain/member/controller/LoginController.java @@ -0,0 +1,23 @@ +package com.rollthedice.backend.domain.member.controller; + +import com.rollthedice.backend.domain.member.dto.SignUpDto; +import com.rollthedice.backend.domain.member.service.MemberService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +public class LoginController { + private final MemberService memberService; + + @ResponseStatus(HttpStatus.OK) + @PostMapping("/oauth2/sign-up") + public void signUp(@RequestBody SignUpDto dto) { + memberService.signUp(dto); + } + +} diff --git a/backend/core/src/main/java/com/rollthedice/backend/domain/member/entity/Role.java b/backend/core/src/main/java/com/rollthedice/backend/domain/member/entity/Role.java index d1d058e9..21808cbc 100644 --- a/backend/core/src/main/java/com/rollthedice/backend/domain/member/entity/Role.java +++ b/backend/core/src/main/java/com/rollthedice/backend/domain/member/entity/Role.java @@ -1,7 +1,7 @@ package com.rollthedice.backend.domain.member.entity; public enum Role { - ADMIN("ROLE_ADMIN"), + GUEST("ROLE_GUEST"), USER("ROLE_USER"); private final String role; diff --git a/backend/core/src/main/java/com/rollthedice/backend/global/oauth2/OAuthAttributes.java b/backend/core/src/main/java/com/rollthedice/backend/global/oauth2/OAuthAttributes.java index 2dfcb75c..f3aacaa1 100644 --- a/backend/core/src/main/java/com/rollthedice/backend/global/oauth2/OAuthAttributes.java +++ b/backend/core/src/main/java/com/rollthedice/backend/global/oauth2/OAuthAttributes.java @@ -41,7 +41,7 @@ public Member toEntity(SocialType socialType, OAuth2UserInfo oauth2UserInfo) { .email(oauth2UserInfo.getEmail()) .nickname(oauth2UserInfo.getNickname()) .imageUrl(oauth2UserInfo.getImageUrl()) - .role(Role.USER) + .role(Role.GUEST) .status(Status.ACTIVE) .build(); } From 5038f292a754d8efe653a8430d28bba8d79d961d Mon Sep 17 00:00:00 2001 From: yeonjy Date: Tue, 19 Mar 2024 17:45:34 +0900 Subject: [PATCH 3/5] =?UTF-8?q?refactor:=20=EC=82=AC=EC=9A=A9=EC=9E=90?= =?UTF-8?q?=EA=B0=80=20Guest=EB=9D=BC=EB=A9=B4=20=EB=8B=89=EB=84=A4?= =?UTF-8?q?=EC=9E=84=20=EC=9E=85=EB=A0=A5=20=ED=9A=8C=EC=9B=90=EA=B0=80?= =?UTF-8?q?=EC=9E=85=20=ED=8E=98=EC=9D=B4=EC=A7=80=EB=A1=9C=20=EC=9D=B4?= =?UTF-8?q?=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../handler/OAuth2LoginSuccessHandler.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/backend/core/src/main/java/com/rollthedice/backend/global/oauth2/handler/OAuth2LoginSuccessHandler.java b/backend/core/src/main/java/com/rollthedice/backend/global/oauth2/handler/OAuth2LoginSuccessHandler.java index f70467aa..4dc8696f 100644 --- a/backend/core/src/main/java/com/rollthedice/backend/global/oauth2/handler/OAuth2LoginSuccessHandler.java +++ b/backend/core/src/main/java/com/rollthedice/backend/global/oauth2/handler/OAuth2LoginSuccessHandler.java @@ -1,5 +1,6 @@ package com.rollthedice.backend.global.oauth2.handler; +import com.rollthedice.backend.domain.member.entity.Role; import com.rollthedice.backend.global.jwt.service.JwtService; import com.rollthedice.backend.global.oauth2.CustomOAuth2User; import jakarta.servlet.ServletException; @@ -21,10 +22,19 @@ public class OAuth2LoginSuccessHandler implements AuthenticationSuccessHandler { @Override public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { - log.info("OAuth2 Login 성공!"); - CustomOAuth2User oAuth2User = (CustomOAuth2User) authentication.getPrincipal(); - loginSuccess(response, oAuth2User); // 로그인에 성공한 경우 access, refresh 토큰 생성 + log.info("OAuth2 Login succeed."); + CustomOAuth2User oAuth2User = (CustomOAuth2User) authentication.getPrincipal(); + + if(oAuth2User.getRole() == Role.GUEST) { + String accessToken = jwtService.createAccessToken(oAuth2User.getEmail()); + response.addHeader(jwtService.getAccessHeader(), "Bearer " + accessToken); + response.sendRedirect("oauth2/sign-up"); + + jwtService.sendAccessAndRefreshToken(response, accessToken, null); + } else { + loginSuccess(response, oAuth2User); response.sendRedirect("/"); + } } private void loginSuccess(HttpServletResponse response, CustomOAuth2User oAuth2User) throws IOException { From f9ce0587c91fb04b92fd8540c539abaf601c5d5a Mon Sep 17 00:00:00 2001 From: yeonjy Date: Tue, 19 Mar 2024 17:46:20 +0900 Subject: [PATCH 4/5] =?UTF-8?q?feat:=20=ED=9A=8C=EC=9B=90=EA=B0=80?= =?UTF-8?q?=EC=9E=85=20=EC=8B=9C=20=EC=82=AC=EC=9A=A9=EC=9E=90=20=EB=8B=89?= =?UTF-8?q?=EB=84=A4=EC=9E=84=20=EC=9E=85=EB=A0=A5=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/domain/member/dto/SignUpDto.java | 10 ++++++++++ .../backend/domain/member/entity/Member.java | 4 ++++ .../domain/member/service/MemberService.java | 18 ++++++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 backend/core/src/main/java/com/rollthedice/backend/domain/member/dto/SignUpDto.java create mode 100644 backend/core/src/main/java/com/rollthedice/backend/domain/member/service/MemberService.java diff --git a/backend/core/src/main/java/com/rollthedice/backend/domain/member/dto/SignUpDto.java b/backend/core/src/main/java/com/rollthedice/backend/domain/member/dto/SignUpDto.java new file mode 100644 index 00000000..f9da9bfb --- /dev/null +++ b/backend/core/src/main/java/com/rollthedice/backend/domain/member/dto/SignUpDto.java @@ -0,0 +1,10 @@ +package com.rollthedice.backend.domain.member.dto; + +import lombok.Getter; +import lombok.NoArgsConstructor; + +@NoArgsConstructor +@Getter +public class SignUpDto { + private String nickname; +} diff --git a/backend/core/src/main/java/com/rollthedice/backend/domain/member/entity/Member.java b/backend/core/src/main/java/com/rollthedice/backend/domain/member/entity/Member.java index 1a7a83a4..f25a7332 100644 --- a/backend/core/src/main/java/com/rollthedice/backend/domain/member/entity/Member.java +++ b/backend/core/src/main/java/com/rollthedice/backend/domain/member/entity/Member.java @@ -40,4 +40,8 @@ public Member update(String email, String imageUrl) { public void updateRefreshToken(String updateRefreshToken) { this.refreshToken = updateRefreshToken; } + + public void updateNickname(String nickname) { + this.nickname = nickname; + } } diff --git a/backend/core/src/main/java/com/rollthedice/backend/domain/member/service/MemberService.java b/backend/core/src/main/java/com/rollthedice/backend/domain/member/service/MemberService.java new file mode 100644 index 00000000..f41faeb4 --- /dev/null +++ b/backend/core/src/main/java/com/rollthedice/backend/domain/member/service/MemberService.java @@ -0,0 +1,18 @@ +package com.rollthedice.backend.domain.member.service; + +import com.rollthedice.backend.domain.member.dto.SignUpDto; +import com.rollthedice.backend.domain.member.entity.Member; +import com.rollthedice.backend.domain.member.query.AuthService; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@RequiredArgsConstructor +@Service +public class MemberService { + private final AuthService authService; + + public void signUp(SignUpDto dto) { + Member member = authService.getMember(); + member.updateNickname(dto.getNickname()); + } +} From d5997ee37949c7d4571705ca7ed643b1d070aa8c Mon Sep 17 00:00:00 2001 From: yeonjy Date: Tue, 19 Mar 2024 18:06:12 +0900 Subject: [PATCH 5/5] =?UTF-8?q?feat:=20@Transactional=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/domain/member/service/MemberService.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/backend/core/src/main/java/com/rollthedice/backend/domain/member/service/MemberService.java b/backend/core/src/main/java/com/rollthedice/backend/domain/member/service/MemberService.java index f41faeb4..23233ae1 100644 --- a/backend/core/src/main/java/com/rollthedice/backend/domain/member/service/MemberService.java +++ b/backend/core/src/main/java/com/rollthedice/backend/domain/member/service/MemberService.java @@ -5,12 +5,14 @@ import com.rollthedice.backend.domain.member.query.AuthService; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; @RequiredArgsConstructor @Service public class MemberService { private final AuthService authService; + @Transactional public void signUp(SignUpDto dto) { Member member = authService.getMember(); member.updateNickname(dto.getNickname());