-
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.
Browse files
Browse the repository at this point in the history
…efactor Feature/#21 security refactor
- Loading branch information
Showing
11 changed files
with
128 additions
and
42 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
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
47 changes: 47 additions & 0 deletions
47
backend/src/main/java/com/ssafy/home/global/auth/filter/JwtExceptionFilter.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,47 @@ | ||
package com.ssafy.home.global.auth.filter; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.ssafy.home.global.error.ErrorCode; | ||
import com.ssafy.home.global.error.ErrorResponse; | ||
import com.ssafy.home.global.error.exception.AuthenticationException; | ||
import io.jsonwebtoken.JwtException; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import java.io.IOException; | ||
import java.io.UnsupportedEncodingException; | ||
|
||
@Slf4j | ||
public class JwtExceptionFilter extends OncePerRequestFilter { | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { | ||
try { | ||
filterChain.doFilter(request, response); | ||
} catch (AuthenticationException e) { | ||
setErrorResponse(response, ErrorCode.ACCESS_TOKEN_REFRESH); | ||
} catch (JwtException | IllegalArgumentException | NullPointerException | UnsupportedEncodingException e) { | ||
setErrorResponse(response, ErrorCode.NOT_VALID_TOKEN); | ||
} | ||
} | ||
|
||
private void setErrorResponse(HttpServletResponse response, ErrorCode errorCode) { | ||
log.error("filter에서 에러 체크"); | ||
|
||
ObjectMapper objectMapper = new ObjectMapper(); | ||
response.setStatus(errorCode.getHttpStatus().value()); | ||
response.setContentType(MediaType.APPLICATION_JSON_VALUE); | ||
response.setCharacterEncoding("UTF-8"); | ||
|
||
try { | ||
response.getWriter().write(objectMapper.writeValueAsString(ErrorResponse.of(errorCode.getErrorCode(),errorCode.getMessage()))); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
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
14 changes: 14 additions & 0 deletions
14
backend/src/main/java/com/ssafy/home/global/config/security/WebSecurityPath.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,14 @@ | ||
package com.ssafy.home.global.config.security; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public enum WebSecurityPath { | ||
REQUIRE_AUTH_PATH("/health/**", "member/logout", "member/info", "member/pw", "/board/**", "/comment/**"); | ||
|
||
private final String[] paths; | ||
|
||
WebSecurityPath(String... paths) { | ||
this.paths = paths; | ||
} | ||
} |
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