This commit is contained in:
mindol1004
2024-10-22 17:12:28 +09:00
parent d63b268765
commit df373d5d27
6 changed files with 19 additions and 24 deletions

View File

@@ -47,8 +47,4 @@ public class AgentUserToken extends AuditEntity {
this.refreshToken = refreshToken;
}
public boolean validateRefreshToken(String refreshToken) {
return this.refreshToken.equals(refreshToken);
}
}

View File

@@ -13,6 +13,7 @@ import com.spring.domain.user.dto.UserManagementResponse;
import com.spring.domain.user.entity.AgentUser;
import com.spring.domain.user.error.UserNotFoundException;
import com.spring.domain.user.repository.AgentUserRepository;
import com.spring.domain.user.repository.AgentUserTokenRepository;
import lombok.RequiredArgsConstructor;
@@ -21,6 +22,7 @@ import lombok.RequiredArgsConstructor;
public class UserManagementService {
private final AgentUserRepository agentUserRepository;
private final AgentUserTokenRepository agentUserTokenRepository;
@Transactional(readOnly = true)
public List<UserManagementResponse> getUsers(UserFindRequest request) {
@@ -42,8 +44,10 @@ public class UserManagementService {
@Transactional
public void deleteUser(String id) {
AgentUser user = agentUserRepository.findById(UUID.fromString(id))
.orElseThrow(UserNotFoundException::new);
AgentUser user = agentUserRepository.findById(UUID.fromString(id)).orElseThrow(UserNotFoundException::new);
if (agentUserTokenRepository.findById(UUID.fromString(id)).isPresent()) {
agentUserTokenRepository.deleteById(UUID.fromString(id));
}
agentUserRepository.delete(user);
}

View File

@@ -50,7 +50,9 @@ public class UserRefreshTokenService implements RefreshTokenService {
@Transactional
@Override
public void deleteRefreshToken(String key) {
agentUserTokenRepository.deleteById(UUID.fromString(key));
if (agentUserTokenRepository.findById(UUID.fromString(key)).isPresent()) {
agentUserTokenRepository.deleteById(UUID.fromString(key));
}
}
}

View File

@@ -78,7 +78,7 @@ public final class JwtAuthenticationFilter extends OncePerRequestFilter {
} else {
// 액세스 토큰이 유효하지 않은 경우 리프레시 토큰을 사용하여 새로운 액세스 토큰을 발급합니다.
String refreshToken = jwtTokenService.resolveTokenFromCookie(request, JwtTokenRule.REFRESH_PREFIX);
jwtTokenService.validateToken(refreshToken);
jwtTokenService.validateRefreshToken(refreshToken);
String reissuedAccessToken = jwtTokenService.getRefreshToken(refreshToken);
Authentication authentication = jwtTokenService.getAuthentication(reissuedAccessToken);
jwtTokenService.saveRefreshToken(authentication.getName(), jwtTokenService.generateRefreshToken(response, authentication));
@@ -117,7 +117,7 @@ public final class JwtAuthenticationFilter extends OncePerRequestFilter {
return Optional.ofNullable(request.getHeader(headerName))
.filter(token -> token.substring(0, 7).equalsIgnoreCase(JwtTokenRule.BEARER_PREFIX.getValue()))
.map(token -> token.substring(7))
.orElse(jwtTokenService.resolveTokenFromCookie(request, JwtTokenRule.ACCESS_PREFIX));
.orElseGet(() -> jwtTokenService.resolveTokenFromCookie(request, JwtTokenRule.ACCESS_PREFIX));
}
/**

View File

@@ -161,25 +161,16 @@ public class JwtTokenService {
* @param maxAgeSeconds 쿠키 유효 시간(초)
* @return 생성된 ResponseCookie 객체
*/
private ResponseCookie setTokenToCookie(String tokenPrefix, String token, long maxAgeSeconds) {
private ResponseCookie setTokenToCookie(String tokenPrefix, String token, long maxAgeMinutes) {
return ResponseCookie.from(tokenPrefix, token)
.path("/")
.maxAge(Duration.ofSeconds(maxAgeSeconds))
.maxAge(Duration.ofMinutes(maxAgeMinutes))
.httpOnly(true)
.sameSite("None")
.secure(true)
.build();
}
/**
* 액세스 토큰의 유효성을 검증합니다.
*
* @param token 검증할 토큰
*/
public void validateToken(String token) {
jwtTokenUtil.tokenStatus(token, accessSecretKey);
}
/**
* 액세스 토큰의 유효성을 검증합니다.
*
@@ -196,8 +187,8 @@ public class JwtTokenService {
* @param token 검증할 토큰
* @return 토큰의 유효성 여부
*/
public boolean validateRefreshToken(String token) {
return jwtTokenUtil.getTokenStatus(token, refreshSecretKey) == JwtTokenStatus.AUTHENTICATED;
public void validateRefreshToken(String token) {
jwtTokenUtil.tokenStatus(token, refreshSecretKey);
}
/**

View File

@@ -31,19 +31,21 @@ const updateTable = (users) => {
<td class="align-middle">${user.userName}</td>
<td class="align-middle">${user.email}</td>
<td class="align-middle">
<select id="userRole-${user.id}" class="form-select form-select-sm">
<select id="userRole-${user.id}" class="form-select form-select-sm" ${USER_INFO.userId === user.userId ? 'disabled' : ''}>
${ROLES.length > 0 ? ROLES.map(role => `
<option value="${role}" ${user.userRole === role ? 'selected' : ''}>${role}</option>
`).join('') : '<option value=""></option>'}
</select>
</td>
<td class="align-middle">
<input id="approved-${user.id}" type="checkbox" ${user.approved ? 'checked' : ''} class="form-check-input">
<input id="approved-${user.id}" type="checkbox" ${user.approved ? 'checked' : ''} class="form-check-input" ${USER_INFO.userId === user.userId ? 'disabled' : ''}>
</td>
<td class="align-middle">
${USER_INFO.userId !== user.userId ? `
<button class="btn btn-sm btn-outline-danger delete-btn" data-id="${user.id}" title="사용자 삭제">
<i class="bi bi-trash"></i>
</button>
` : ''}
</td>
</tr>
`).join('');