Skip to content

Commit d496668

Browse files
committed
Server:新增代理接口,支持前端任意跨域
1 parent 6b62775 commit d496668

File tree

1 file changed

+82
-10
lines changed
  • APIJSON-Java-Server/APIJSONBoot/src/main/java/apijson/demo/server

1 file changed

+82
-10
lines changed

APIJSON-Java-Server/APIJSONBoot/src/main/java/apijson/demo/server/Controller.java

Lines changed: 82 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,33 @@
2424

2525
import java.net.URLDecoder;
2626
import java.rmi.ServerException;
27+
import java.util.ArrayList;
28+
import java.util.Enumeration;
29+
import java.util.List;
2730
import java.util.Random;
2831
import java.util.concurrent.TimeoutException;
2932

33+
import javax.servlet.http.HttpServletRequest;
34+
import javax.servlet.http.HttpServletResponse;
3035
import javax.servlet.http.HttpSession;
3136

37+
import org.springframework.beans.factory.annotation.Autowired;
38+
import org.springframework.http.HttpEntity;
39+
import org.springframework.http.HttpHeaders;
40+
import org.springframework.http.HttpMethod;
41+
import org.springframework.http.ResponseEntity;
42+
import org.springframework.stereotype.Service;
43+
import org.springframework.web.bind.annotation.GetMapping;
3244
import org.springframework.web.bind.annotation.PathVariable;
3345
import org.springframework.web.bind.annotation.PostMapping;
3446
import org.springframework.web.bind.annotation.RequestBody;
3547
import org.springframework.web.bind.annotation.RequestMapping;
48+
import org.springframework.web.bind.annotation.RequestParam;
49+
import org.springframework.web.bind.annotation.ResponseBody;
3650
import org.springframework.web.bind.annotation.RestController;
51+
import org.springframework.web.client.RestTemplate;
3752

53+
import com.alibaba.fastjson.JSONArray;
3854
import com.alibaba.fastjson.JSONObject;
3955

