package com.yfd.platform.exception;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.yfd.platform.config.ResponseResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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;
@Component
public class AuthenticationException implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, org.springframework.security.core.AuthenticationException authException) throws IOException, ServletException {
        JSONObject jobj=new JSONObject();
        if(authException.getMessage().equals("用户账号不存在！")){
            jobj.putOnce("code","401");
            jobj.putOnce("msg","用户账号不存在/密码错误，登录失败！");
        }else{
            jobj.putOnce("code","401");
            jobj.putOnce("msg","用户Token失效，请重新登录！");
        }
        response.setStatus(200);
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        response.getWriter().println(JSONUtil.toJsonStr(jobj));
    }
}
