-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] JWT Handler 응답 및 API 권한 설정 #134
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,8 @@ public enum ErrorCode { | |
ALREADY_LOGOUT_MEMBER("A3", "이미 로그아웃한 회원입니다"), | ||
EMAIL_SEND_ERROR("A4", "이메일 인증 코드 전송을 실패했습니다."), | ||
UNKNOWN_PROVIDER("A4", "provider 값이 KAKAO 또는 NAVER가 아닙니다."), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 인증 코드 중복된 제 부분 메모해 두었어요. 수정하겠습니다! |
||
NOT_ALLOWED_MEMBER("A6", "해당 요청에 대한 권한이 없습니다."), | ||
NOT_AUTHENTICATED_REQUEST("A7", "유효한 JWT 토큰이 없습니다."), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 역시 센스가 .. A5를 비워두셨네요👍 |
||
|
||
|
||
VOLUNTEER_NOT_FOUND("M1", "해당 이동봉사자를 찾을 수 없습니다."), // Member -> M (이동봉사자, 이동봉사 중개 통일) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
package com.pawwithu.connectdog.jwt; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.pawwithu.connectdog.error.dto.ErrorResponse; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
@@ -9,6 +11,8 @@ | |
|
||
import java.io.IOException; | ||
|
||
import static com.pawwithu.connectdog.error.ErrorCode.NOT_ALLOWED_MEMBER; | ||
|
||
/** | ||
* 필요한 권한이 존재하지 않는 경우에 403 Forbidden 에러를 리턴 | ||
* -> Spring Security가 AccessDeniedHandler 인터페이스의 handle 메서드 호출 | ||
|
@@ -17,10 +21,22 @@ | |
@Component | ||
public class JwtAccessDeniedHandler implements AccessDeniedHandler { | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
public JwtAccessDeniedHandler(ObjectMapper objectMapper) { | ||
this.objectMapper = objectMapper; | ||
} | ||
|
||
@Override | ||
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException { | ||
// 필요한 권한이 없이 접근하려 할때 403 | ||
log.info("허가 받지 않은 사용자의 접근입니다."); | ||
response.sendError(HttpServletResponse.SC_FORBIDDEN); | ||
ErrorResponse errorResponse = ErrorResponse.of(NOT_ALLOWED_MEMBER.getCode(), NOT_ALLOWED_MEMBER.getMessage()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이런 식으로 허가 받지 않은 사용자의 접근 응답 설정을 하는군요! 이렇게 하면 우리가 설정한 기존 에러 방식과 일치하고 좋은 것 같아요 👍 |
||
String jsonResponse = objectMapper.writeValueAsString(errorResponse); | ||
|
||
response.setContentType("application/json"); | ||
response.setCharacterEncoding("UTF-8"); | ||
response.setStatus(HttpServletResponse.SC_FORBIDDEN); | ||
response.getWriter().write(jsonResponse); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
package com.pawwithu.connectdog.jwt; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.pawwithu.connectdog.error.dto.ErrorResponse; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
|
@@ -10,6 +12,8 @@ | |
|
||
import java.io.IOException; | ||
|
||
import static com.pawwithu.connectdog.error.ErrorCode.NOT_AUTHENTICATED_REQUEST; | ||
|
||
/** | ||
* 유효한 자격 증명을 제공하지 않고 접근하려 할 때, 401 UnAuthorized 에러를 리턴 | ||
* -> Spring Security가 AuthenticationEntryPoint 인터페이스의 commence 메서드 호출 | ||
|
@@ -18,10 +22,22 @@ | |
@Component | ||
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint { | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
public JwtAuthenticationEntryPoint(ObjectMapper objectMapper) { | ||
this.objectMapper = objectMapper; | ||
} | ||
|
||
@Override | ||
public void commence(HttpServletRequest request, HttpServletResponse response, | ||
AuthenticationException authException) throws IOException, ServletException { | ||
log.info("인증되지 않은 요청입니다."); | ||
response.sendError(HttpServletResponse.SC_UNAUTHORIZED); | ||
ErrorResponse errorResponse = ErrorResponse.of(NOT_AUTHENTICATED_REQUEST.getCode(), NOT_AUTHENTICATED_REQUEST.getMessage()); | ||
String jsonResponse = objectMapper.writeValueAsString(errorResponse); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 인증되지 않은 요청 응답 설정도 좋습니다 👍 |
||
|
||
response.setContentType("application/json"); | ||
response.setCharacterEncoding("UTF-8"); | ||
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); | ||
response.getWriter().write(jsonResponse); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
신청을 할 때는 인증 완료한 봉사자만 신청 가능하게 하고, 중개의 경우는 모두 인증된 중개자만 접근하게 하는 권한 설정이네요. 처음 설계할 때 이동 봉사자와 중개를 분리하고, 각각 인증 절차를 거친 후에만 접근 가능한 기능을 구분했던 부분이 이렇게 마무리 되는 기분이에요! (기분 좋습니다😊)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
맞아요! 각각의 권한으로 테스트를 진행했는데 잘 동작하는 것 같습니다 ㅎ ㅎ