-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from WE-ARE-RACCOONS/RAC-128
RAC-128 feat: Security 예외 처리
- Loading branch information
Showing
5 changed files
with
83 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/main/java/com/postgraduate/global/config/security/filter/CustomAccessDeniedHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.postgraduate.global.config.security.filter; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.postgraduate.global.dto.ErrorResponse; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.access.AccessDeniedException; | ||
import org.springframework.security.web.access.AccessDeniedHandler; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.IOException; | ||
|
||
import static com.postgraduate.domain.auth.presentation.contant.AuthResponseCode.AUTH_DENIED; | ||
import static com.postgraduate.domain.auth.presentation.contant.AuthResponseMessage.PERMISSION_DENIED; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class CustomAccessDeniedHandler implements AccessDeniedHandler { | ||
private final ObjectMapper objectMapper; | ||
@Override | ||
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException { | ||
response.setStatus(HttpStatus.FORBIDDEN.value()); | ||
response.setContentType(MediaType.APPLICATION_JSON_VALUE); | ||
response.setCharacterEncoding("UTF-8"); | ||
objectMapper.writeValue( | ||
response.getOutputStream(), | ||
new ErrorResponse(AUTH_DENIED.getCode(), PERMISSION_DENIED.getMessage()) | ||
); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...n/java/com/postgraduate/global/config/security/filter/CustomAuthenticationEntryPoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.postgraduate.global.config.security.filter; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.postgraduate.global.dto.ErrorResponse; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.web.AuthenticationEntryPoint; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.IOException; | ||
|
||
import static com.postgraduate.domain.auth.presentation.contant.AuthResponseCode.AUTH_FAILED; | ||
import static com.postgraduate.domain.auth.presentation.contant.AuthResponseMessage.FAILED_AUTH; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint { | ||
private final ObjectMapper objectMapper; | ||
|
||
@Override | ||
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { | ||
response.setStatus(HttpStatus.UNAUTHORIZED.value()); | ||
response.setContentType(MediaType.APPLICATION_JSON_VALUE); | ||
response.setCharacterEncoding("UTF-8"); | ||
objectMapper.writeValue( | ||
response.getOutputStream(), | ||
new ErrorResponse(AUTH_FAILED.getCode(), FAILED_AUTH.getMessage()) | ||
); | ||
} | ||
} |