29
29
import static apijson .framework .APIJSONConstant .REQUEST_ ;
30
30
import static apijson .framework .APIJSONConstant .USER_ID ;
31
31
import static apijson .framework .APIJSONConstant .VERSION ;
32
+ import static org .springframework .http .HttpHeaders .COOKIE ;
33
+ import static org .springframework .http .HttpHeaders .SET_COOKIE ;
32
34
33
35
import java .net .URLDecoder ;
34
36
import java .rmi .ServerException ;
@@ -232,7 +234,7 @@ public String openHead(@PathVariable String request, HttpSession session) {
232
234
233
235
234
236
235
-
237
+
236
238
public static final String USER_ ;
237
239
public static final String PRIVACY_ ;
238
240
public static final String VERIFY_ ; //加下划线后缀是为了避免 Verify 和 verify 都叫VERIFY,分不清
@@ -242,7 +244,7 @@ public String openHead(@PathVariable String request, HttpSession session) {
242
244
VERIFY_ = Verify .class .getSimpleName ();
243
245
}
244
246
245
-
247
+
246
248
247
249
public static final String CURRENT_USER_ID = "currentUserId" ;
248
250
public static final String NAME = "name" ;
@@ -603,7 +605,7 @@ public JSONObject login(@RequestBody String request, HttpSession session) {
603
605
session .setAttribute (PRIVACY_ , privacy ); //用户隐私信息
604
606
session .setAttribute (REMEMBER , remember ); //是否记住登录
605
607
session .setMaxInactiveInterval (60 *60 *24 *(remember ? 7 : 1 )); //设置session过期时间
606
-
608
+
607
609
response .put (REMEMBER , remember );
608
610
response .put (DEFAULTS , defaults );
609
611
return response ;
@@ -974,7 +976,7 @@ public JSONObject putBalance(@RequestBody String request, HttpSession session) {
974
976
}
975
977
976
978
977
- public static final String COOKIE = "Cookie" ;
979
+ public static final String ADD_COOKIE = "Add- Cookie" ;
978
980
public static final List <String > EXCEPT_HEADER_LIST ;
979
981
static {
980
982
EXCEPT_HEADER_LIST = Arrays .asList ( //accept-encoding 在某些情况下导致乱码,origin 和 sec-fetch-mode 等 CORS 信息导致服务器代理失败
@@ -996,6 +998,7 @@ public JSONObject putBalance(@RequestBody String request, HttpSession session) {
996
998
* @param session HTTP session
997
999
* @return
998
1000
*/
1001
+ @ SuppressWarnings ("unchecked" )
999
1002
@ RequestMapping (value = "/delegate" )
1000
1003
public String delegate (
1001
1004
@ RequestParam (value = "$_except_headers" , required = false ) String exceptHeaders ,
@@ -1013,22 +1016,41 @@ public String delegate(
1013
1016
List <String > exceptHeaderList = StringUtil .isEmpty (exceptHeaders , true )
1014
1017
? EXCEPT_HEADER_LIST : Arrays .asList (StringUtil .split (exceptHeaders ));
1015
1018
1019
+
1020
+ List <String > setCookie = null ;
1021
+ List <String > addCookie = null ;
1022
+
1016
1023
while (names .hasMoreElements ()) {
1017
1024
name = names .nextElement ();
1018
1025
if (name != null && exceptHeaderList .contains (name .toLowerCase ()) == false ) {
1019
- headers .add (name , request .getHeader (name ));
1026
+ //APIAuto 是一定精准发送 Set-Cookie 名称过来的,预留其它命名可实现覆盖原 Cookie Header 等更多可能
1027
+ if (SET_COOKIE .toLowerCase ().equals (name .toLowerCase ())) { //接收到时就已经被强制小写
1028
+ setCookie = Arrays .asList (request .getHeader (name )); // JSON.parseArray(request.getHeader(name), String.class);
1029
+ }
1030
+ else if (ADD_COOKIE .toLowerCase ().equals (name .toLowerCase ())) {
1031
+ addCookie = Arrays .asList (request .getHeader (name ));
1032
+ }
1033
+ else {
1034
+ headers .add (name , request .getHeader (name ));
1035
+ }
1020
1036
}
1021
1037
}
1022
1038
1023
- @ SuppressWarnings ("unchecked" )
1024
- List <String > cookie = session == null ? null : (List <String >) session .getAttribute (COOKIE );
1025
- if (cookie != null && cookie .isEmpty () == false ) {
1026
- List <String > c = headers .get (COOKIE );
1027
- if (c == null ) {
1028
- c = new ArrayList <>();
1039
+ if (setCookie == null && session != null ) {
1040
+ setCookie = (List <String >) session .getAttribute (COOKIE );
1041
+ }
1042
+ if (addCookie != null && addCookie .isEmpty () == false ) {
1043
+ if (setCookie == null ) {
1044
+ setCookie = addCookie ;
1045
+ }
1046
+ else {
1047
+ setCookie = new ArrayList <>(setCookie );
1048
+ setCookie .addAll (addCookie );
1029
1049
}
1030
- c .addAll (cookie );
1031
- headers .put (COOKIE , c );
1050
+ }
1051
+
1052
+ if (setCookie != null ) { //允许传空的 Cookie && setCookie.isEmpty() == false) {
1053
+ headers .put (COOKIE , setCookie );
1032
1054
}
1033
1055
}
1034
1056
@@ -1069,7 +1091,7 @@ public String delegate(
1069
1091
1070
1092
HttpHeaders hs = entity .getHeaders ();
1071
1093
if (session != null && hs != null ) {
1072
- List <String > cookie = hs .get ("Set-Cookie" );
1094
+ List <String > cookie = hs .get (SET_COOKIE );
1073
1095
if (cookie != null && cookie .isEmpty () == false ) {
1074
1096
session .setAttribute (COOKIE , cookie );
1075
1097
}
@@ -1143,8 +1165,8 @@ public String swaggerAPIDocs() {
1143
1165
" }\n " +
1144
1166
"}" ;
1145
1167
}
1146
-
1147
-
1168
+
1169
+
1148
1170
1149
1171
1150
1172
@ PostMapping ("method/invoke" )
@@ -1158,13 +1180,13 @@ public JSONObject invokeMethod(@RequestBody String request) {
1158
1180
req ,
1159
1181
DemoApplication .getApplicationContext ().getBean (
1160
1182
Class .forName (pkgName .replaceAll ("/" , "." ) + "." + clsName )
1161
- )
1162
- );
1183
+ )
1184
+ );
1163
1185
}
1164
1186
} catch (Exception e ) {
1165
1187
Log .e (TAG , "listMethod try { JSONObject req = JSON.parseObject(request); ... } catch (Exception e) { \n " + e .getMessage ());
1166
1188
}
1167
-
1189
+
1168
1190
return super .invokeMethod (request );
1169
1191
}
1170
1192
0 commit comments