Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9771c80

Browse files
Java:APIJSONBoot 和 MultiDataSource 代理接口解决前端有时发送不了 Cookie 导致登录鉴权失败等问题
1 parent 6ca8725 commit 9771c80

File tree

5 files changed

+85
-7
lines changed

5 files changed

+85
-7
lines changed

‎APIJSON-Java-Server/APIJSONBoot-MultiDataSource/pom.xml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
<modelVersion>4.0.0</modelVersion>
55

66
<groupId>apijson.boot</groupId>
7-
<artifactId>apijson-boot-multi-datasource</artifactId>
8-
<version>4.7.2</version>
9-
<packaging>jar</packaging>
7+
<artifactId>apijson-boot</artifactId>
8+
<version>4.8.0</version>
9+
<!-- <packaging>jar</packaging> -->
1010

1111
<name>APIJSONBoot-MultiDataSource</name>
1212
<description>Demo project for APIJSON Server based on SpringBoot</description>

‎APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/boot/DemoApplication.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import javax.naming.Context;
2323

24-
import apijson.framework.APIJSONParser;
2524
import org.springframework.beans.BeansException;
2625
import org.springframework.boot.SpringApplication;
2726
import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -48,6 +47,7 @@
4847
import apijson.demo.DemoVerifier;
4948
import apijson.framework.APIJSONApplication;
5049
import apijson.framework.APIJSONCreator;
50+
import apijson.framework.APIJSONParser;
5151
import apijson.orm.AbstractVerifier;
5252
import apijson.orm.FunctionParser;
5353
import apijson.orm.Parser;
@@ -278,6 +278,7 @@ public void addCorsMappings(CorsRegistry registry) {
278278
.allowedOriginPatterns("*")
279279
.allowedMethods("*")
280280
.allowCredentials(true)
281+
.exposedHeaders(DemoController.APIJSON_DELEGATE_ID) // Cookie 和 Set-Cookie 怎么设置都没用 ,Cookie,Set-Cookie") // .exposedHeaders("*")
281282
.maxAge(3600);
282283
}
283284
};

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import org.springframework.web.client.RestTemplate;
6666

6767
import com.alibaba.fastjson.JSONObject;
68+
import com.fasterxml.jackson.databind.util.LRUMap;
6869

6970
import apijson.JSON;
7071
import 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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import javax.naming.Context;
2323

24-
import apijson.framework.APIJSONParser;
2524
import org.springframework.beans.BeansException;
2625
import org.springframework.boot.SpringApplication;
2726
import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -48,6 +47,7 @@
4847
import apijson.demo.DemoVerifier;
4948
import apijson.framework.APIJSONApplication;
5049
import apijson.framework.APIJSONCreator;
50+
import apijson.framework.APIJSONParser;
5151
import apijson.orm.AbstractVerifier;
5252
import apijson.orm.FunctionParser;
5353
import apijson.orm.Parser;
@@ -278,6 +278,7 @@ public void addCorsMappings(CorsRegistry registry) {
278278
.allowedOriginPatterns("*")
279279
.allowedMethods("*")
280280
.allowCredentials(true)
281+
.exposedHeaders(DemoController.APIJSON_DELEGATE_ID) // Cookie 和 Set-Cookie 怎么设置都没用 ,Cookie,Set-Cookie") // .exposedHeaders("*")
281282
.maxAge(3600);
282283
}
283284
};

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import org.springframework.web.client.RestTemplate;
6666

6767
import com.alibaba.fastjson.JSONObject;
68+
import com.fasterxml.jackson.databind.util.LRUMap;
6869

6970
import apijson.JSON;
7071
import 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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /