diff --git a/src/main/java/balancetalk/global/config/SecurityConfig.java b/src/main/java/balancetalk/global/config/SecurityConfig.java index e2588616a..38fde223e 100644 --- a/src/main/java/balancetalk/global/config/SecurityConfig.java +++ b/src/main/java/balancetalk/global/config/SecurityConfig.java @@ -39,7 +39,7 @@ public class SecurityConfig { private static final String[] PUBLIC_POST = { "/members/join", "/members/login", "/email/request", "/email/verify", - "/posts/{postId}/vote" + "/posts/{postId}/vote", "/files/image/upload" }; private static final String[] PUBLIC_PUT = { diff --git a/src/main/java/balancetalk/global/jwt/JwtTokenProvider.java b/src/main/java/balancetalk/global/jwt/JwtTokenProvider.java index d752ed603..857c9caae 100644 --- a/src/main/java/balancetalk/global/jwt/JwtTokenProvider.java +++ b/src/main/java/balancetalk/global/jwt/JwtTokenProvider.java @@ -39,9 +39,11 @@ public class JwtTokenProvider { /** * Access 토큰 생성 */ - public String createAccessToken(Authentication authentication) { + public String createAccessToken(Authentication authentication, Long memberId) { validateAuthentication(authentication); - Claims claims = Jwts.claims().setSubject(authentication.getName()); + Claims claims = Jwts.claims(); + claims.put("memberId", memberId); + claims.setSubject(authentication.getName()); Date now = new Date(); Date expireDate = new Date(now.getTime() + accessExpirationTime); @@ -56,9 +58,11 @@ public String createAccessToken(Authentication authentication) { /** * Refresh 토큰 생성 */ - public String createRefreshToken(Authentication authentication) { + public String createRefreshToken(Authentication authentication, Long memberId) { validateAuthentication(authentication); - Claims claims = Jwts.claims().setSubject(authentication.getName()); + Claims claims = Jwts.claims(); + claims.put("memberId", memberId); + claims.setSubject(authentication.getName()); Date now = new Date(); Date expireDate = new Date(now.getTime() + refreshExpirationTime); @@ -94,6 +98,12 @@ public String getPayload(String token) { return Jwts.parserBuilder().setSigningKey(secretKey).build().parseClaimsJws(token).getBody().getSubject(); } + public Long getMemberId(String token) { + validateToken(token); + Claims claims = Jwts.parserBuilder().setSigningKey(secretKey).build().parseClaimsJws(token).getBody(); + return claims.get("memberId", Long.class); + } + public boolean validateToken(String token) { try { Jws claims = Jwts.parserBuilder().setSigningKey(secretKey).build().parseClaimsJws(token); @@ -113,7 +123,7 @@ private void validateAuthentication(Authentication authentication) { } } - public TokenDto reissueToken(String refreshToken) { + public TokenDto reissueToken(String refreshToken, Long memberId) { validateToken(refreshToken); Authentication authentication = getAuthentication(refreshToken); // redis에 저장된 RefreshToken 값을 가져옴 @@ -122,8 +132,8 @@ public TokenDto reissueToken(String refreshToken) { throw new BalanceTalkException(ErrorCode.INVALID_REFRESH_TOKEN); } TokenDto tokenDto = new TokenDto( - createAccessToken(authentication), - createRefreshToken(authentication) + createAccessToken(authentication, memberId), + createRefreshToken(authentication, memberId) ); return tokenDto; } diff --git a/src/main/java/balancetalk/module/member/application/MemberService.java b/src/main/java/balancetalk/module/member/application/MemberService.java index 359505377..3d1918d2f 100644 --- a/src/main/java/balancetalk/module/member/application/MemberService.java +++ b/src/main/java/balancetalk/module/member/application/MemberService.java @@ -9,6 +9,7 @@ import balancetalk.module.member.domain.Member; import balancetalk.module.member.domain.MemberRepository; import balancetalk.module.member.dto.*; +import io.jsonwebtoken.ExpiredJwtException; import jakarta.servlet.http.HttpServletRequest; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -24,7 +25,7 @@ import java.util.List; import java.util.stream.Collectors; -import static balancetalk.global.exception.ErrorCode.NOT_FOUND_FILE; +import static balancetalk.global.exception.ErrorCode.*; @Slf4j @Service @@ -40,6 +41,12 @@ public class MemberService { @Transactional public Long join(final JoinRequest joinRequest) { + if (memberRepository.existsByEmail(joinRequest.getEmail())) { + throw new BalanceTalkException(ALREADY_REGISTERED_EMAIL); + } + if (memberRepository.existsByNickname(joinRequest.getNickname())) { + throw new BalanceTalkException(ALREADY_REGISTERED_NICKNAME); + } joinRequest.setPassword(passwordEncoder.encode(joinRequest.getPassword())); File profilePhoto = null; if (joinRequest.getProfilePhoto() != null && !joinRequest.getProfilePhoto().isEmpty()) { @@ -63,8 +70,8 @@ public TokenDto login(final LoginRequest loginRequest) { Authentication authentication = authenticationManager.authenticate( new UsernamePasswordAuthenticationToken(loginRequest.getEmail(), loginRequest.getPassword()) ); - String refreshToken = jwtTokenProvider.createRefreshToken(authentication); - return jwtTokenProvider.reissueToken(refreshToken); // 만료되었다면, 재발급 + String refreshToken = jwtTokenProvider.createRefreshToken(authentication, member.getId()); + return jwtTokenProvider.reissueToken(refreshToken, member.getId()); // 만료되었다면, 재발급 } @Transactional(readOnly = true) diff --git a/src/main/java/balancetalk/module/member/domain/MemberRepository.java b/src/main/java/balancetalk/module/member/domain/MemberRepository.java index 9e40086e7..1653f927e 100644 --- a/src/main/java/balancetalk/module/member/domain/MemberRepository.java +++ b/src/main/java/balancetalk/module/member/domain/MemberRepository.java @@ -9,6 +9,7 @@ public interface MemberRepository extends JpaRepository { Optional findByEmail(String username); boolean existsByNickname(String nickname); + boolean existsByEmail(String email); void deleteByEmail(String email); @Query("select m.id from Member m JOIN m.votes v WHERE v.balanceOption.id = :balanceOptionId") diff --git a/src/test/java/balancetalk/module/member/application/MemberServiceTest.java b/src/test/java/balancetalk/module/member/application/MemberServiceTest.java index a8be6b232..10f0b9d5c 100644 --- a/src/test/java/balancetalk/module/member/application/MemberServiceTest.java +++ b/src/test/java/balancetalk/module/member/application/MemberServiceTest.java @@ -130,7 +130,7 @@ void LoginMember_Success() { // given when(memberRepository.findByEmail(loginRequest.getEmail())).thenReturn(Optional.ofNullable(member)); when(passwordEncoder.matches(eq(loginRequest.getPassword()), eq(joinRequest.getPassword()))).thenReturn(true); - when(jwtTokenProvider.reissueToken(any())).thenReturn(new TokenDto(accessToken, refreshToken)); + when(jwtTokenProvider.reissueToken(any() , anyLong())).thenReturn(new TokenDto(accessToken, refreshToken)); // when TokenDto result = memberService.login(loginRequest); diff --git a/src/test/java/balancetalk/module/notice/presentation/NoticeControllerTest.java b/src/test/java/balancetalk/module/notice/presentation/NoticeControllerTest.java index 8d13d4005..3ad6681d3 100644 --- a/src/test/java/balancetalk/module/notice/presentation/NoticeControllerTest.java +++ b/src/test/java/balancetalk/module/notice/presentation/NoticeControllerTest.java @@ -20,6 +20,7 @@ // import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; // import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + @ExtendWith(SpringExtension.class) @SpringBootTest @AutoConfigureMockMvc