Skip to content

Commit 0a24a0e

Browse files
committed
delegate 接口支持前端通过 Set-Cookie 或 Add-Cookie 来自定义 Cookie
1 parent 0c0b681 commit 0a24a0e

File tree

1 file changed

+41
-19
lines changed

1 file changed

+41
-19
lines changed

APIJSON-Java-Server/APIJSONBoot/src/main/java/apijson/boot/DemoController.java

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import static apijson.framework.APIJSONConstant.REQUEST_;
3030
import static apijson.framework.APIJSONConstant.USER_ID;
3131
import static apijson.framework.APIJSONConstant.VERSION;
32+
import static org.springframework.http.HttpHeaders.COOKIE;
33+
import static org.springframework.http.HttpHeaders.SET_COOKIE;
3234

3335
import java.net.URLDecoder;
3436
import java.rmi.ServerException;
@@ -232,7 +234,7 @@ public String openHead(@PathVariable String request, HttpSession session) {
232234

233235

234236

235-
237+
236238
public static final String USER_;
237239
public static final String PRIVACY_;
238240
public static final String VERIFY_; //加下划线后缀是为了避免 Verify 和 verify 都叫VERIFY,分不清
@@ -242,7 +244,7 @@ public String openHead(@PathVariable String request, HttpSession session) {
242244
VERIFY_ = Verify.class.getSimpleName();
243245
}
244246

245-
247+
246248

247249
public static final String CURRENT_USER_ID = "currentUserId";
248250
public static final String NAME = "name";
@@ -603,7 +605,7 @@ public JSONObject login(@RequestBody String request, HttpSession session) {
603605
session.setAttribute(PRIVACY_, privacy); //用户隐私信息
604606
session.setAttribute(REMEMBER, remember); //是否记住登录
605607
session.setMaxInactiveInterval(60*60*24*(remember ? 7 : 1)); //设置session过期时间
606-
608+
607609
response.put(REMEMBER, remember);
608610
response.put(DEFAULTS, defaults);
609611
return response;
@@ -974,7 +976,7 @@ public JSONObject putBalance(@RequestBody String request, HttpSession session) {
974976
}
975977

976978

977-
public static final String COOKIE = "Cookie";
979+
public static final String ADD_COOKIE = "Add-Cookie";
978980
public static final List<String> EXCEPT_HEADER_LIST;
979981
static {
980982
EXCEPT_HEADER_LIST = Arrays.asList( //accept-encoding 在某些情况下导致乱码,origin 和 sec-fetch-mode 等 CORS 信息导致服务器代理失败
@@ -996,6 +998,7 @@ public JSONObject putBalance(@RequestBody String request, HttpSession session) {
996998
* @param session HTTP session
997999
* @return
9981000
*/
1001+
@SuppressWarnings("unchecked")
9991002
@RequestMapping(value = "/delegate")
10001003
public String delegate(
10011004
@RequestParam(value = "$_except_headers", required = false) String exceptHeaders,
@@ -1013,22 +1016,41 @@ public String delegate(
10131016
List<String> exceptHeaderList = StringUtil.isEmpty(exceptHeaders, true)
10141017
? EXCEPT_HEADER_LIST : Arrays.asList(StringUtil.split(exceptHeaders));
10151018

1019+
1020+
List<String> setCookie = null;
1021+
List<String> addCookie = null;
1022+
10161023
while (names.hasMoreElements()) {
10171024
name = names.nextElement();
10181025
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+
}
10201036
}
10211037
}
10221038

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);
10291049
}
1030-
c.addAll(cookie);
1031-
headers.put(COOKIE, c);
1050+
}
1051+
1052+
if (setCookie != null) { //允许传空的 Cookie && setCookie.isEmpty() == false) {
1053+
headers.put(COOKIE, setCookie);
10321054
}
10331055
}
10341056

@@ -1069,7 +1091,7 @@ public String delegate(
10691091

10701092
HttpHeaders hs = entity.getHeaders();
10711093
if (session != null && hs != null) {
1072-
List<String> cookie = hs.get("Set-Cookie");
1094+
List<String> cookie = hs.get(SET_COOKIE);
10731095
if (cookie != null && cookie.isEmpty() == false) {
10741096
session.setAttribute(COOKIE, cookie);
10751097
}
@@ -1143,8 +1165,8 @@ public String swaggerAPIDocs() {
11431165
" }\n"+
11441166
"}";
11451167
}
1146-
1147-
1168+
1169+
11481170

11491171

11501172
@PostMapping("method/invoke")
@@ -1158,13 +1180,13 @@ public JSONObject invokeMethod(@RequestBody String request) {
11581180
req,
11591181
DemoApplication.getApplicationContext().getBean(
11601182
Class.forName(pkgName.replaceAll("/", ".") + "." + clsName)
1161-
)
1162-
);
1183+
)
1184+
);
11631185
}
11641186
} catch (Exception e) {
11651187
Log.e(TAG, "listMethod try { JSONObject req = JSON.parseObject(request); ... } catch (Exception e) { \n" + e.getMessage());
11661188
}
1167-
1189+
11681190
return super.invokeMethod(request);
11691191
}
11701192

0 commit comments

Comments
 (0)