package com.jiayue.ssi.handler;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Service;

/**
 * 登录失败：解决匿名用户访问无权限资源时的异常
 * @author xsl
 * @date 2023/4/4
 */
@Service
public class EntryPointUnauthorizedHandler implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setStatus(406);
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("认证异常，请先登录！");
    }
}
