-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Type: token expired message 반환 (#70)
[Fix] token expired message 반환
- Loading branch information
Showing
5 changed files
with
63 additions
and
40 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
40 changes: 40 additions & 0 deletions
40
src/main/java/igoMoney/BE/common/config/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,40 @@ | ||
package igoMoney.BE.common.config; | ||
|
||
import igoMoney.BE.common.exception.ErrorResponse; | ||
import io.jsonwebtoken.JwtException; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import java.io.IOException; | ||
|
||
@Component | ||
public class JwtExceptionFilter extends OncePerRequestFilter { | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws ServletException, IOException { | ||
try { | ||
chain.doFilter(req, res); // go to 'JwtAuthFilter' | ||
} catch (JwtException ex) { | ||
setErrorResponse(HttpStatus.UNAUTHORIZED, res, ex); | ||
} | ||
} | ||
|
||
public void setErrorResponse(HttpStatus status, HttpServletResponse res, Throwable ex) throws IOException { | ||
res.setStatus(status.value()); | ||
res.setContentType("application/json; charset=UTF-8"); | ||
|
||
ErrorResponse jwtExceptionResponse = ErrorResponse.builder() | ||
.status(HttpStatus.UNAUTHORIZED.value()) | ||
.error(HttpStatus.UNAUTHORIZED.name()) | ||
.code("TOKEN") | ||
.detail(ex.getMessage()) | ||
.build(); | ||
jwtExceptionResponse.setDateNull(); | ||
res.getWriter().write(jwtExceptionResponse.convertToJson()); | ||
} | ||
} |
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