-
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 #30 from Central-MakeUs/dev
fix: #22 token 뿌리기 롤백
- Loading branch information
Showing
1 changed file
with
54 additions
and
55 deletions.
There are no files selected for viewing
109 changes: 54 additions & 55 deletions
109
core/core-security/src/main/java/com/mm/coresecurity/oauth/OAuth2AuthSuccessHandler.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,86 +1,85 @@ | ||
package com.mm.coresecurity.oauth; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.oauth2.core.user.DefaultOAuth2User; | ||
import org.springframework.security.web.authentication.AuthenticationSuccessHandler; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import com.mm.coredomain.domain.Groups; | ||
import com.mm.coredomain.domain.Member; | ||
import com.mm.coredomain.domain.OAuthProvider; | ||
import com.mm.coredomain.repository.GroupRepository; | ||
import com.mm.coredomain.repository.MemberRepository; | ||
import com.mm.coreinfraredis.repository.RedisRefreshTokenRepository; | ||
import com.mm.coresecurity.jwt.JwtTokenProvider; | ||
|
||
import com.mm.coresecurity.util.HttpResponseUtil; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.oauth2.core.user.DefaultOAuth2User; | ||
import org.springframework.security.web.authentication.AuthenticationSuccessHandler; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Slf4j | ||
@Component | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class OAuth2AuthSuccessHandler implements AuthenticationSuccessHandler { | ||
private final JwtTokenProvider jwtTokenProvider; | ||
private final MemberRepository memberRepository; | ||
private final GroupRepository groupRepository; | ||
private final RedisRefreshTokenRepository redisRefreshTokenRepository; | ||
private final JwtTokenProvider jwtTokenProvider; | ||
private final MemberRepository memberRepository; | ||
private final GroupRepository groupRepository; | ||
private final RedisRefreshTokenRepository redisRefreshTokenRepository; | ||
|
||
@Override | ||
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, | ||
Authentication authentication) throws IOException, ServletException { | ||
DefaultOAuth2User principal = (DefaultOAuth2User)authentication.getPrincipal(); | ||
@Override | ||
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, | ||
Authentication authentication) throws IOException, ServletException { | ||
DefaultOAuth2User principal = (DefaultOAuth2User) authentication.getPrincipal(); | ||
|
||
Map<String, String> properties = principal.getAttribute("kakao_account"); | ||
String email = properties.get("email"); | ||
Map<String, String> properties = principal.getAttribute("kakao_account"); | ||
String email = properties.get("email"); | ||
|
||
Member member = getMemberElseCreateMember(email); | ||
List<SimpleGrantedAuthority> authorities = member.getGroups() | ||
.getGroupPermissions() | ||
.stream() | ||
.map(groupPermission -> new SimpleGrantedAuthority(groupPermission.getPermission().getName())) | ||
.toList(); | ||
Member member = getMemberElseCreateMember(email); | ||
List<SimpleGrantedAuthority> authorities = member.getGroups() | ||
.getGroupPermissions() | ||
.stream() | ||
.map(groupPermission -> new SimpleGrantedAuthority(groupPermission.getPermission().getName())) | ||
.toList(); | ||
|
||
// TODO provider kakao, apple 구분 | ||
OAuth2UserDetails userDetails = OAuth2UserDetails.builder() | ||
.id(member.getId()) | ||
.provider(OAuthProvider.KAKAO) | ||
.authorities(authorities) | ||
.build(); | ||
// TODO provider kakao, apple 구분 | ||
OAuth2UserDetails userDetails = OAuth2UserDetails.builder() | ||
.id(member.getId()) | ||
.provider(OAuthProvider.KAKAO) | ||
.authorities(authorities) | ||
.build(); | ||
|
||
String accessToken = jwtTokenProvider.generateAccessToken(userDetails); | ||
String refreshToken = jwtTokenProvider.generateRefreshToken(); | ||
String accessToken = jwtTokenProvider.generateAccessToken(userDetails); | ||
String refreshToken = jwtTokenProvider.generateRefreshToken(); | ||
|
||
redisRefreshTokenRepository.save(refreshToken, member.getId()); | ||
redisRefreshTokenRepository.save(refreshToken, member.getId()); | ||
|
||
Map<String, String> tokenMap = new HashMap<>(); | ||
tokenMap.put("accessToken", accessToken); | ||
tokenMap.put("refreshToken", refreshToken); | ||
Map<String, String> tokenMap = new HashMap<>(); | ||
tokenMap.put("accessToken", accessToken); | ||
tokenMap.put("refreshToken", refreshToken); | ||
|
||
log.info(">>>>>>>>>>>>>> OAUTH2 handler"); | ||
log.info(">>>>>>>>>>>>>> OAUTH2 handler"); | ||
|
||
response.addHeader("Authorization", "Bearer " + accessToken); | ||
// HttpResponseUtil.writeSuccessResponse(response, tokenMap); | ||
} | ||
response.addHeader("Authorization", "Bearer " + accessToken); | ||
HttpResponseUtil.writeSuccessResponse(response, tokenMap); | ||
} | ||
|
||
private Member getMemberElseCreateMember(String email) { | ||
return memberRepository.findByEmail(email).orElseGet(() -> { | ||
Groups userGroup = groupRepository.findByName("USER_GROUP").orElseThrow(RuntimeException::new); | ||
Member member = Member.builder() | ||
.email(email) | ||
.groups(userGroup) | ||
.build(); | ||
return memberRepository.save(member); | ||
}); | ||
} | ||
private Member getMemberElseCreateMember(String email) { | ||
return memberRepository.findByEmail(email).orElseGet(() -> { | ||
Groups userGroup = groupRepository.findByName("USER_GROUP").orElseThrow(RuntimeException::new); | ||
Member member = Member.builder() | ||
.email(email) | ||
.groups(userGroup) | ||
.build(); | ||
return memberRepository.save(member); | ||
}); | ||
} | ||
} |