package com.example.jz.auth;
|
|
import cn.hutool.json.JSONUtil;
|
import com.example.jz.modle.R;
|
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;
|
import java.io.PrintWriter;
|
|
/**
|
* @author 安瑾然
|
* @data 2022/7/18 - 10:55 AM
|
* @description 未登录处理
|
*/
|
@Component
|
public class MyUnAuthEntryPoint implements AuthenticationEntryPoint {
|
@Override
|
public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
|
// 设置返回消息类型
|
httpServletResponse.setHeader("Content-type", "text/html;charset=UTF-8");
|
httpServletResponse.setCharacterEncoding("utf-8");
|
httpServletResponse.setContentType("application/json;charset=UTF-8");
|
// 返回给请求端
|
PrintWriter writer = httpServletResponse.getWriter();
|
writer.write(JSONUtil.toJsonStr(R.failed("当前账户未登录")));
|
writer.close();
|
}
|
}
|