37 lines
1.6 KiB
Java
37 lines
1.6 KiB
Java
package com.alttd.altitudeweb.config;
|
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.security.access.AccessDeniedException;
|
|
import org.springframework.security.core.AuthenticationException;
|
|
import org.springframework.security.web.AuthenticationEntryPoint;
|
|
import org.springframework.security.web.access.AccessDeniedHandler;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.io.IOException;
|
|
|
|
@Slf4j
|
|
@Component
|
|
public class SecurityAuthFailureHandler implements AccessDeniedHandler, AuthenticationEntryPoint {
|
|
|
|
@Override
|
|
public void handle(HttpServletRequest request, HttpServletResponse response,
|
|
AccessDeniedException accessDeniedException) throws IOException {
|
|
log.warn("Access denied: User '{}' attempted to access '{}' without proper permissions",
|
|
request.getUserPrincipal() != null ? request.getUserPrincipal().getName() : "unknown",
|
|
request.getRequestURI());
|
|
|
|
response.sendError(HttpServletResponse.SC_FORBIDDEN, "Access Denied");
|
|
}
|
|
|
|
@Override
|
|
public void commence(HttpServletRequest request, HttpServletResponse response,
|
|
AuthenticationException authException) throws IOException {
|
|
log.warn("Authentication failure: Unauthenticated user attempted to access secured endpoint '{}'",
|
|
request.getRequestURI());
|
|
|
|
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Authentication Required");
|
|
}
|
|
}
|