content: package com.xzll.gateway.component;
  
  import cn.hutool.json.JSONUtil;
  import com.xzll.common.constant.answercode.AnswerCode;
  import com.xzll.common.pojo.base.WebBaseResponse;
  import org.springframework.core.io.buffer.DataBuffer;
  import org.springframework.http.HttpHeaders;
  import org.springframework.http.HttpStatus;
  import org.springframework.http.MediaType;
  import org.springframework.http.server.reactive.ServerHttpResponse;
  import org.springframework.security.core.AuthenticationException;
  import org.springframework.security.web.server.ServerAuthenticationEntryPoint;
  import org.springframework.stereotype.Component;
  import org.springframework.web.server.ServerWebExchange;
  import reactor.core.publisher.Mono;
  
  import java.nio.charset.StandardCharsets;
  
  /**
   * @Author: hzz
   * @Date: 2024/6/10 09:50:47
   * @Description: token无效处理，即未认证。在这里自定义处理JWT请求头过期或签名错误的结果
   */
  @Component
  public class RestAuthenticationEntryPoint implements ServerAuthenticationEntryPoint {
      @Override
      public Mono<Void> commence(ServerWebExchange exchange, AuthenticationException e) {
          ServerHttpResponse response = exchange.getResponse();
          response.setStatusCode(HttpStatus.OK);
          response.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
          String body= JSONUtil.toJsonStr(WebBaseResponse.returnResultError(AnswerCode.TOKEN_INVALID.getCode(), e.getMessage()));
          DataBuffer buffer =  response.bufferFactory().wrap(body.getBytes(StandardCharsets.UTF_8));
          return response.writeWith(Mono.just(buffer));
      }
  }
  