4056
import apijson.demo.server.model.BaseModel;
@@ -63,6 +79,7 @@
6379
* <br > 3.调试方便 - 建议使用 APIJSON在线测试工具 或 Postman
6480
* @author Lemon
6581
*/
82+
@Service
6683
@RestController
6784
@RequestMapping("")
6885
public class Controller {
@@ -200,15 +217,15 @@ public String openHead(@PathVariable String request, HttpSession session) {
200217
public static final String FUNCTION_;
201218
public static final String REQUEST_;
202219
public static final String ACCESS_;
203-
220+
204221
public static final String USER_;
205222
public static final String PRIVACY_;
206223
public static final String VERIFY_; //加下划线后缀是为了避免 Verify 和 verify 都叫VERIFY,分不清
207224
static {
208225
FUNCTION_ = Function.class.getSimpleName();
209226
REQUEST_ = Request.class.getSimpleName();
210227
ACCESS_ = Access.class.getSimpleName();
211-
228+
212229
USER_ = User.class.getSimpleName();
213230
PRIVACY_ = Privacy.class.getSimpleName();
214231
VERIFY_ = Verify.class.getSimpleName();
@@ -233,8 +250,8 @@ public String openHead(@PathVariable String request, HttpSession session) {
233250

234251
public static final String TYPE = "type";
235252

236-
237-
253+
254+
238255
/**重新加载配置
239256
* @param request
240257
* @return
@@ -261,17 +278,17 @@ public JSONObject reload(@RequestBody String request) {
261278
} catch (Exception e) {
262279
return DemoParser.extendErrorResult(requestObject, e);
263280
}
264-
281+
265282
JSONResponse response = new JSONResponse(headVerify(Verify.TYPE_RELOAD, phone, verify));
266283
response = response.getJSONResponse(VERIFY_);
267284
if (JSONResponse.isExist(response) == false) {
268285
return DemoParser.extendErrorResult(requestObject, new ConditionErrorException("手机号或验证码错误"));
269286
}
270287

271288
JSONObject result = DemoParser.newSuccessResult();
272-
289+
273290
boolean reloadAll = StringUtil.isEmpty(type, true) || "ALL".equals(type);
274-
291+
275292
if (reloadAll || "FUNCTION".equals(type)) {
276293
try {
277294
result.put(FUNCTION_, DemoFunction.init());
@@ -280,7 +297,7 @@ public JSONObject reload(@RequestBody String request) {
280297
result.put(FUNCTION_, DemoParser.newErrorResult(e));
281298
}
282299
}
283-
300+
284301
if (reloadAll || "REQUEST".equals(type)) {
285302
try {
286303
result.put(REQUEST_, StructureUtil.init());
@@ -289,7 +306,7 @@ public JSONObject reload(@RequestBody String request) {
289306
result.put(REQUEST_, DemoParser.newErrorResult(e));
290307
}
291308
}
292-
309+
293310
if (reloadAll || "ACCESS".equals(type)) {
294311
try {
295312
result.put(ACCESS_, DemoVerifier.init());
@@ -298,7 +315,7 @@ public JSONObject reload(@RequestBody String request) {
298315
result.put(ACCESS_, DemoParser.newErrorResult(e));
299316
}
300317
}
301-
318+
302319
return result;
303320
}
304321

@@ -952,4 +969,59 @@ public JSONObject putBalance(@RequestBody String request, HttpSession session) {
952969
}
953970

954971

972+
@Autowired
973+
HttpServletRequest request;
974+
@Autowired
975+
HttpServletResponse response;
976+
977+
@RequestMapping(value = "/delegate")
978+
// @ResponseBody
979+
public String delegate(@RequestParam("$_delegate_url") String url, @RequestBody String body, HttpMethod method, HttpSession session){
980+
Enumeration<String> names = request.getHeaderNames();
981+
HttpHeaders headers = null;
982+
String name;
983+
if (names != null) {
984+
headers = new HttpHeaders();
985+
while (names.hasMoreElements()) {
986+
name = names.nextElement();
987+
headers.add(name, request.getHeader(name));
988+
}
989+
990+
List<String> cookie = session == null ? null : (List<String>) session.getAttribute("Cookie");
991+
if (cookie != null && cookie.isEmpty() == false) {
992+
List<String> c = headers.get("Cookie");
993+
if (c == null) {
994+
c = new ArrayList<>();
995+
}
996+
c.addAll(cookie);
997+
headers.put("Cookie", c);
998+
}
999+
}
1000+
try {
1001+
request.getParameterMap().remove("$_delegate_url");
1002+
} catch (Exception e) {
1003+
// TODO: handle exception
1004+
}
1005+
1006+
RestTemplate client = new RestTemplate();
1007+
// 请勿轻易改变此提交方式,大部分的情况下,提交方式都是表单提交
1008+
HttpEntity<String> requestEntity = new HttpEntity<>(method == HttpMethod.GET ? JSON.toJSONString(request.getParameterMap()) : body, headers);
1009+
// 执行HTTP请求
1010+
ResponseEntity<String> entity = client.exchange(url, method, requestEntity, String.class);
1011+
1012+
HttpHeaders hs = entity.getHeaders();
1013+
if (session != null && hs != null) {
1014+
List<String> cookie = hs.get("Set-Cookie");
1015+
if (cookie != null && cookie.isEmpty() == false) {
1016+
session.setAttribute("Cookie", cookie);
1017+
}
1018+
}
1019+
return entity.getBody();
1020+
}
1021+
1022+
@GetMapping("v2/api-docs")
1023+
public JSONObject swaggerAPIDocs() {
1024+
return new DemoParser().parseResponse(new JSONRequest("Swagger", new JSONRequest())).getJSONObject("Swagger");
1025+
}
1026+
9551027
}

0 commit comments

Comments
 (0)