-
Notifications
You must be signed in to change notification settings - Fork 3
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 #18 from KUIT-Couphone/feature/jwtFilter
feat: JWT Authentication 로직 개선 및 예외처리 보완
- Loading branch information
Showing
6 changed files
with
134 additions
and
21 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/main/java/com/example/couphoneserver/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 com.example.couphoneserver.config; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Component | ||
public class JwtExceptionFilter extends OncePerRequestFilter { | ||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { | ||
try { | ||
filterChain.doFilter(request, response); // go to JwtAuthenticationFilter | ||
} catch (Exception e) { | ||
setErrorResponse(request, response, e); | ||
} | ||
} | ||
|
||
public void setErrorResponse(HttpServletRequest request, HttpServletResponse response, Exception e) throws IOException { | ||
response.setContentType(MediaType.APPLICATION_JSON_VALUE); | ||
final Map<String, Object> body = new HashMap<>(); | ||
|
||
body.put("status", HttpServletResponse.SC_UNAUTHORIZED); | ||
body.put("error", "Unauthorized"); | ||
body.put("message", e.getMessage()); | ||
body.put("path", request.getServletPath()); | ||
|
||
final ObjectMapper mapper = new ObjectMapper(); | ||
mapper.writeValue(response.getOutputStream(), body); | ||
response.setStatus(HttpServletResponse.SC_OK); | ||
} | ||
} |
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
17 changes: 17 additions & 0 deletions
17
src/main/java/com/example/couphoneserver/config/permitAllUrl.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,17 @@ | ||
package com.example.couphoneserver.config; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class permitAllUrl { | ||
private static final Set<String> urlSet; | ||
|
||
static { | ||
urlSet = new HashSet<>(); | ||
urlSet.add("/auth/login"); | ||
} | ||
|
||
public static boolean of(String url) { | ||
return urlSet.contains(url); | ||
} | ||
} |
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