36 lines
1.3 KiB
Java
36 lines
1.3 KiB
Java
package ru.mtusi.messenger.security;
|
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
import lombok.RequiredArgsConstructor;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.security.core.AuthenticationException;
|
|
import org.springframework.security.web.AuthenticationEntryPoint;
|
|
import org.springframework.stereotype.Component;
|
|
import ru.mtusi.messenger.dto.ErrorErrorDTO;
|
|
import ru.mtusi.messenger.exception.ApiErrorFactory;
|
|
|
|
import java.io.IOException;
|
|
|
|
@Component
|
|
@RequiredArgsConstructor
|
|
public class ApiAuthenticationEntryPoint implements AuthenticationEntryPoint {
|
|
private final ApiErrorFactory errors;
|
|
|
|
@Override
|
|
public void commence(HttpServletRequest request, HttpServletResponse response,
|
|
AuthenticationException authException) throws IOException {
|
|
boolean isLogin = "/api/v1/auth/sessions".equals(request.getRequestURI());
|
|
String challenge = isLogin
|
|
? "Basic realm=\"messenger\", charset=\"UTF-8\""
|
|
: "Bearer realm=\"messenger\"";
|
|
|
|
errors.writeError(
|
|
response,
|
|
request,
|
|
HttpStatus.UNAUTHORIZED,
|
|
challenge,
|
|
ErrorErrorDTO.CodeEnum.UNAUTHORIZED,
|
|
"No valid authentication");
|
|
}
|
|
} |