-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from tukcomCD2024/refactor#62/oauth2-login
Refactor#62/oauth2 login
- Loading branch information
Showing
31 changed files
with
349 additions
and
444 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
backend/memetory/src/main/java/com/example/memetory/domain/auth/controller/AuthApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.example.memetory.domain.auth.controller; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
|
||
import com.example.memetory.domain.auth.dto.LoginRequest; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.Parameter; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
|
||
@Tag(name = "Auth") | ||
public interface AuthApi { | ||
|
||
@Operation( | ||
summary = "로그인", | ||
description = "앱에서 받아온 인증서버의 access_token을 통해서 우리 서버의 JWT를 받아가는 과정" | ||
+ "처음 로그인 할 경우 DB에 사용자 등록이 진행된다." | ||
) | ||
@ApiResponses( | ||
@ApiResponse( | ||
responseCode = "200", | ||
description = "로그인 성공, header의 Authorization과 Authorization-refresh를 확인" | ||
) | ||
) | ||
ResponseEntity<HttpStatus> login(LoginRequest request, | ||
@Parameter(hidden = true) HttpServletResponse response); | ||
} |
25 changes: 25 additions & 0 deletions
25
...nd/memetory/src/main/java/com/example/memetory/domain/auth/controller/AuthController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.example.memetory.domain.auth.controller; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.example.memetory.domain.auth.dto.LoginRequest; | ||
import com.example.memetory.domain.auth.service.AuthService; | ||
|
||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class AuthController implements AuthApi { | ||
private final AuthService authService; | ||
|
||
@PostMapping("/login") | ||
public ResponseEntity<HttpStatus> login(@RequestBody LoginRequest request, HttpServletResponse response) { | ||
authService.authenticateOrRegisterUser(request, response); | ||
return new ResponseEntity<>(HttpStatus.OK); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
backend/memetory/src/main/java/com/example/memetory/domain/auth/dto/LoginRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.example.memetory.domain.auth.dto; | ||
|
||
import com.example.memetory.domain.member.entity.SocialType; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
@Schema(description = "로그인 포맷") | ||
public class LoginRequest { | ||
@Schema(description = "인증서버에서 받아온 access token을 입력") | ||
private String token; | ||
@Schema(description = "인증서버타입, 현재는 google만 가능") | ||
private SocialType socialType; | ||
} |
52 changes: 52 additions & 0 deletions
52
backend/memetory/src/main/java/com/example/memetory/domain/auth/service/AuthService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.example.memetory.domain.auth.service; | ||
|
||
import java.util.UUID; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Service; | ||
|
||
import com.example.memetory.domain.auth.dto.LoginRequest; | ||
import com.example.memetory.domain.auth.userInfo.OAuth2UserInfo; | ||
import com.example.memetory.domain.member.entity.Member; | ||
import com.example.memetory.domain.member.entity.Role; | ||
import com.example.memetory.domain.member.entity.SocialType; | ||
import com.example.memetory.domain.member.repository.MemberRepository; | ||
import com.example.memetory.global.security.jwt.service.JwtService; | ||
|
||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class AuthService { | ||
private static final Logger log = LoggerFactory.getLogger(AuthService.class); | ||
private final MemberRepository memberRepository; | ||
private final OAuth2ProviderService oAuth2ProviderService; | ||
private final JwtService jwtService; | ||
|
||
public void authenticateOrRegisterUser(LoginRequest loginRequest, HttpServletResponse response) { | ||
OAuth2UserInfo userInfo = oAuth2ProviderService.getUserInfo(loginRequest); | ||
Member member = findOrElseRegisterMember(userInfo, loginRequest.getSocialType()); | ||
jwtService.sendAccessAndRefreshToken(response, member.getEmail()); | ||
} | ||
|
||
private Member findOrElseRegisterMember(OAuth2UserInfo userInfo, SocialType socialType) { | ||
return memberRepository.findBySocialTypeAndSocialId(socialType, userInfo.getId()) | ||
.orElse(registerMember(socialType, userInfo)); | ||
} | ||
|
||
private Member registerMember(SocialType socialType, OAuth2UserInfo userInfo) { | ||
Member member = Member.builder() | ||
.socialType(socialType) | ||
.socialId(userInfo.getId()) | ||
.email(UUID.randomUUID() + "@socialUser.com") | ||
.name(userInfo.getName()) | ||
.nickname(String.valueOf(UUID.randomUUID())) | ||
.imageUrl(userInfo.getImageUrl()) | ||
.role(Role.USER) | ||
.build(); | ||
|
||
return memberRepository.save(member); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...emetory/src/main/java/com/example/memetory/domain/auth/service/OAuth2ProviderService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.example.memetory.domain.auth.service; | ||
|
||
import static com.example.memetory.domain.member.entity.SocialType.*; | ||
|
||
import java.util.Map; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
|
||
import com.example.memetory.domain.auth.dto.LoginRequest; | ||
import com.example.memetory.domain.auth.userInfo.GoogleOAuth2UserInfo; | ||
import com.example.memetory.domain.auth.userInfo.OAuth2UserInfo; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class OAuth2ProviderService { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(OAuth2ProviderService.class); | ||
|
||
public OAuth2UserInfo getUserInfo(LoginRequest request) { | ||
return switch (request.getSocialType()) { | ||
case GOOGLE -> getGoogleUserInfo(request); | ||
}; | ||
} | ||
|
||
private OAuth2UserInfo getGoogleUserInfo(LoginRequest request) { | ||
Map attributes = WebClient.create(GOOGLE.getProviderUrl()) | ||
.get() | ||
.headers(httpHeaders -> { | ||
httpHeaders.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); | ||
httpHeaders.setBearerAuth(request.getToken()); | ||
}) | ||
.accept(MediaType.APPLICATION_JSON) | ||
.retrieve() | ||
.bodyToMono(Map.class) | ||
.log() | ||
.block(); | ||
|
||
return new GoogleOAuth2UserInfo(attributes); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.