-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJwtAuthenticationEntryPoint.java
More file actions
60 lines (50 loc) · 2.29 KB
/
Copy pathJwtAuthenticationEntryPoint.java
File metadata and controls
60 lines (50 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.leets.tdd.global.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.leets.tdd.global.jwt.JwtAuthErrorType;
import com.leets.tdd.global.common.ApiResponse;
import com.leets.tdd.user.exception.UserErrorCode;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.MediaType;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import java.io.IOException;
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {
private final ObjectMapper objectMapper;
public JwtAuthenticationEntryPoint(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
@Override
public void commence(
HttpServletRequest request,
HttpServletResponse response,
AuthenticationException authException
) throws IOException {
if (request.getRequestURI().matches("/api/v1/(parties|delivery-parties)/[^/]+/join")) {
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.setCharacterEncoding("UTF-8");
response.getWriter().write(
objectMapper.writeValueAsString(ApiResponse.fail("로그인이 필요합니다."))
);
return;
}
Object errorType = request.getAttribute(JwtAuthenticationFilter.JWT_ERROR_ATTRIBUTE);
UserErrorCode errorCode;
if (JwtAuthErrorType.MISSING.equals(errorType)) {
errorCode = UserErrorCode.TOKEN_MISSING;
} else if (JwtAuthErrorType.EXPIRED.equals(errorType)) {
errorCode = UserErrorCode.TOKEN_EXPIRED;
} else if (JwtAuthErrorType.BANNED.equals(errorType)) {
errorCode = UserErrorCode.ACCOUNT_BANNED;
} else {
errorCode = UserErrorCode.INVALID_TOKEN;
}
response.setStatus(errorCode.getHttpStatus().value());
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.setCharacterEncoding("UTF-8");
response.getWriter().write(
objectMapper.writeValueAsString(ApiResponse.fail(errorCode.getMessage()))
);
}
}