Skip to content

Commit

Permalink
Merge pull request #46 from Kernel360/44-feat-refresh-token-and-excep…
Browse files Browse the repository at this point in the history
…tion-refactoring

feat: exception 로직 구현, JsessionId 삭제
  • Loading branch information
I-migi authored Sep 24, 2024
2 parents acecb71 + 4a4101f commit 46a15d2
Show file tree
Hide file tree
Showing 23 changed files with 280 additions and 161 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ public enum ErrorCode {

// 404 Errors
NOT_FOUND(404, "요청한 리소스를 찾을 수 없습니다."),
JWT_COOKIE_NOT_FOUND(404, "JWT 쿠키를 찾을 수 없습니다."),

// RESOURCE_NOT_EXIST
RESOURCE_NOT_EXIST(404, "특정 리소스를 찾을 수 없습니다."),
MEMBER_NOT_EXIST(404, "해당하는 회원이 존재하지 않습니다."),
CLUB_NOT_EXIST(404, "해당하는 동호회가 존재하지 않습니다."),

// 409 Errors
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.badminton.api.common.exception.member;

import org.badminton.api.common.error.ErrorCode;
import org.badminton.api.common.exception.BadmintonException;

public class MemberNotExistException extends BadmintonException {

public MemberNotExistException(ErrorCode errorCode, Long providerId, Exception e) {
super(errorCode, "providerId", String.valueOf(providerId), e);
}

public MemberNotExistException(ErrorCode errorCode, String providerId) {
super(errorCode, "providerId", String.valueOf(providerId));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.badminton.api.common.exception.oauth;

import org.badminton.api.common.error.ErrorCode;
import org.badminton.api.common.exception.BadmintonException;

public class JwtCookieNotFoundException extends BadmintonException {
public JwtCookieNotFoundException(ErrorCode errorCode) {
super(errorCode);
}

public JwtCookieNotFoundException(ErrorCode errorCode, Exception e) {
super(errorCode, e);
}
}

Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.badminton.api.member;

import org.badminton.api.member.model.dto.MemberDeleteResponse;
import org.badminton.api.member.model.dto.MemberLogoutResponse;
import org.badminton.api.member.model.dto.MemberUpdateRequest;
import org.badminton.api.member.model.dto.MemberUpdateResponse;
import org.badminton.api.member.service.MemberService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PatchMapping;
Expand Down Expand Up @@ -31,17 +33,10 @@ public class MemberController {
tags = {"Member"}
)
@PatchMapping
public ResponseEntity<String> update(HttpServletRequest request,
public ResponseEntity<MemberUpdateResponse> update(HttpServletRequest request,
@RequestBody MemberUpdateRequest updateRequest) {
try {
memberService.updateMember(request, updateRequest);
return new ResponseEntity<>("update successful", HttpStatus.OK);
} catch (IllegalArgumentException e) {
return new ResponseEntity<>("update failed", HttpStatus.BAD_REQUEST);
} catch (Exception e) {
return new ResponseEntity<>("update failed", HttpStatus.INTERNAL_SERVER_ERROR);
}

MemberUpdateResponse memberUpdateResponse = memberService.updateMember(request, updateRequest);
return ResponseEntity.ok(memberUpdateResponse);
}

@Operation(
Expand All @@ -50,15 +45,9 @@ public ResponseEntity<String> update(HttpServletRequest request,
tags = {"Member"}
)
@DeleteMapping
public ResponseEntity<String> delete(HttpServletRequest request, HttpServletResponse response) {
try {
memberService.deleteMember(request, response);
return new ResponseEntity<>("delete successful", HttpStatus.OK);
} catch (IllegalArgumentException e) {
return new ResponseEntity<>("delete failed", HttpStatus.BAD_REQUEST);
} catch (Exception e) {
return new ResponseEntity<>("delete failed", HttpStatus.INTERNAL_SERVER_ERROR);
}
public ResponseEntity<MemberDeleteResponse> delete(HttpServletRequest request, HttpServletResponse response) {
MemberDeleteResponse memberDeleteResponse = memberService.deleteMember(request, response);
return ResponseEntity.ok(memberDeleteResponse);
}

@Operation(
Expand All @@ -67,16 +56,9 @@ public ResponseEntity<String> delete(HttpServletRequest request, HttpServletResp
tags = {"Member"}
)
@PostMapping("/logout")
public ResponseEntity<String> logout(HttpServletRequest request, HttpServletResponse response) {
try {
memberService.logoutMember(request, response);
return ResponseEntity.ok("로그아웃 성공 , OAuth 연결끊기 성공!");
} catch (IllegalArgumentException e) {
return new ResponseEntity<>("logout failed", HttpStatus.BAD_REQUEST);
} catch (Exception e) {
return new ResponseEntity<>("logout failed", HttpStatus.INTERNAL_SERVER_ERROR);
}

public ResponseEntity<MemberLogoutResponse> logout(HttpServletRequest request, HttpServletResponse response) {
MemberLogoutResponse memberLogoutResponse = memberService.logoutMember(request, response);
return ResponseEntity.ok(memberLogoutResponse);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import java.io.IOException;

import org.badminton.api.member.model.dto.CustomOAuth2Member;
import org.badminton.api.member.model.dto.MemberResponse;
import org.badminton.api.member.oauth2.dto.CustomOAuth2Member;
import org.badminton.domain.member.entity.MemberAuthorization;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
Expand All @@ -25,14 +25,15 @@ public class JwtFilter extends OncePerRequestFilter {
private final JwtUtil jwtUtil;

@Override
protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException {
protected boolean shouldNotFilter(HttpServletRequest request) {
String path = request.getRequestURI();
return path.equals("/") || path.equals("/groups") || path.startsWith("/oauth2") || path.startsWith("/login")
|| path.startsWith("/api") || path.startsWith("/swagger-ui") || path.startsWith("/v3/api-docs")
|| path.startsWith("/v1");

}

// 일반적인 예외 -> 커스텀 예외 처리 X
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
Expand Down Expand Up @@ -66,7 +67,8 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
String accessToken = jwtUtil.getAccessToken(jwtToken);
String registrationId = jwtUtil.getRegistrationId(jwtToken);

MemberResponse memberResponse = new MemberResponse(MemberAuthorization.AUTHORIZATION_USER.name(), name, email,
MemberResponse memberResponse = new MemberResponse(MemberAuthorization.AUTHORIZATION_USER.name(), name,
email,
providerId, profileImage);
log.info("memberDto: {}", memberResponse);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.badminton.api.common.error.ErrorCode;
import org.badminton.api.common.exception.oauth.JwtCookieNotFoundException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

Expand All @@ -18,8 +20,6 @@
@Slf4j
public class JwtUtil {

//TODO: refresh 토큰 추가

private final SecretKey secretKey;

public JwtUtil(@Value("${spring.jwt.secret}") String secret) {
Expand All @@ -42,9 +42,9 @@ public String extractJwtTokenFromRequest(HttpServletRequest request) {
}
}
}
throw new IllegalArgumentException("JWT 쿠키가 없습니다");
throw new JwtCookieNotFoundException(ErrorCode.JWT_COOKIE_NOT_FOUND);
}

public String getProviderId(String token) {
return getDetail(token, "providerId");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.badminton.api.member.model.dto;

import org.badminton.domain.member.entity.MemberEntity;

import io.swagger.v3.oas.annotations.media.Schema;

@Schema(description = "회원 삭제 responseDto")
public record MemberDeleteResponse(
@Schema(description = "멤버 id", example = "1")
Long memberId,
@Schema(description = "삭제 여부", example = "true")
boolean isDeleted
) {

public static MemberDeleteResponse memberEntityToDeleteResponse(MemberEntity memberEntity) {
return new MemberDeleteResponse(memberEntity.getMemberId(), memberEntity.isMemberDeleted());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.badminton.api.member.model.dto;

import org.badminton.domain.member.entity.MemberEntity;

import io.swagger.v3.oas.annotations.media.Schema;

@Schema(description = "회원 로그아웃 responseDto")
public record MemberLogoutResponse(
@Schema(description = "회원 역할", example = "AUTHORIZATION_USER")
String authorization,

@Schema(description = "회원 이름", example = "이선우")
String name,

@Schema(description = "oAuth 로그인 이메일", example = "qosle@naver.com")
String email,

@Schema(description = "oAuth 제공 ID", example = "1070449979547641023123")
String providerId,

@Schema(description = "oAuth 제공 이미지", example = "1070449979547641023123")
String profileImage
) {
public static MemberLogoutResponse memberEntityToLogoutResponse(MemberEntity memberEntity) {
return new MemberLogoutResponse(memberEntity.getAuthorization(), memberEntity.getName(),
memberEntity.getEmail(),
memberEntity.getProviderId(), memberEntity.getProfileImage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@ public record MemberRequest(
String profileImage
) {

public static MemberEntity memberRequestToEntity(MemberRequest memberRequest) {
return new MemberEntity(memberRequest.email(), memberRequest.name(), memberRequest.providerId(),
memberRequest.profileImage, memberRequest.authorization());
// public static MemberEntity memberRequestToEntity(MemberRequest memberRequest) {
// return new MemberEntity(memberRequest.email(), memberRequest.name(), memberRequest.providerId(),
// memberRequest.profileImage, memberRequest.authorization());
// }

public MemberEntity memberRequestToEntity() {
return new MemberEntity(this.email(), this.name(), this.providerId(),
this.profileImage, this.authorization());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package org.badminton.api.member.model.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;

@Schema(description = "회원 수정 DTO")
@Getter
public class MemberUpdateRequest {

public record MemberUpdateRequest(
@Schema(description = "프로필 사진", example = "http://img1.kakaocdn.net/thumb/R640x640.q70/?fname=http://t1.kakaocdn.net/account_images/default_profile.jpeg")
String profileImage;
String profileImage
) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.badminton.api.member.model.dto;

import org.badminton.domain.member.entity.MemberEntity;

import io.swagger.v3.oas.annotations.media.Schema;

@Schema(description = "회원 수정 responseDto")
public record MemberUpdateResponse(
@Schema(description = "회원 역할", example = "AUTHORIZATION_USER")
String authorization,

@Schema(description = "회원 이름", example = "이선우")
String name,

@Schema(description = "oAuth 로그인 이메일", example = "qosle@naver.com")
String email,

@Schema(description = "oAuth 제공 ID", example = "1070449979547641023123")
String providerId,

@Schema(description = "oAuth 제공 이미지", example = "1070449979547641023123")
String profileImage
) {
public static MemberUpdateResponse memberEntityToUpdateResponse(MemberEntity memberEntity) {
return new MemberUpdateResponse(memberEntity.getAuthorization(), memberEntity.getName(),
memberEntity.getEmail(),
memberEntity.getProviderId(), memberEntity.getProfileImage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
import java.util.Iterator;

import org.badminton.api.member.jwt.JwtUtil;
import org.badminton.api.member.model.dto.CustomOAuth2Member;
import org.badminton.api.member.oauth2.dto.CustomOAuth2Member;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
Expand All @@ -27,7 +26,7 @@ public class CustomSuccessHandler extends SimpleUrlAuthenticationSuccessHandler

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {
Authentication authentication) throws IOException {

log.info("CustomSuccessHandler onAuthenticationSuccess");

Expand All @@ -54,9 +53,12 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
String token = jwtUtil.createJwt(providerId, authorization, name, email, profileImage,
accessToken, registrationId, 24 * 60 * 60 * 1000L); // 초 * 분 * 시

// response.setHeader("Authorization", "Bearer " + token);
request.getSession().invalidate();

response.setHeader("Set-Cookie", "JSESSIONID=; HttpOnly; Path=/; Max-Age=0; Secure; SameS ite=None;");

response.addCookie(createCookie(token));

response.sendRedirect("http://localhost:3000/");
}

Expand All @@ -73,3 +75,4 @@ private Cookie createCookie(String value) {

}


Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package org.badminton.api.member.model.dto;
package org.badminton.api.member.oauth2.dto;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

import org.badminton.api.member.model.dto.MemberResponse;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.core.user.OAuth2User;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.badminton.api.member.model.dto;
package org.badminton.api.member.oauth2.dto;

import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.badminton.api.member.model.dto;
package org.badminton.api.member.oauth2.dto;

import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.badminton.api.member.model.dto;
package org.badminton.api.member.oauth2.dto;

import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.badminton.api.member.model.dto;
package org.badminton.api.member.oauth2.dto;

public interface OAuthResponse {

Expand Down
Loading

0 comments on commit 46a15d2

Please sign in to comment.