Skip to content
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

fix: #22 token 뿌리기 롤백 #30

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
}
}
Loading