6565import org .springframework .web .client .RestTemplate ;
6666
6767import com .alibaba .fastjson .JSONObject ;
68+ import com .fasterxml .jackson .databind .util .LRUMap ;
6869
6970import apijson .JSON ;
7071import apijson .JSONResponse ;
@@ -714,6 +715,8 @@ public JSONObject login(@RequestBody String request, HttpSession session) {
714715 @ PostMapping ("logout" )
715716 @ Override
716717 public JSONObject logout (HttpSession session ) {
718+ SESSION_MAP .remove (session .getId ());
719+
717720 long userId ;
718721 try {
719722 userId = DemoVerifier .getVisitorId (session );//必须在session.invalidate();前!
@@ -1074,10 +1077,24 @@ public JSONObject putBalance(@RequestBody String request, HttpSession session) {
10741077
10751078
10761079 // 为 APIAuto 提供的代理接口(解决跨域问题) 和 导入第三方文档的测试接口 https://github.com/TommyLemon/APIAuto <<<<<<<<<<<<<<<<<<<<<<<<<<<
1080+
1081+ public static class SessionMap extends LRUMap <String , HttpSession > {
1082+ public SessionMap () {
1083+ super (16 , 1000000 );
1084+ }
1085+ public void remove (String key ) {
1086+ _map .remove (key );
1087+ }
1088+ }
1089+
1090+ public static final SessionMap SESSION_MAP ;
10771091
10781092 public static final String ADD_COOKIE = "Add-Cookie" ;
1093+ public static final String APIJSON_DELEGATE_ID = "APIJSON-DELEGATE-ID" ;
10791094 public static final List <String > EXCEPT_HEADER_LIST ;
10801095 static {
1096+ SESSION_MAP = new SessionMap ();
1097+
10811098 EXCEPT_HEADER_LIST = Arrays .asList ( //accept-encoding 在某些情况下导致乱码,origin 和 sec-fetch-mode 等 CORS 信息导致服务器代理失败
10821099 "accept-encoding" , "accept-language" , // "accept", "connection"
10831100 "host" , "origin" , "referer" , "user-agent" , "sec-fetch-mode" , "sec-fetch-site" , "sec-fetch-dest" , "sec-fetch-user"
@@ -1100,16 +1117,18 @@ public JSONObject putBalance(@RequestBody String request, HttpSession session) {
11001117 @ SuppressWarnings ("unchecked" )
11011118 @ RequestMapping (value = "delegate" )
11021119 public String delegate (
1120+ @ RequestParam ("$_delegate_url" ) String url ,
11031121 @ RequestParam (value = "$_type" , required = false ) String type ,
11041122 @ RequestParam (value = "$_except_headers" , required = false ) String exceptHeaders ,
1105- @ RequestParam ("$_delegate_url" ) String url ,
1123+ @ RequestParam (value = "$_delegate_id" , required = false ) String sessionId ,
11061124 @ RequestBody (required = false ) String body ,
11071125 HttpMethod method , HttpSession session
11081126 ) {
11091127
11101128 if (Log .DEBUG == false ) {
11111129 return DemoParser .newErrorResult (new IllegalAccessException ("非 DEBUG 模式下不允许使用服务器代理!" )).toJSONString ();
11121130 }
1131+
11131132
11141133 if ("GRPC" .equals (type )) {
11151134 int index = url .indexOf ("://" );
@@ -1145,6 +1164,7 @@ public String delegate(
11451164
11461165 List <String > setCookie = null ;
11471166 List <String > addCookie = null ;
1167+ List <String > apijsonDelegateId = null ;
11481168
11491169 while (names .hasMoreElements ()) {
11501170 name = names .nextElement ();
@@ -1156,15 +1176,29 @@ public String delegate(
11561176 else if (ADD_COOKIE .toLowerCase ().equals (name .toLowerCase ())) {
11571177 addCookie = Arrays .asList (request .getHeader (name ));
11581178 }
1179+ else if (APIJSON_DELEGATE_ID .toLowerCase ().equals (name .toLowerCase ())) {
1180+ apijsonDelegateId = Arrays .asList (request .getHeader (name ));
1181+ }
11591182 else {
11601183 headers .add (name , request .getHeader (name ));
11611184 }
11621185 }
11631186 }
1187+
1188+ if (sessionId == null ) {
1189+ sessionId = apijsonDelegateId == null || apijsonDelegateId .isEmpty () ? null : apijsonDelegateId .get (0 );
1190+ }
1191+ if (sessionId != null ) {
1192+ HttpSession s = SESSION_MAP .get (sessionId );
1193+ if (s != null ) {
1194+ s = session ;
1195+ }
1196+ }
11641197
11651198 if (setCookie == null && session != null ) {
11661199 setCookie = (List <String >) session .getAttribute (COOKIE );
11671200 }
1201+
11681202 if (addCookie != null && addCookie .isEmpty () == false ) {
11691203 if (setCookie == null ) {
11701204 setCookie = addCookie ;
@@ -1223,6 +1257,10 @@ else if (ADD_COOKIE.toLowerCase().equals(name.toLowerCase())) {
12231257 session .setAttribute (COOKIE , cookie );
12241258 }
12251259 }
1260+
1261+ SESSION_MAP .put (session .getId (), session );
1262+ response .setHeader (APIJSON_DELEGATE_ID , session .getId ());
1263+
12261264 return entity .getBody ();
12271265 }
12281266
0 commit comments