Feat: 로그아웃 핸들러 생성

This commit is contained in:
김용수 2024-09-24 12:05:56 +09:00
parent 2548b502d4
commit 0ac4c743ed
4 changed files with 60 additions and 8 deletions

View File

@ -12,6 +12,7 @@ import com.worlabel.global.config.swagger.SwaggerApiError;
import com.worlabel.global.config.swagger.SwaggerApiSuccess;
import com.worlabel.global.exception.CustomException;
import com.worlabel.global.exception.ErrorCode;
import com.worlabel.global.service.FcmService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.Cookie;
@ -65,7 +66,7 @@ public class AuthController {
@SwaggerApiSuccess(description = "Return Member Info")
@SwaggerApiError({ErrorCode.INVALID_TOKEN, ErrorCode.INVALID_REFRESH_TOKEN, ErrorCode.USER_NOT_FOUND})
@GetMapping("/profile")
public MemberResponse getMemberInfo(@CurrentUser Integer currentMember){
public MemberResponse getMemberInfo(@CurrentUser final Integer currentMember){
return memberService.getMemberId(currentMember);
}
@ -73,8 +74,8 @@ public class AuthController {
@SwaggerApiSuccess(description = "Redis에 FCM 토큰이 저장됨")
@SwaggerApiError({ErrorCode.INVALID_TOKEN, ErrorCode.INVALID_REFRESH_TOKEN, ErrorCode.USER_NOT_FOUND})
@PostMapping("/fcm")
public void saveFcmToken(@CurrentUser Integer currentMember, @RequestBody final FcmTokenRequest tokenRequest){
public void saveFcmToken(@CurrentUser final Integer currentMember, @RequestBody final FcmTokenRequest tokenRequest){
authService.saveFcmToken(currentMember, tokenRequest.getToken());
}
private static String parseRefreshCookie(HttpServletRequest request) {

View File

@ -0,0 +1,38 @@
package com.worlabel.domain.auth.handler;
import com.worlabel.domain.auth.service.AuthService;
import com.worlabel.domain.auth.service.JwtTokenService;
import com.worlabel.global.exception.CustomException;
import com.worlabel.global.exception.ErrorCode;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.stereotype.Component;
@Slf4j
@Component
@RequiredArgsConstructor
public class CustomLogoutHandler implements LogoutHandler {
private AuthService authService;
private JwtTokenService jwtTokenService;
@Override
public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
String token = request.getHeader("Authorization");
try {
String refreshedToken = token.substring(7);
int memberId = jwtTokenService.parseId(refreshedToken);
authService.deleteRefreshToken(memberId);
authService.deleteFcmToken(memberId);
log.debug("로그아웃된 사용자의 토큰이 삭제 됨 {}", memberId);
} catch (Exception e) {
throw new CustomException(ErrorCode.INVALID_REFRESH_TOKEN);
}
}
}

View File

@ -2,6 +2,7 @@ package com.worlabel.domain.auth.service;
import com.worlabel.domain.auth.entity.dto.JwtToken;
import com.worlabel.domain.auth.repository.AuthCacheRepository;
import com.worlabel.domain.auth.repository.FcmRepository;
import com.worlabel.global.exception.CustomException;
import com.worlabel.global.exception.ErrorCode;
import lombok.RequiredArgsConstructor;
@ -15,9 +16,10 @@ import java.util.Objects;
@Service
@RequiredArgsConstructor
public class AuthService {
private final JwtTokenService jwtTokenService;
private final AuthCacheRepository authCacheRepository;
private final JwtTokenService jwtTokenService;
private final FcmRepository fcmRepository;;
/**
* JWT 토큰 재발급
@ -34,7 +36,19 @@ public class AuthService {
/**
* 레디에 리프레시 토큰 저장
*/
public void saveRefreshToken(int id, String refreshToken,Long expiredTime) {
authCacheRepository.save(id, refreshToken, expiredTime);
public void saveRefreshToken(int memberId, String refreshToken,Long expiredTime) {
authCacheRepository.save(memberId, refreshToken, expiredTime);
}
public void deleteRefreshToken(int memberId) {
authCacheRepository.delete(memberId);
}
public void saveFcmToken(int memberId, String fcmToken) {
fcmRepository.save(memberId, fcmToken);
}
public void deleteFcmToken(int memberId) {
fcmRepository.delete(memberId);
}
}

View File

@ -82,7 +82,6 @@ public class SecurityConfig {
.successHandler(oAuth2SuccessHandler)
);
// JWT 필터 추가
http
.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);