Skip to content

Commit

Permalink
[FIX] 빌드 에러 해결 (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
arinming committed Jan 19, 2024
1 parent 983494d commit 57fb0e6
Show file tree
Hide file tree
Showing 16 changed files with 301 additions and 297 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies {
annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"

runtimeOnly 'com.mysql:mysql-connector-j'
implementation 'org.mariadb.jdbc:mariadb-java-client:2.6.2'

testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,74 +1,74 @@
package com.ttubeog.domain.auth.application;

import com.ttubeog.domain.member.domain.Provider;
import com.ttubeog.domain.member.domain.Role;
import com.ttubeog.domain.member.domain.Member;
import com.ttubeog.domain.member.domain.repository.MemberRepository;
import com.ttubeog.global.DefaultAssert;
import com.ttubeog.global.config.security.auth.OAuth2UserInfo;
import com.ttubeog.global.config.security.auth.OAuth2UserInfoFactory;
import com.ttubeog.global.config.security.token.MemberPrincipal;
import lombok.RequiredArgsConstructor;
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.user.OAuth2User;
import org.springframework.stereotype.Service;

import java.util.Optional;

@RequiredArgsConstructor
@Service
public class CustomDefaultOAuth2UserService extends DefaultOAuth2UserService{

private final MemberRepository memberRepository;

@Override
public OAuth2User loadUser(OAuth2UserRequest oAuth2UserRequest) throws OAuth2AuthenticationException {
OAuth2User oAuth2User = super.loadUser(oAuth2UserRequest);
try {
return processOAuth2User(oAuth2UserRequest, oAuth2User);
} catch (Exception e) {
DefaultAssert.isAuthentication(e.getMessage());
}
return null;
}

private OAuth2User processOAuth2User(OAuth2UserRequest oAuth2UserRequest, OAuth2User oAuth2User) {
OAuth2UserInfo oAuth2UserInfo = OAuth2UserInfoFactory.getOAuth2UserInfo(oAuth2UserRequest.getClientRegistration().getRegistrationId(), oAuth2User.getAttributes());
DefaultAssert.isAuthentication(!oAuth2UserInfo.getEmail().isEmpty());

Optional<Member> userOptional = memberRepository.findByEmail(oAuth2UserInfo.getEmail());
Member member;
if(userOptional.isPresent()) {
member = userOptional.get();
DefaultAssert.isAuthentication(member.getProvider().equals(Provider.valueOf(oAuth2UserRequest.getClientRegistration().getRegistrationId())));
member = updateExistingMember(member, oAuth2UserInfo);
} else {
member = registerNewMember(oAuth2UserRequest, oAuth2UserInfo);
}

return MemberPrincipal.create(member, oAuth2User.getAttributes());
}

private Member registerNewMember(OAuth2UserRequest oAuth2UserRequest, OAuth2UserInfo oAuth2UserInfo) {
Member member = Member.builder()
.provider(Provider.valueOf(oAuth2UserRequest.getClientRegistration().getRegistrationId()))
.providerId(oAuth2UserInfo.getId())
.name(oAuth2UserInfo.getName())
.email(oAuth2UserInfo.getEmail())
.imageUrl(oAuth2UserInfo.getImageUrl())
.role(Role.USER)
.build();
return memberRepository.save(member);
}

private Member updateExistingMember(Member member, OAuth2UserInfo oAuth2UserInfo) {

member.updateName(oAuth2UserInfo.getName());
member.updateImageUrl(oAuth2UserInfo.getImageUrl());

return memberRepository.save(member);
}
}
//package com.ttubeog.domain.auth.application;
//
//import com.ttubeog.domain.member.domain.Provider;
//import com.ttubeog.domain.member.domain.Role;
//import com.ttubeog.domain.member.domain.Member;
//import com.ttubeog.domain.member.domain.repository.MemberRepository;
//import com.ttubeog.global.DefaultAssert;
//import com.ttubeog.global.config.security.auth.OAuth2UserInfo;
//import com.ttubeog.global.config.security.token.UserPrincipal;
//import lombok.RequiredArgsConstructor;
//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.user.OAuth2User;
//import org.springframework.stereotype.Service;
//
//import java.util.Optional;
//
//@RequiredArgsConstructor
//@Service
//public class CustomDefaultOAuth2UserService extends DefaultOAuth2UserService {
//
// private final MemberRepository memberRepository;
//
// @Override
// public OAuth2User loadUser(OAuth2UserRequest oAuth2UserRequest) throws OAuth2AuthenticationException {
// OAuth2User oAuth2User = super.loadUser(oAuth2UserRequest);
// try {
// return processOAuth2User(oAuth2UserRequest, oAuth2User);
// } catch (Exception e) {
// DefaultAssert.isAuthentication(e.getMessage());
// }
// return null;
// }
//
// private OAuth2User processOAuth2User(OAuth2UserRequest oAuth2UserRequest, OAuth2User oAuth2User) {
// OAuth2UserInfo oAuth2UserInfo = OAuth2UserInfoFactory.getOAuth2UserInfo(oAuth2UserRequest.getClientRegistration().getRegistrationId(), oAuth2User.getAttributes());
// DefaultAssert.isAuthentication(!oAuth2UserInfo.getEmail().isEmpty());
//
// Optional<Member> userOptional = memberRepository.findByEmail(oAuth2UserInfo.getEmail());
// Member member;
// if (userOptional.isPresent()) {
// member = userOptional.get();
// DefaultAssert.isAuthentication(member.getProvider().equals(Provider.valueOf(oAuth2UserRequest.getClientRegistration().getRegistrationId())));
// member = updateExistingMember(member, oAuth2UserInfo);
// } else {
// member = registerNewMember(oAuth2UserRequest, oAuth2UserInfo);
// }
//
// return UserPrincipal.create(member, oAuth2User.getAttributes());
// }
//
// private Member registerNewMember(OAuth2UserRequest oAuth2UserRequest, OAuth2UserInfo oAuth2UserInfo) {
// Member member = Member.builder()
// .provider(Provider.valueOf(oAuth2UserRequest.getClientRegistration().getRegistrationId()))
// .providerId(oAuth2UserInfo.getId())
// .name(oAuth2UserInfo.getName())
// .email(oAuth2UserInfo.getEmail())
// .imageUrl(oAuth2UserInfo.getImageUrl())
// .refreshToken(oAuth2UserInfo.getRefreshToken())
// .role(Role.USER)
// .build();
//
// return memberRepository.save(member);
// }
//
// private Member updateExistingMember(Member member, OAuth2UserInfo oAuth2UserInfo) {
//
// member.updateName(oAuth2UserInfo.getName());
// member.updateImageUrl(oAuth2UserInfo.getImageUrl());
//
// return memberRepository.save(member);
// }
//}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.ttubeog.domain.member.domain.Member;
import com.ttubeog.domain.member.domain.repository.MemberRepository;
import com.ttubeog.global.DefaultAssert;
import com.ttubeog.global.config.security.token.MemberPrincipal;
import com.ttubeog.global.config.security.token.UserPrincipal;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
Expand All @@ -27,15 +27,15 @@ public UserDetails loadUserByUsername(String email) throws UsernameNotFoundExcep
new UsernameNotFoundException("멤버 정보를 찾을 수 없습니다.")
);

return MemberPrincipal.create(member);
return UserPrincipal.create(member);
}

