-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
139 additions
and
71 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
26 changes: 22 additions & 4 deletions
26
src/main/java/team7/inplace/security/entryPoint/LoginAuthenticationEntryPoint.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 |
---|---|---|
@@ -1,20 +1,38 @@ | ||
package team7.inplace.security.entryPoint; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ProblemDetail; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.web.AuthenticationEntryPoint; | ||
import team7.inplace.global.exception.InplaceException; | ||
import team7.inplace.global.exception.code.AuthorizationErrorCode; | ||
|
||
public class LoginAuthenticationEntryPoint implements AuthenticationEntryPoint { | ||
|
||
@Value("${spring.redirect.front-end-url}") | ||
private String frontEndUrl; | ||
private final ObjectMapper objectMapper; | ||
|
||
public LoginAuthenticationEntryPoint(ObjectMapper objectMapper) { | ||
this.objectMapper = objectMapper; | ||
} | ||
|
||
@Override | ||
public void commence(HttpServletRequest request, HttpServletResponse response, | ||
AuthenticationException authException) throws IOException { | ||
response.sendRedirect(frontEndUrl); | ||
setErrorResponse(response, InplaceException.of(AuthorizationErrorCode.NOT_AUTHENTICATION)); | ||
} | ||
|
||
private void setErrorResponse( | ||
HttpServletResponse response, | ||
InplaceException inplaceException | ||
) throws IOException { | ||
response.setStatus(inplaceException.getHttpStatus().value()); | ||
response.setContentType(MediaType.APPLICATION_JSON_VALUE); | ||
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail( | ||
inplaceException.getHttpStatus(), inplaceException.getMessage()); | ||
response.getWriter().write(objectMapper.writeValueAsString(problemDetail)); | ||
} | ||
} |
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