-
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.
Browse files
Browse the repository at this point in the history
강제 머지.. 죄송티비..
- Loading branch information
Showing
18 changed files
with
370 additions
and
110 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
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
33 changes: 33 additions & 0 deletions
33
api/src/main/java/com/org/gunbbang/login/CustomOAuth2User.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,33 @@ | ||
package com.org.gunbbang.login; | ||
|
||
import com.org.gunbbang.Role; | ||
import java.util.Collection; | ||
import java.util.Map; | ||
import lombok.Getter; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.oauth2.core.user.DefaultOAuth2User; | ||
|
||
@Getter | ||
public class CustomOAuth2User extends DefaultOAuth2User { | ||
private Long memberId; | ||
private String email; | ||
private Role role; | ||
|
||
public CustomOAuth2User( | ||
Collection<? extends GrantedAuthority> authorities, | ||
Map<String, Object> attributes, | ||
String nameAttributeKey, | ||
Long memberId, | ||
String email, | ||
Role role) { | ||
super(authorities, attributes, nameAttributeKey); | ||
this.memberId = memberId; | ||
this.email = email; | ||
this.role = role; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "CustomOAuth2User{" + "email='" + email + '\'' + ", role=" + role + '}'; | ||
} | ||
} |
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
61 changes: 61 additions & 0 deletions
61
api/src/main/java/com/org/gunbbang/login/OAuthAttributes.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,61 @@ | ||
package com.org.gunbbang.login; | ||
|
||
import com.org.gunbbang.PlatformType; | ||
import com.org.gunbbang.Role; | ||
import com.org.gunbbang.entity.Member; | ||
import com.org.gunbbang.login.userinfo.KakaoOAuth2UserInfo; | ||
import com.org.gunbbang.login.userinfo.OAuth2UserInfo; | ||
import com.org.gunbbang.repository.BreadTypeRepository; | ||
import com.org.gunbbang.repository.NutrientTypeRepository; | ||
import java.util.Map; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
// 소셜별로 받아오는 데이터가 다르므로 데이터 분기 처리하는 DTO 클래스 | ||
@Getter | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
@Builder | ||
public class OAuthAttributes { | ||
private String nameAttributeKey; // OAuth2 로그인 진행 시 키가 되는 필드 값 | ||
private OAuth2UserInfo oauth2UserInfo; // 소셜 타입별 로그인 유저 정보 | ||
private BreadTypeRepository breadTypeRepository; | ||
private NutrientTypeRepository nutrientTypeRepository; | ||
|
||
/** | ||
* SocialType에 맞는 메소드 호출하여 OAuthAttributes 객체 변환 파라미터: userNameAttributeName -> OAuth2 로그인 시 | ||
* 키가(pk) 되는 값 / attribute OAuth 서비스의 유저 정보들 소셜 별 of 메서드 -> 각각 소셜 로그인 API에서 제공하는 회원의 식별값, | ||
* attribute, nameAttribute를 저장 후 build | ||
*/ | ||
public static OAuthAttributes of( | ||
PlatformType platformType, String userNameAttribute, Map<String, Object> attributes) { | ||
if (platformType == PlatformType.APPLE) { | ||
// return ofAppple(userNameAttribute, attributes); | ||
} | ||
return ofKakao(userNameAttribute, attributes); | ||
} | ||
|
||
private static OAuthAttributes ofKakao( | ||
String userNameAttributeName, Map<String, Object> attribute) { | ||
return OAuthAttributes.builder() | ||
.nameAttributeKey(userNameAttributeName) | ||
.oauth2UserInfo(new KakaoOAuth2UserInfo(attribute)) | ||
.build(); | ||
} | ||
|
||
// apple 로그인 필요한 정보 어떤식으로 넘기는지 찾아봐야 함 | ||
// private static OAuthAttribute ofApple(){ | ||
// | ||
// } | ||
|
||
// TODO: breadtype, mainpurpose, | ||
public Member toEntity(PlatformType platformType, OAuth2UserInfo oauth2UserInfo) { | ||
return Member.builder() | ||
.platformType(platformType) | ||
.email(oauth2UserInfo.getEmail()) | ||
.nickname("GUEST") | ||
.role(Role.GUEST) | ||
.build(); | ||
} | ||
} |
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
23 changes: 23 additions & 0 deletions
23
api/src/main/java/com/org/gunbbang/login/handler/OAuth2LoginFailureHandler.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,23 @@ | ||
package com.org.gunbbang.login.handler; | ||
|
||
import java.io.IOException; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.web.authentication.AuthenticationFailureHandler; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Slf4j | ||
@Component | ||
public class OAuth2LoginFailureHandler implements AuthenticationFailureHandler { | ||
@Override | ||
public void onAuthenticationFailure( | ||
HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) | ||
throws IOException, ServletException { | ||
response.setStatus(HttpServletResponse.SC_BAD_REQUEST); | ||
response.getWriter().write("소셜 로그인 실패! 서버 로그를 확인해주세요."); | ||
log.info("소셜 로그인에 실패했습니다. 에러메시지: {}", exception.getMessage()); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
api/src/main/java/com/org/gunbbang/login/handler/OAuth2LoginSuccessHandler.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,55 @@ | ||
package com.org.gunbbang.login.handler; | ||
|
||
import com.org.gunbbang.Role; | ||
import com.org.gunbbang.jwt.service.JwtService; | ||
import com.org.gunbbang.login.CustomOAuth2User; | ||
import java.io.IOException; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.web.authentication.AuthenticationSuccessHandler; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class OAuth2LoginSuccessHandler implements AuthenticationSuccessHandler { | ||
private final JwtService jwtService; | ||
|
||
@Override | ||
public void onAuthenticationSuccess( | ||
HttpServletRequest request, HttpServletResponse response, Authentication authentication) | ||
throws IOException, ServletException { | ||
System.out.println("OAuth2 Login 성공!"); | ||
log.info("OAuth2 Login 성공!"); | ||
try { | ||
CustomOAuth2User oAuth2User = (CustomOAuth2User) authentication.getPrincipal(); | ||
System.out.println("oAuth2User = " + oAuth2User); | ||
|
||
// User의 Role이 GUEST일 경우 처음 요청한 회원이므로 닉네임 설정으로 갈 수 있도록 값을 알려줌 | ||
// TODO: 닉네임 업데이트 시 Role.GUEST인 경우에는 변경 완료되면 Role을 업데이트해야함 | ||
if (oAuth2User.getRole() == Role.GUEST) { | ||
String accessToken = jwtService.createAccessToken(oAuth2User.getEmail(), 6L); | ||
response.addHeader(jwtService.getAccessHeader(), "Bearer " + accessToken); | ||
} else { | ||
loginSuccess(response, oAuth2User); | ||
} | ||
} catch (Exception e) { | ||
throw e; | ||
} | ||
} | ||
|
||
private void loginSuccess(HttpServletResponse response, CustomOAuth2User oAuth2User) | ||
throws IOException { | ||
String accessToken = jwtService.createAccessToken(oAuth2User.getEmail(), 6L); | ||
String refreshToken = jwtService.createRefreshToken(); | ||
response.addHeader(jwtService.getAccessHeader(), "Bearer " + accessToken); | ||
response.addHeader(jwtService.getRefreshHeader(), "Bearer " + accessToken); | ||
|
||
jwtService.sendAccessAndRefreshToken(response, accessToken, refreshToken); | ||
jwtService.updateRefreshToken(oAuth2User.getEmail(), refreshToken); | ||
} | ||
} |
Oops, something went wrong.