package cn.reghao.oss.console.account.security;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 在 WebSecurityConfig 中配置的需要认证的接口没有获取到请求中的认证信息时会转到此处进行处理
 *
 * @author reghao
 * @date 2023-07-28 16:43:59
 */
@Slf4j
public class ExceptionAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception)
            throws IOException, ServletException {
        String uri = request.getRequestURI();
        String errMsg = exception.getMessage();
        log.error("请求 {} 接口认证失败: {}, 重定向到登录页面", uri, errMsg);
        response.sendRedirect("/login");
    }
}
