-
Notifications
You must be signed in to change notification settings - Fork 3
구글 OAuth2 로그인 기능 #156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Goldbar97
wants to merge
3
commits into
ESJung95:develop
Choose a base branch
from
Goldbar97:feat/OAuth2Google
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
구글 OAuth2 로그인 기능 #156
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
56 changes: 56 additions & 0 deletions
56
...in/java/com/ctrls/auto_enter_view/component/GoogleOAuth2AuthenticationSuccessHandler.java
This file contains hidden or 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,56 @@ | ||
| package com.ctrls.auto_enter_view.component; | ||
|
|
||
| import com.ctrls.auto_enter_view.entity.CandidateEntity; | ||
| import com.ctrls.auto_enter_view.enums.UserRole; | ||
| import com.ctrls.auto_enter_view.repository.CandidateRepository; | ||
| import com.ctrls.auto_enter_view.security.JwtTokenProvider; | ||
| import jakarta.servlet.ServletException; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import java.io.IOException; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.security.core.Authentication; | ||
| import org.springframework.security.oauth2.core.user.OAuth2User; | ||
| import org.springframework.security.web.authentication.AuthenticationSuccessHandler; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class GoogleOAuth2AuthenticationSuccessHandler implements AuthenticationSuccessHandler { | ||
|
|
||
| private final CandidateRepository candidateRepository; | ||
| private final KeyGenerator keyGenerator; | ||
| private final JwtTokenProvider jwtTokenProvider; | ||
|
|
||
| @Override | ||
| @Transactional | ||
| public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, | ||
| Authentication authentication) throws IOException, ServletException { | ||
|
|
||
| OAuth2User oAuth2User = (OAuth2User) authentication.getPrincipal(); | ||
|
|
||
| CandidateEntity candidateEntity = candidateRepository.findByCandidateKey(oAuth2User.getName()) | ||
| .orElseGet(() -> { | ||
| CandidateEntity newCandidate = CandidateEntity.builder() | ||
| .candidateKey(oAuth2User.getName()) | ||
| .name(oAuth2User.getAttribute("name")) | ||
| .email(oAuth2User.getAttribute("email")) | ||
| .password(keyGenerator.generateKey()) | ||
| .phoneNumber("undefinedNumber") | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 일단 이렇게 생성해서 DB 저장시킨거죠..? 따로 핸드폰번호까지 리다이렉트해서 받아오는 로직은 아직 없는거 맞나요?ㅜㅜ
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 네 일단 저렇게 회원 엔티티 만들어지게 했습니다 |
||
| .role(UserRole.ROLE_CANDIDATE) | ||
| .build(); | ||
|
|
||
| candidateRepository.save(newCandidate); | ||
|
|
||
| return newCandidate; | ||
| }); | ||
|
|
||
| String token = jwtTokenProvider.generateToken(candidateEntity.getEmail(), | ||
| candidateEntity.getRole()); | ||
|
|
||
| response.setHeader("Authorization", "Bearer " + token); | ||
| response.setContentType("application/json"); | ||
| response.sendRedirect("/common/job-postings"); | ||
| } | ||
| } | ||
40 changes: 40 additions & 0 deletions
40
src/main/java/com/ctrls/auto_enter_view/dto/candidate/GoogleOAuth2User.java
This file contains hidden or 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,40 @@ | ||
| package com.ctrls.auto_enter_view.dto.candidate; | ||
|
|
||
| import com.ctrls.auto_enter_view.enums.UserRole; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.Map; | ||
| import lombok.AllArgsConstructor; | ||
| import org.springframework.security.core.GrantedAuthority; | ||
| import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
| import org.springframework.security.oauth2.core.user.OAuth2User; | ||
|
|
||
| @AllArgsConstructor | ||
| public class GoogleOAuth2User implements OAuth2User { | ||
|
|
||
| private final Map<String, Object> map; | ||
|
|
||
| @Override | ||
| public <A> A getAttribute(String name) { | ||
|
|
||
| return OAuth2User.super.getAttribute(name); | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, Object> getAttributes() { | ||
|
|
||
| return map; | ||
| } | ||
|
|
||
| @Override | ||
| public Collection<? extends GrantedAuthority> getAuthorities() { | ||
|
|
||
| return Collections.singletonList(new SimpleGrantedAuthority(UserRole.ROLE_CANDIDATE.name())); | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
|
|
||
| return (String) getAttributes().get("key"); | ||
| } | ||
| } |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/ctrls/auto_enter_view/dto/candidate/GoogleResponse.java
This file contains hidden or 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,34 @@ | ||
| package com.ctrls.auto_enter_view.dto.candidate; | ||
|
|
||
| import java.util.Map; | ||
| import lombok.AllArgsConstructor; | ||
|
|
||
| @AllArgsConstructor | ||
| public class GoogleResponse implements OAuth2Response { | ||
|
|
||
| private final Map<String, Object> attribute; | ||
|
|
||
| @Override | ||
| public String getProvider() { | ||
|
|
||
| return "google"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getProviderId() { | ||
|
|
||
| return attribute.get("sub").toString(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getEmail() { | ||
|
|
||
| return attribute.get("email").toString(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
|
|
||
| return attribute.get("name").toString(); | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/ctrls/auto_enter_view/dto/candidate/OAuth2Response.java
This file contains hidden or 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,12 @@ | ||
| package com.ctrls.auto_enter_view.dto.candidate; | ||
|
|
||
| public interface OAuth2Response { | ||
|
|
||
| String getProvider(); | ||
|
|
||
| String getProviderId(); | ||
|
|
||
| String getEmail(); | ||
|
|
||
| String getName(); | ||
| } |
This file contains hidden or 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
42 changes: 42 additions & 0 deletions
42
src/main/java/com/ctrls/auto_enter_view/service/GoogleOAuth2Service.java
This file contains hidden or 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,42 @@ | ||
| package com.ctrls.auto_enter_view.service; | ||
|
|
||
| import com.ctrls.auto_enter_view.dto.candidate.GoogleOAuth2User; | ||
| import com.ctrls.auto_enter_view.dto.candidate.GoogleResponse; | ||
| import com.ctrls.auto_enter_view.dto.candidate.OAuth2Response; | ||
| import java.util.Map; | ||
| 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.OAuth2Error; | ||
| import org.springframework.security.oauth2.core.OAuth2ErrorCodes; | ||
| import org.springframework.security.oauth2.core.user.OAuth2User; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Service | ||
| public class GoogleOAuth2Service extends DefaultOAuth2UserService { | ||
|
|
||
| @Override | ||
| @Transactional | ||
| public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException { | ||
|
|
||
| OAuth2User oAuth2User = super.loadUser(userRequest); | ||
| String registrationId = userRequest.getClientRegistration().getRegistrationId(); | ||
| OAuth2Response oAuth2Response; | ||
|
|
||
| if (registrationId.equals("google")) { | ||
| oAuth2Response = new GoogleResponse(oAuth2User.getAttributes()); | ||
| } else { | ||
| throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_REQUEST)); | ||
| } | ||
|
|
||
| String key = oAuth2Response.getProvider() + "-" + oAuth2Response.getProviderId(); | ||
| Map<String, Object> immuatableMap = Map.of( | ||
| "key", key, | ||
| "name", oAuth2Response.getName(), | ||
| "email", oAuth2Response.getEmail() | ||
| ); | ||
|
|
||
| return new GoogleOAuth2User(immuatableMap); | ||
| } | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이거 추가안해줘도 되는 줄 알았는데.. 아니군요ㅜ.ㅜ
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ESJung95 님은 라이브러리 추가 없이 작동됐나요?