puzhibing
2026-05-14 4f7fd9e879d9de211b255eb21535a034893ffbff
xp-robotaxi-gateway-boot/src/main/java/com/xiaopeng/gateway/filter/AuthFilter.java
@@ -1,10 +1,14 @@
package com.xiaopeng.gateway.filter;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.xiaopeng.common.core.exception.auth.NotLoginException;
import com.xiaopeng.gateway.config.properties.AntiShakeProperties;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
@@ -27,6 +31,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
@@ -46,6 +51,15 @@
   @Autowired
   private RedisService redisService;
    @Value("${xiaopeng.xtiger.sso.issuer}")
    private String issuer;
    @Value("${xiaopeng.xtiger.sso.appId}")
    private String appId;
    @Value("${xiaopeng.xtiger.sso.serverHost}")
    private String serverHost;
   
   
   
@@ -117,7 +131,19 @@
   
   private Mono<Void> unauthorizedResponse(ServerWebExchange exchange, String msg) {
      log.error("[鉴权异常处理]请求路径:{}", exchange.getRequest().getPath() + "\n" + msg);
      return ServletUtils.webFluxResponseWriter(exchange.getResponse(), msg, HttpStatus.UNAUTHORIZED);
        String s = UUID.randomUUID().toString();
        String state1 = s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18) + s.substring(19, 23) + s.substring(24);
        String s1 = UUID.randomUUID().toString();
        String state2 = s1.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18) + s.substring(19, 23) + s.substring(24);
        String nonce = RandomStringUtils.randomAlphanumeric(6);
        JSONObject jsonObject = JSON.parseObject("{\n" +
                "    \"uid\": null,\n" +
                "    \"username\": null,\n" +
                "    \"authUrl\": \"" + issuer + "/oidc/auth?scope=openid+profile+offline_access+phone+email&response_type=code&redirect_uri=" + serverHost + "/dashboard&state=" + state1 + "&nonce=" + nonce + "&client_id=" + appId + "\",\n" +
                "    \"logoutUrl\": null,\n" +
                "    \"callbackUrl\": \"" + serverHost + "/dashboard?state=" + state2 + "\"\n" +
                "  }");
      return ServletUtils.webFluxResponseWriter(exchange.getResponse(), jsonObject.toString(), HttpStatus.UNAUTHORIZED);
   }
   
   
@@ -140,7 +166,7 @@
      return token;
   }
   
   
   /**