-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminAuthenticationEntryPoint.java
More file actions
41 lines (33 loc) · 1.6 KB
/
AdminAuthenticationEntryPoint.java
File metadata and controls
41 lines (33 loc) · 1.6 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
package com.acon.server.global.admin;
import com.acon.server.global.dto.ErrorResponse;
import com.acon.server.global.exception.ErrorType;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;
@Component
@RequiredArgsConstructor
public class AdminAuthenticationEntryPoint implements AuthenticationEntryPoint {
private final ObjectMapper objectMapper;
@Override
public void commence(HttpServletRequest request,
HttpServletResponse response,
AuthenticationException authException) throws IOException {
if (authException instanceof InsufficientAuthenticationException) {
ErrorType errorType = ErrorType.UNAUTHORIZED_ERROR;
response.setStatus(errorType.getHttpStatus().value());
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
objectMapper.writeValue(response.getOutputStream(), ErrorResponse.fail(errorType));
return;
}
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
}
}