package com.cool.bang.core.secure.process;

import com.cool.bang.common.tools.servlet.ServletUtil;
import com.cool.bang.common.web.response.Result;
import com.xiaoleilu.hutool.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

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

/**
 * 自定义 spring security 用户未登录处理类
 *
 * @author PeiZhi Guo
 * 2022-4-13 16:04
 */
@Component
public class SecurityEntryPointHandler implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
                         AuthenticationException e) throws IOException, ServletException {
        ServletUtil.jsonWrite(Result.failure(HttpStatus.HTTP_UNAUTHORIZED,"请先登录"), HttpStatus.HTTP_UNAUTHORIZED);
    }
}
