Skip to content

Commit a4acc17

Browse files
committed
feat: 리프레시 토큰 RDB 저장 로직
1 parent c696e21 commit a4acc17

File tree

4 files changed

+76
-10
lines changed

4 files changed

+76
-10
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.example.log4u.common.oauth2.entity;
2+
3+
import jakarta.persistence.Column;
4+
import jakarta.persistence.Entity;
5+
import jakarta.persistence.GeneratedValue;
6+
import jakarta.persistence.GenerationType;
7+
import jakarta.persistence.Id;
8+
import lombok.AccessLevel;
9+
import lombok.AllArgsConstructor;
10+
import lombok.NoArgsConstructor;
11+
import lombok.Setter;
12+
13+
@Entity(name = "refresh_token")
14+
@NoArgsConstructor(access = AccessLevel.PROTECTED)
15+
@AllArgsConstructor
16+
public class RefreshToken {
17+
@Id
18+
@GeneratedValue(strategy = GenerationType.IDENTITY)
19+
private Long id;
20+
21+
@Column(nullable = false)
22+
@Setter
23+
private String name;
24+
25+
@Column(nullable = false)
26+
@Setter
27+
private String refresh;
28+
29+
@Column(nullable = false)
30+
@Setter
31+
private String expiration;
32+
}

src/main/java/com/example/log4u/common/oauth2/handler/OAuth2AuthenticationSuccessHandler.java

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.IOException;
44
import java.util.Collection;
5+
import java.util.Date;
56
import java.util.Iterator;
67
import java.util.Optional;
78

@@ -13,7 +14,9 @@
1314
import org.springframework.stereotype.Component;
1415

1516
import com.example.log4u.common.oauth2.dto.CustomOAuth2User;
17+
import com.example.log4u.common.oauth2.entity.RefreshToken;
1618
import com.example.log4u.common.oauth2.jwt.JwtUtil;
19+
import com.example.log4u.common.oauth2.repository.RefreshTokenRepository;
1720
import com.example.log4u.domain.user.entity.User;
1821
import com.example.log4u.domain.user.repository.UserRepository;
1922

@@ -27,9 +30,9 @@
2730
public class OAuth2AuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
2831

2932
private final UserRepository userRepository;
33+
private final RefreshTokenRepository refreshTokenRepository;
3034
private final JwtUtil jwtUtil;
3135

32-
3336
private static final String MAIN_PAGE = "http://localhost:3000/";
3437
private static final String PROFILE_CREATE_PAGE = "http://localhost:3000/profile";
3538
private static final String LOGIN_PAGE = "http://localhost:3000/login";
@@ -53,34 +56,41 @@ public void onAuthenticationSuccess(
5356
CustomOAuth2User customOAuth2User = (CustomOAuth2User)authentication.getPrincipal();
5457
Optional<User> existUser = userRepository.findByProviderId(customOAuth2User.getProviderId());
5558
Long userId = existUser.map(User::getUserId).orElse(null);
59+
String name = customOAuth2User.getName();
5660

57-
String redirectUrl = switch(customOAuth2User.getRole()){
61+
String redirectUrl = switch (customOAuth2User.getRole()) {
5862
case "ROLE_GUEST" -> PROFILE_CREATE_PAGE;
5963
case "ROLE_USER" -> MAIN_PAGE;
6064
default -> LOGIN_PAGE;
6165
};
6266

63-
redirectTo(response, userId, authentication, redirectUrl);
67+
setCookieAndSaveRefreshToken(response, userId, authentication, name);
68+
redirectTo(response, redirectUrl);
6469
}
6570

66-
private void redirectTo(
71+
private void setCookieAndSaveRefreshToken(
6772
HttpServletResponse response,
6873
Long userId,
6974
Authentication authentication,
70-
String redirectUrl
71-
) throws IOException {
72-
75+
String name
76+
) {
7377
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
7478
Iterator<? extends GrantedAuthority> iterator = authorities.iterator();
7579
GrantedAuthority auth = iterator.next();
7680
String role = auth.getAuthority();
7781

82+
// 쿠키 생성
7883
String access = jwtUtil.createJwt(ACCESS_TOKEN_KEY, userId, role, accessTokenValidityInSeconds);
79-
String refresh = jwtUtil.createJwt(REFRESH_TOKEN_KEY, userId, role, refreshTokenValidityInSeconds);
84+
String refresh = jwtUtil.createJwt(REFRESH_TOKEN_KEY, userId, role, refreshTokenValidityInSeconds);
85+
// 저장
86+
saveRefreshToken(refresh, name);
8087

8188
response.addCookie(createCookie(ACCESS_TOKEN_KEY, access));
8289
response.addCookie(createCookie(REFRESH_TOKEN_KEY, refresh));
8390
response.setStatus(HttpStatus.OK.value());
91+
}
92+
93+
public void redirectTo(HttpServletResponse response, String redirectUrl) throws IOException {
8494
response.sendRedirect(redirectUrl);
8595
}
8696

@@ -93,4 +103,15 @@ private Cookie createCookie(String key, String value) {
93103
return cookie;
94104
}
95105

106+
public void saveRefreshToken(String refresh, String name) {
107+
Date date = new Date(System.currentTimeMillis() + refreshTokenValidityInSeconds);
108+
109+
RefreshToken refreshToken = new RefreshToken(
110+
null,
111+
name,
112+
refresh,
113+
date.toString()
114+
);
115+
refreshTokenRepository.save(refreshToken);
116+
}
96117
}

src/main/java/com/example/log4u/common/oauth2/jwt/JwtLogoutFilter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private void doFilter(HttpServletRequest request, HttpServletResponse response,
7777
}
7878

7979
//DB에 저장되어 있는지 확인
80-
Boolean isExist = refreshTokenRepository.existsByRefreshToken(refresh);
80+
Boolean isExist = refreshTokenRepository.existsByRefresh(refresh);
8181
if (Boolean.FALSE.equals(isExist)) {
8282
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
8383
return;
@@ -89,7 +89,7 @@ private void doFilter(HttpServletRequest request, HttpServletResponse response,
8989

9090
public void logout(HttpServletResponse response, String refresh) {
9191
// DB 에서 리프레시 토큰 제거
92-
refreshTokenRepository.deleteByRefreshToken(refresh);
92+
refreshTokenRepository.deleteByRefresh(refresh);
9393
// 쿠키 제거
9494
deleteCookie(response);
9595
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example.log4u.common.oauth2.repository;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
import org.springframework.stereotype.Repository;
5+
6+
import com.example.log4u.common.oauth2.entity.RefreshToken;
7+
8+
@Repository
9+
public interface RefreshTokenRepository extends JpaRepository<RefreshToken, Long> {
10+
Boolean existsByRefresh(String refresh);
11+
12+
void deleteByRefresh(String refresh);
13+
}

0 commit comments

Comments
 (0)