content: package com.www.security.exception;
  
  import cn.hutool.json.JSONUtil;
  import com.www.vo.Result;
  import org.springframework.security.access.AccessDeniedException;
  import org.springframework.security.web.access.AccessDeniedHandler;
  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:45
   * @Description:        jwt访问拒绝处理器
   */
  @Component
  public class JwtAccessDeniedHandler implements AccessDeniedHandler {
      @Override
      public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
          // 以流的形式返回
          response.setContentType("/application/json;charset=UTF-8");
          // 设置响应权限不足的状态码---403
          response.setStatus(HttpServletResponse.SC_FORBIDDEN);
          ServletOutputStream outputStream = response.getOutputStream();
  
          Result result = Result.fail(accessDeniedException.getMessage());
          outputStream.write(JSONUtil.toJsonStr(result).getBytes("UTF-8"));
          outputStream.flush();
          outputStream.close();
      }
  }
  