Skip to content
Merged
Show file tree
Hide file tree
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
Expand Up @@ -6,8 +6,8 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.log4u.common.constants.TokenConstants;
import com.example.log4u.common.oauth2.jwt.JwtUtil;
import com.example.log4u.common.oauth2.repository.RefreshTokenRepository;
import com.example.log4u.common.oauth2.service.RefreshTokenService;

import io.jsonwebtoken.ExpiredJwtException;
Expand All @@ -23,7 +23,6 @@ public class OAuth2Controller {

private final JwtUtil jwtUtil;
private final RefreshTokenService refreshTokenService;
private final RefreshTokenRepository refreshTokenRepository;

@GetMapping("/token/reissue")
public ResponseEntity<?> reissue(
Expand All @@ -35,10 +34,10 @@ public ResponseEntity<?> reissue(
String access = null;
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
if (cookie.getName().equals("refresh")) {
if (cookie.getName().equals(TokenConstants.REFRESH_TOKEN)) {
refresh = cookie.getValue();
}
if (cookie.getName().equals("access")) {
if (cookie.getName().equals(TokenConstants.ACCESS_TOKEN)) {
access = cookie.getValue();
}
}
Expand All @@ -57,7 +56,7 @@ public ResponseEntity<?> reissue(

// 토큰이 refresh인지 확인 (발급시 페이로드에 명시)
String category = jwtUtil.getTokenType(refresh);
if (!category.equals("refresh")) {
if (!category.equals(TokenConstants.REFRESH_TOKEN)) {
return new ResponseEntity<>("잘못된 토큰입니다.", HttpStatus.BAD_REQUEST);
}

Expand All @@ -67,21 +66,20 @@ public ResponseEntity<?> reissue(

private void createNewTokens(HttpServletResponse response, String access, String refresh) {
// 기존 리프레시 토큰 삭제
refreshTokenRepository.deleteByRefresh(refresh);
refreshTokenService.deleteRefreshToken(refresh);

Long userId = jwtUtil.getUserId(access);
String role = jwtUtil.getRole(access);
String name = jwtUtil.getName(access);

String newAccessToken = jwtUtil.createJwt("access", userId, name, role, 600000L);
String newRefreshToken = jwtUtil.createJwt("refresh", userId, name, role, 600000L);
String newAccessToken = jwtUtil.createJwt(TokenConstants.ACCESS_TOKEN, userId, name, role, 600000L);
String newRefreshToken = jwtUtil.createJwt(TokenConstants.REFRESH_TOKEN, userId, name, role, 600000L);

response.addCookie(createCookie("refresh", newRefreshToken));
response.addCookie(createCookie("access", newAccessToken));
response.addCookie(createCookie(TokenConstants.REFRESH_TOKEN, newRefreshToken));
response.addCookie(createCookie(TokenConstants.ACCESS_TOKEN, newAccessToken));

// 새 리프레시 토큰 저장
refreshTokenService.saveRefreshToken(
userId,
name,
refresh
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class RefreshToken {
@Setter
private String name;

@Column(nullable = false)
@Column(nullable = false, unique = true)
@Setter
private String refresh;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private void setCookieAndSaveRefreshToken(
String refresh = jwtUtil.createJwt(REFRESH_TOKEN_KEY, userId, name, role, refreshTokenValidityInSeconds);

// 리프레시 토큰 DB 저장
refreshTokenService.saveRefreshToken(null, name, refresh);
refreshTokenService.saveRefreshToken(name, refresh);

response.addCookie(createCookie(ACCESS_TOKEN_KEY, access));
response.addCookie(createCookie(REFRESH_TOKEN_KEY, refresh));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.example.log4u.common.oauth2.entity.RefreshToken;
import com.example.log4u.common.oauth2.repository.RefreshTokenRepository;

import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;

@Service
Expand All @@ -19,15 +20,20 @@ public class RefreshTokenService {
@Value("${jwt.refresh-token-expire-time-seconds}")
private long refreshTokenValidityInSeconds;

public void saveRefreshToken(Long userId, String name, String refresh) {
public void saveRefreshToken(String name, String refresh) {
Date date = new Date(System.currentTimeMillis() + refreshTokenValidityInSeconds);

RefreshToken refreshToken = new RefreshToken(
userId,
null,
name,
refresh,
date.toString()
);
refreshTokenRepository.save(refreshToken);
}

@Transactional
public void deleteRefreshToken(String refresh) {
refreshTokenRepository.deleteByRefresh(refresh);
}
}
6 changes: 6 additions & 0 deletions src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ spring:
hibernate:
ddl-auto: create


properties:
hibernate:
format_sql: true
show_sql: true

datasource:
url: jdbc:mysql://localhost:3307/log4u
username: dev
Expand Down