-
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.
Fix: Spring Serurity 수정(권한설정 & 세션관리기능 수정)
- Loading branch information
Showing
5 changed files
with
106 additions
and
2 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
src/main/java/com/medicare/neulpeum/config/CustomAccessDenyHandler.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,26 @@ | ||
package com.medicare.neulpeum.config; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.access.AccessDeniedException; | ||
import org.springframework.security.web.access.AccessDeniedHandler; | ||
|
||
import java.io.IOException; | ||
|
||
public class CustomAccessDenyHandler implements AccessDeniedHandler { | ||
@Override | ||
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException { | ||
ErrorResponse errorResponse = new ErrorResponse(ErrorCode.FORBIDDEN_CLIENT); | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
String jsonErrorResponse = objectMapper.writeValueAsString(errorResponse); | ||
|
||
response.setStatus(HttpStatus.FORBIDDEN.value()); | ||
response.setCharacterEncoding("utf-8"); | ||
response.setContentType(MediaType.APPLICATION_JSON_VALUE); // application/json | ||
response.getWriter().write(jsonErrorResponse); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/medicare/neulpeum/config/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,26 @@ | ||
package com.medicare.neulpeum.config; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.web.AuthenticationEntryPoint; | ||
|
||
import java.io.IOException; | ||
|
||
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint { | ||
@Override | ||
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { | ||
ErrorResponse errorResponse = new ErrorResponse(ErrorCode.UNAUTHORIZED_CLIENT); | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
String jsonErrorResponse = objectMapper.writeValueAsString(errorResponse); | ||
|
||
response.setStatus(HttpStatus.BAD_REQUEST.value()); | ||
response.setCharacterEncoding("utf-8"); | ||
response.setContentType(MediaType.APPLICATION_JSON_VALUE); // application/json | ||
response.getWriter().write(jsonErrorResponse); | ||
} | ||
} |
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,22 @@ | ||
package com.medicare.neulpeum.config; | ||
|
||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
public enum ErrorCode { | ||
UNAUTHORIZED_CLIENT(HttpStatus.BAD_REQUEST, "접근 토큰이 없습니다."), | ||
FORBIDDEN_CLIENT(HttpStatus.FORBIDDEN, "접근 권한이 없습니다."), | ||
EXPIRED_TOKEN(HttpStatus.UNAUTHORIZED, "만료된 토큰입니다."), | ||
JWT_DECODE_FAIL(HttpStatus.UNAUTHORIZED, "올바른 토큰이 필요합니다."), | ||
JWT_SIGNATURE_FAIL(HttpStatus.UNAUTHORIZED, "올바른 토큰이 필요합니다."); | ||
|
||
private HttpStatus httpStatus; | ||
private String message; | ||
|
||
|
||
ErrorCode(HttpStatus httpStatus, String message) { | ||
this.httpStatus = httpStatus; | ||
this.message = message; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/medicare/neulpeum/config/ErrorResponse.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,16 @@ | ||
package com.medicare.neulpeum.config; | ||
|
||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
public class ErrorResponse { | ||
private String errorCode; | ||
private String message; | ||
|
||
public ErrorResponse(ErrorCode errorCode) { | ||
this.errorCode = errorCode.name(); | ||
this.message = errorCode.getMessage(); | ||
} | ||
} |
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