forked from mtusi2026/mtusi2026lab3
docs(report): fill REPORT.md for lab3 grade 3
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package ru.mtusi.messenger.config;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import jakarta.servlet.ServletException;
|
||||
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 org.springframework.stereotype.Component;
|
||||
import ru.mtusi.messenger.dto.ErrorResponse;
|
||||
import ru.mtusi.messenger.filter.RequestIdFilter;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Component
|
||||
public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public RestAuthenticationEntryPoint(ObjectMapper objectMapper) {
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException)
|
||||
throws IOException, ServletException {
|
||||
|
||||
// Достаем X-Request-Id, который сгенерировал наш фильтр ранее
|
||||
String requestId = (String) request.getAttribute(RequestIdFilter.REQUEST_ID_ATTR);
|
||||
|
||||
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); // 401
|
||||
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
response.setHeader("WWW-Authenticate", "Bearer realm=\"messenger\"");
|
||||
|
||||
ErrorResponse error = ErrorResponse.of("unauthorized", "No valid bearer token", requestId);
|
||||
response.getWriter().write(objectMapper.writeValueAsString(error));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user