-
Notifications
You must be signed in to change notification settings - Fork 3
Naver OAuth2 회원가입 및 로그인 #157
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
IM-GYURI
wants to merge
15
commits into
ESJung95:develop
Choose a base branch
from
IM-GYURI:feature/naver-oauth
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
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
cad1ffb
Merge pull request #147 from ESJung95/develop
GLORY-JI b0c0f8d
Merge pull request #149 from ESJung95/develop
ESJung95 9cb2736
Merge pull request #152 from ESJung95/develop
IM-GYURI 1e0edd9
Merge pull request #154 from ESJung95/develop
IM-GYURI 3cb5686
feat: Update GitHub Actions workflow
ESJung95 b8d8e60
Merge pull request #155 from ESJung95/develop
ESJung95 3761773
feat : add dependency
IM-GYURI 58a45bf
fix : fix highlight magenta
IM-GYURI d711258
refactor : delete PasswordEncoder and add oauth2Login()
IM-GYURI fc02446
feat : add NaverOAuth2AuthenticationSuccessHandler
IM-GYURI 310a640
feat : add NaverOAuth2Service
IM-GYURI 379188c
feat : add NaverOAuth2User
IM-GYURI d1e6b95
feat : add NaverResponse
IM-GYURI 2e1363d
feat : add OAuth2Response
IM-GYURI 8a2ee01
feat : add PasswordEncoderConfig
IM-GYURI 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,8 @@ name: Merge Dev to Main | |
| on: | ||
| push: | ||
| branches: [ main ] | ||
| paths-ignore: | ||
| - 'README.md' | ||
|
|
||
| jobs: | ||
| build: | ||
|
|
||
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
60 changes: 60 additions & 0 deletions
60
...ain/java/com/ctrls/auto_enter_view/component/NaverOAuth2AuthenticationSuccessHandler.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,60 @@ | ||
| package com.ctrls.auto_enter_view.component; | ||
|
|
||
| import static com.ctrls.auto_enter_view.enums.UserRole.ROLE_CANDIDATE; | ||
|
|
||
| import com.ctrls.auto_enter_view.entity.CandidateEntity; | ||
| import com.ctrls.auto_enter_view.repository.CandidateRepository; | ||
| import com.ctrls.auto_enter_view.security.JwtTokenProvider; | ||
| import com.ctrls.auto_enter_view.util.RandomGenerator; | ||
| 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.crypto.password.PasswordEncoder; | ||
| 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 NaverOAuth2AuthenticationSuccessHandler implements AuthenticationSuccessHandler { | ||
|
|
||
| private final CandidateRepository candidateRepository; | ||
| private final KeyGenerator keyGenerator; | ||
| private final JwtTokenProvider jwtTokenProvider; | ||
| private final PasswordEncoder passwordEncoder; | ||
|
|
||
| @Override | ||
| @Transactional | ||
| public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, | ||
| Authentication authentication) throws IOException, ServletException { | ||
| OAuth2User oAuth2User = (OAuth2User) authentication.getPrincipal(); | ||
|
|
||
| CandidateEntity candidateEntity = candidateRepository.findByEmail( | ||
| oAuth2User.getAttribute("email")) | ||
| .orElseGet(() -> { | ||
| CandidateEntity newCandidate = CandidateEntity.builder() | ||
| .candidateKey(keyGenerator.generateKey()) | ||
| .name(oAuth2User.getAttribute("name")) | ||
| .email(oAuth2User.getAttribute("email")) | ||
| .password(passwordEncoder.encode(RandomGenerator.generateTemporaryPassword())) | ||
| .phoneNumber(oAuth2User.getAttribute("mobile")) | ||
| .role(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"); | ||
| } | ||
| } |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/ctrls/auto_enter_view/component/PasswordEncoderConfig.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,15 @@ | ||
| package com.ctrls.auto_enter_view.component; | ||
|
|
||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
| import org.springframework.security.crypto.password.PasswordEncoder; | ||
|
|
||
| @Configuration | ||
| public class PasswordEncoderConfig { | ||
|
|
||
| @Bean | ||
| public PasswordEncoder passwordEncoder() { | ||
| return new BCryptPasswordEncoder(); | ||
| } | ||
| } |
36 changes: 36 additions & 0 deletions
36
src/main/java/com/ctrls/auto_enter_view/dto/candidate/NaverOAuth2User.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,36 @@ | ||
| 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 NaverOAuth2User implements OAuth2User { | ||
|
|
||
| private final Map<String, Object> attributes; | ||
|
|
||
| @Override | ||
| public <A> A getAttribute(String name) { | ||
| return (A) attributes.get(name); | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, Object> getAttributes() { | ||
| return attributes; | ||
| } | ||
|
|
||
| @Override | ||
| public Collection<? extends GrantedAuthority> getAuthorities() { | ||
| return Collections.singletonList(new SimpleGrantedAuthority(UserRole.ROLE_CANDIDATE.name())); | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return (String) attributes.get("key"); | ||
| } | ||
| } |
43 changes: 43 additions & 0 deletions
43
src/main/java/com/ctrls/auto_enter_view/dto/candidate/NaverResponse.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,43 @@ | ||
| package com.ctrls.auto_enter_view.dto.candidate; | ||
|
|
||
| import java.util.Map; | ||
| import lombok.AllArgsConstructor; | ||
|
|
||
| @AllArgsConstructor | ||
| public class NaverResponse implements OAuth2Response { | ||
|
|
||
| private final Map<String, Object> attribute; | ||
|
|
||
| @Override | ||
| public String getProvider() { | ||
| return "naver"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getProviderId() { | ||
| Map<String, Object> response = (Map<String, Object>) attribute.get("response"); | ||
| Object providerId = response != null ? response.get("id") : null; | ||
| return providerId != null ? providerId.toString() : null; | ||
| } | ||
|
|
||
| @Override | ||
| public String getEmail() { | ||
| Map<String, Object> response = (Map<String, Object>) attribute.get("response"); | ||
| Object email = response != null ? response.get("email") : null; | ||
| return email != null ? email.toString() : null; | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| Map<String, Object> response = (Map<String, Object>) attribute.get("response"); | ||
| Object name = response != null ? response.get("name") : null; | ||
| return name != null ? name.toString() : null; | ||
| } | ||
|
|
||
| @Override | ||
| public String getNumber() { | ||
| Map<String, Object> response = (Map<String, Object>) attribute.get("response"); | ||
| Object number = response != null ? response.get("mobile") : null; | ||
| return number != null ? number.toString() : null; | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
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,14 @@ | ||
| package com.ctrls.auto_enter_view.dto.candidate; | ||
|
|
||
| public interface OAuth2Response { | ||
|
|
||
| String getProvider(); | ||
|
|
||
| String getProviderId(); | ||
|
|
||
| String getEmail(); | ||
|
|
||
| String getName(); | ||
|
|
||
| String getNumber(); | ||
| } |
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
52 changes: 52 additions & 0 deletions
52
src/main/java/com/ctrls/auto_enter_view/service/NaverOAuth2Service.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,52 @@ | ||
| package com.ctrls.auto_enter_view.service; | ||
|
|
||
| import com.ctrls.auto_enter_view.dto.candidate.NaverOAuth2User; | ||
| 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 NaverOAuth2Service extends DefaultOAuth2UserService { | ||
|
|
||
| @Override | ||
| @Transactional | ||
| public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException { | ||
| OAuth2User oAuth2User = super.loadUser(userRequest); | ||
| String registrationId = userRequest.getClientRegistration().getRegistrationId(); | ||
| Map<String, Object> attributes; | ||
|
|
||
| if (registrationId.equals("naver")) { | ||
| attributes = oAuth2User.getAttributes(); | ||
|
|
||
| Map<String, Object> response = (Map<String, Object>) attributes.get("response"); | ||
| if (response == null) { | ||
| throw new OAuth2AuthenticationException( | ||
| new OAuth2Error(OAuth2ErrorCodes.INVALID_REQUEST, "No 'response' field in attributes", | ||
| null)); | ||
| } | ||
|
|
||
| String key = "naver-" + response.get("id"); | ||
|
|
||
| Object name = response.get("name"); | ||
| Object email = response.get("email"); | ||
| Object mobile = response.get("mobile"); | ||
|
|
||
| Map<String, Object> immutableMap = Map.of( | ||
| "key", key, | ||
| "name", name != null ? name.toString() : null, | ||
| "email", email != null ? email.toString() : null, | ||
| "mobile", mobile != null ? mobile.toString() : null | ||
| ); | ||
|
|
||
| return new NaverOAuth2User(immutableMap); | ||
| } else { | ||
| throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_REQUEST)); | ||
| } | ||
| } | ||
| } | ||
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.
Value 값을 String 으로 넣어주더라도 나중에 사용할 때 다시 Object 로 될텐데 null 확인을 하는 이유가 따로 있으신가요?
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.
사실 NullPointerException이 떠서 그 부분을 해결하려고 하다보니 null 체크가 여기저기 남아있습니다. 필요 없는 부분들은 후에 삭제하겠습니다!