-
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 #79 from TRIP-Side-Project/dev
태그 & 소셜 로그인 기능 구현
- Loading branch information
Showing
21 changed files
with
361 additions
and
131 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
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
21 changes: 0 additions & 21 deletions
21
src/main/java/com/api/trip/common/security/oauth/OAuthFailureHandler.java
This file was deleted.
Oops, something went wrong.
85 changes: 52 additions & 33 deletions
85
src/main/java/com/api/trip/common/security/oauth/OAuthSuccessHandler.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 |
---|---|---|
@@ -1,63 +1,82 @@ | ||
package com.api.trip.common.security.oauth; | ||
|
||
import com.api.trip.common.security.jwt.JwtToken; | ||
import com.api.trip.common.security.jwt.JwtTokenProvider; | ||
import jakarta.servlet.ServletException; | ||
import com.api.trip.domain.member.controller.dto.LoginResponse; | ||
import com.api.trip.domain.member.model.Member; | ||
import com.api.trip.domain.member.model.MemberRole; | ||
import com.api.trip.domain.member.repository.MemberRepository; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.ResponseCookie; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.security.oauth2.core.user.OAuth2User; | ||
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
|
||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Optional; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class OAuthSuccessHandler extends SimpleUrlAuthenticationSuccessHandler { | ||
|
||
private final MemberRepository memberRepository; | ||
private final JwtTokenProvider jwtTokenProvider; | ||
private final ObjectMapper objectMapper; | ||
|
||
@Override | ||
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { | ||
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException { | ||
|
||
OAuth2User oAuth2User = (OAuth2User) authentication.getPrincipal(); | ||
log.debug("OAuth2User: {}", oAuth2User); | ||
|
||
String email = oAuth2User.getAttribute("email"); | ||
Optional<Member> findMember = memberRepository.findByEmail(email); | ||
|
||
String role = oAuth2User.getAuthorities().stream() | ||
.findFirst() | ||
.orElseThrow(IllegalAccessError::new) | ||
.getAuthority(); | ||
|
||
/** 미구현 | ||
if (isExist) { | ||
JwtToken jwtToken = jwtTokenProvider.createJwtToken(email, role); | ||
log.debug("JWT TOKEN: {} {}", jwtToken.getAccessToken(), jwtToken.getRefreshToken()); | ||
String targetUrl = UriComponentsBuilder.fromUriString("/") | ||
.queryParam("accessToken", jwtToken.getAccessToken()) | ||
.queryParam("refreshToken", jwtToken.getRefreshToken()) | ||
.build() | ||
.encode(StandardCharsets.UTF_8) | ||
.toUriString(); | ||
getRedirectStrategy().sendRedirect(request, response, targetUrl); | ||
} else { | ||
// 회원이 존재하는 않는 경우 회원 가입 후 토큰 발급 | ||
String targetUrl = UriComponentsBuilder.fromUriString("/") | ||
.queryParam("email", email) | ||
.queryParam("name", nickname) | ||
.build() | ||
.encode(StandardCharsets.UTF_8) | ||
.toUriString(); | ||
getRedirectStrategy().sendRedirect(request, response, targetUrl); | ||
// 회원이 아닌 경우에 회원 가입 진행 | ||
|
||
Long memberId = 0L; | ||
String role = ""; | ||
|
||
if (findMember.isEmpty()) { | ||
String name = oAuth2User.getAttribute("name"); | ||
String picture = oAuth2User.getAttribute("picture"); | ||
|
||
Member member = Member.of(email, "", name, picture); | ||
memberRepository.save(member); | ||
|
||
memberId = member.getId(); | ||
role = member.getRole().getValue(); | ||
} | ||
*/ | ||
|
||
// OAuth2User 객체에서 권한 가져옴 | ||
JwtToken jwtToken = jwtTokenProvider.createJwtToken(email, role); | ||
|
||
// 쿠키 세팅 | ||
response.addHeader(HttpHeaders.SET_COOKIE, createCookie("tokenType", "Bearer")); | ||
response.addHeader(HttpHeaders.SET_COOKIE, createCookie("accessToken", jwtToken.getAccessToken())); | ||
response.addHeader(HttpHeaders.SET_COOKIE, createCookie("refreshToken", jwtToken.getRefreshToken())); | ||
response.addHeader(HttpHeaders.SET_COOKIE, createCookie("memberId", String.valueOf(memberId))); | ||
response.sendRedirect("/home"); | ||
} | ||
|
||
private static String createCookie(String name, String value) { | ||
return ResponseCookie.from(name, value) | ||
.path("/") | ||
.httpOnly(true) | ||
.sameSite("None") | ||
.secure(true) | ||
.build() | ||
.toString(); | ||
} | ||
|
||
} |
2 changes: 0 additions & 2 deletions
2
src/main/java/com/api/trip/domain/interestitem/controller/dto/CreateInterestItemRequest.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
3 changes: 1 addition & 2 deletions
3
src/main/java/com/api/trip/domain/interestitem/controller/dto/GetInterestItemsResponse.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
2 changes: 1 addition & 1 deletion
2
...rip/domain/interestitem/InterestItem.java → ...main/interestitem/model/InterestItem.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
3 changes: 1 addition & 2 deletions
3
src/main/java/com/api/trip/domain/interestitem/repository/InterestItemRepository.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
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.