@Transactional
public UserDetails loadUserById(Long id) {
Optional<Member> user = memberRepository.findById(id);
DefaultAssert.isOptionalPresent(user);

return MemberPrincipal.create(user.get());
return UserPrincipal.create(user.get());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.ttubeog.domain.auth.dto.TokenMapping;
import com.ttubeog.global.config.security.OAuth2Config;
import com.ttubeog.global.config.security.token.MemberPrincipal;
import com.ttubeog.global.config.security.token.UserPrincipal;
import io.jsonwebtoken.*;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.security.Keys;
Expand All @@ -27,7 +27,7 @@ public class CustomTokenProviderService {
private CustomMemberDetailsService customMemberDetailsService;

public TokenMapping refreshToken(Authentication authentication, String refreshToken) {
MemberPrincipal memberPrincipal = (MemberPrincipal) authentication.getPrincipal();
UserPrincipal userPrincipal = (UserPrincipal) authentication.getPrincipal();
Date now = new Date();

Date accessTokenExpiresIn = new Date(now.getTime() + oAuth2Config.getAuth().getAccessTokenExpirationMsec());
Expand All @@ -37,21 +37,21 @@ public TokenMapping refreshToken(Authentication authentication, String refreshTo
Key key = Keys.hmacShaKeyFor(keyBytes);

String accessToken = Jwts.builder()
.setSubject(Long.toString(memberPrincipal.getId()))
.setSubject(Long.toString(userPrincipal.getId()))
.setIssuedAt(new Date())
.setExpiration(accessTokenExpiresIn)
.signWith(key, SignatureAlgorithm.HS512)
.compact();

return TokenMapping.builder()
.userEmail(memberPrincipal.getEmail())
.userEmail(userPrincipal.getEmail())
.accessToken(accessToken)
.refreshToken(refreshToken)
.build();
}

public TokenMapping createToken(Authentication authentication) {
MemberPrincipal memberPrincipal = (MemberPrincipal) authentication.getPrincipal();
UserPrincipal userPrincipal = (UserPrincipal) authentication.getPrincipal();

Date now = new Date();

Expand All @@ -64,7 +64,7 @@ public TokenMapping createToken(Authentication authentication) {
Key key = Keys.hmacShaKeyFor(keyBytes);

String accessToken = Jwts.builder()
.setSubject(Long.toString(memberPrincipal.getId()))
.setSubject(Long.toString(userPrincipal.getId()))
.setIssuedAt(new Date())
.setExpiration(accessTokenExpiresIn)
.signWith(key, SignatureAlgorithm.HS512)
Expand All @@ -76,7 +76,7 @@ public TokenMapping createToken(Authentication authentication) {
.compact();

return TokenMapping.builder()
.userEmail(memberPrincipal.getEmail())
.userEmail(userPrincipal.getEmail())
.accessToken(accessToken)
.refreshToken(refreshToken)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@

@RequiredArgsConstructor
@Configuration
public class SecurityConfig {
public class SecurityDomainConfig {
private final JwtTokenService jwtTokenService;
private final MemberService memberService;

@Bean
@Bean(name = "domainAuth")
public AuthenticationManager authenticationManager(
final AuthenticationConfiguration authenticationConfiguration
) throws Exception {
Expand Down
7 changes: 0 additions & 7 deletions src/main/java/com/ttubeog/domain/auth/filter/JwtFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,16 @@

import com.ttubeog.domain.auth.application.JwtTokenService;
import com.ttubeog.domain.member.application.MemberService;
import com.ttubeog.domain.member.domain.Member;
import com.ttubeog.domain.member.dto.MemberDto;
import com.ttubeog.domain.member.dto.response.MemberDetailRes;
import com.ttubeog.global.config.security.token.MemberPrincipal;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.GenericFilterBean;

import java.io.IOException;
import java.nio.file.attribute.UserPrincipal;

@RequiredArgsConstructor
public class JwtFilter extends GenericFilterBean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.ttubeog.domain.member.domain.Member;
import com.ttubeog.domain.member.domain.repository.MemberRepository;
import com.ttubeog.global.DefaultAssert;
import com.ttubeog.global.config.security.token.MemberPrincipal;
import com.ttubeog.global.config.security.token.UserPrincipal;
import com.ttubeog.global.payload.ApiResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
Expand All @@ -21,8 +21,8 @@ public class MemberService {
private final MemberRepository memberRepository;

// 현재 유저 조회
public ResponseEntity<?> getCurrentUser(MemberPrincipal memberPrincipal){
Optional<Member> checkUser = memberRepository.findById(memberPrincipal.getId());
public ResponseEntity<?> getCurrentUser(UserPrincipal userPrincipal){
Optional<Member> checkUser = memberRepository.findById(userPrincipal.getId());
DefaultAssert.isOptionalPresent(checkUser);
Member member = checkUser.get();

Expand Down
13 changes: 9 additions & 4 deletions src/main/java/com/ttubeog/domain/member/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
@Getter
public class Member extends BaseEntity {

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String name;
Expand All @@ -34,9 +35,10 @@ public class Member extends BaseEntity {
private String providerId;

private String platform;
private String refreshToken;

@Builder
public Member(Long id, String name, String email, String imageUrl, Boolean emailVerified, String password, Provider provider, Role role, String providerId, String platform) {
public Member(Long id, String name, String email, String imageUrl, String password, Provider provider, Role role, String providerId, String platform, String refreshToken) {
this.id = id;
this.name = name;
this.email = email;
Expand All @@ -46,13 +48,16 @@ public Member(Long id, String name, String email, String imageUrl, Boolean email
this.role = role;
this.providerId = providerId;
this.platform = platform;
this.refreshToken = refreshToken;
}

public void updateName(String name){


public void updateName(String name) {
this.name = name;
}

public void updateImageUrl(String imageUrl){
public void updateImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/ttubeog/domain/member/domain/Provider.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.ttubeog.domain.member.domain;

public enum Provider {
kakao,
apple
KAKAO,
APPLE
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.ttubeog.domain.member.application.MemberService;
import com.ttubeog.domain.member.dto.response.MemberDetailRes;
import com.ttubeog.global.config.security.token.CurrentMember;
import com.ttubeog.global.config.security.token.MemberPrincipal;
import com.ttubeog.global.config.security.token.CurrentUser;
import com.ttubeog.global.config.security.token.UserPrincipal;
import com.ttubeog.global.payload.ErrorResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
Expand Down Expand Up @@ -33,9 +33,9 @@ public class MemberController {
})
@GetMapping
public ResponseEntity<?> getCurrentMember(
@Parameter(description = "Accesstoken을 입력해주세요.", required = true) @CurrentMember MemberPrincipal memberPrincipal
@Parameter(description = "Accesstoken을 입력해주세요.", required = true) @CurrentUser UserPrincipal userPrincipal
) {
return memberService.getCurrentUser(memberPrincipal);
return memberService.getCurrentUser(userPrincipal);
}

}
Loading

0 comments on commit 57fb0e6

Please sign in to comment.