content: package com.www.security.exception;
  
  import cn.hutool.json.JSONUtil;
  import com.www.vo.Result;
  import org.springframework.security.core.AuthenticationException;
  import org.springframework.security.web.AuthenticationEntryPoint;
  import org.springframework.stereotype.Component;
  
  import javax.servlet.ServletException;
  import javax.servlet.ServletOutputStream;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import java.io.IOException;
  
  /**
   * Created with IntelliJ IDEA 2021.
   *
   * @Author: Mr Qin
   * @Date: 2024/03/25/19:44
   * @Description:
   */
  @Component
  public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {
  
  
      @Override
      public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
          // 以流的形式返回
          response.setContentType("/application/json;charset=UTF-8");
          // 设置响应状态码，未认证---401
          response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
          ServletOutputStream outputStream = response.getOutputStream();
  
          // 未登录时访问资源，要求先登录
          Result result = Result.fail("请先登录");
          outputStream.write(JSONUtil.toJsonStr(result).getBytes("UTF-8"));
          outputStream.flush();
          outputStream.close();
      }
  }
  