From 7c180e6b70c320ff627501e3bfd675a532156921 Mon Sep 17 00:00:00 2001 From: KarmaPol Date: Wed, 31 Jan 2024 22:18:06 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20#22=20token=20=EB=BF=8C=EB=A6=AC?= =?UTF-8?q?=EA=B8=B0=20=EB=A1=A4=EB=B0=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../oauth/OAuth2AuthSuccessHandler.java | 109 +++++++++--------- 1 file changed, 54 insertions(+), 55 deletions(-) diff --git a/core/core-security/src/main/java/com/mm/coresecurity/oauth/OAuth2AuthSuccessHandler.java b/core/core-security/src/main/java/com/mm/coresecurity/oauth/OAuth2AuthSuccessHandler.java index c2f6b55..418f078 100644 --- a/core/core-security/src/main/java/com/mm/coresecurity/oauth/OAuth2AuthSuccessHandler.java +++ b/core/core-security/src/main/java/com/mm/coresecurity/oauth/OAuth2AuthSuccessHandler.java @@ -1,17 +1,5 @@ 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; @@ -19,68 +7,79 @@ 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 properties = principal.getAttribute("kakao_account"); - String email = properties.get("email"); + Map properties = principal.getAttribute("kakao_account"); + String email = properties.get("email"); - Member member = getMemberElseCreateMember(email); - List authorities = member.getGroups() - .getGroupPermissions() - .stream() - .map(groupPermission -> new SimpleGrantedAuthority(groupPermission.getPermission().getName())) - .toList(); + Member member = getMemberElseCreateMember(email); + List 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 tokenMap = new HashMap<>(); - tokenMap.put("accessToken", accessToken); - tokenMap.put("refreshToken", refreshToken); + Map 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); + }); + } }