| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 55eed59 commit 1b47ee7
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -968,6 +968,7 @@ public JSONObject putBalance(@RequestBody String request, HttpSession session) { | |||
| 968 | 968 | } | |
| 969 | 969 | ||
| 970 | 970 | ||
| 971 | + public static final String COOKIE = "Cookie"; | ||
| 971 | 972 | public static final List<String> EXCEPT_HEADER_LIST; | |
| 972 | 973 | static { | |
| 973 | 974 | EXCEPT_HEADER_LIST = Arrays.asList( //accept-encoding 在某些情况下导致乱码,origin 和 sec-fetch-mode 等 CORS 信息导致服务器代理失败 | |
@@ -981,29 +982,47 @@ public JSONObject putBalance(@RequestBody String request, HttpSession session) { | |||
| 981 | 982 | @Autowired | |
| 982 | 983 | HttpServletResponse response; | |
| 983 | 984 | ||
| 985 | + /**代理接口,解决前端(APIAuto等)跨域问题 | ||
| 986 | + * @param exceptHeaders 排除请求头,必须放在最前面,放后面可能被当成 $_delegate_url 的一部分 | ||
| 987 | + * @param url 被代理的 url | ||
| 988 | + * @param body POST Body | ||
| 989 | + * @param method HTTP Method | ||
| 990 | + * @param session HTTP session | ||
| 991 | + * @return | ||
| 992 | + */ | ||
| 984 | 993 | @RequestMapping(value = "/delegate") | |
| 985 | - public String delegate(@RequestParam("$_delegate_url") String url, @RequestBody String body, HttpMethod method, HttpSession session){ | ||
| 994 | + public String delegate( | ||
| 995 | + @RequestParam(value = "$_except_headers", required = false) String exceptHeaders, | ||
| 996 | + @RequestParam("$_delegate_url") String url, | ||
| 997 | + @RequestBody(required = false) String body, | ||
| 998 | + HttpMethod method, HttpSession session | ||
| 999 | + ) { | ||
| 1000 | + | ||
| 986 | 1001 | Enumeration<String> names = request.getHeaderNames(); | |
| 987 | 1002 | HttpHeaders headers = null; | |
| 988 | 1003 | String name; | |
| 989 | 1004 | if (names != null) { | |
| 990 | 1005 | headers = new HttpHeaders(); | |
| 1006 | + //Arrays.asList(null) 抛异常,可以排除不存在的头来替代 exceptHeaders == null //空字符串表示不排除任何头 | ||
| 1007 | + List<String> exceptHeaderList = StringUtil.isEmpty(exceptHeaders, true) | ||
| 1008 | + ? EXCEPT_HEADER_LIST : Arrays.asList(StringUtil.split(exceptHeaders)); | ||
| 1009 | + | ||
| 991 | 1010 | while (names.hasMoreElements()) { | |
| 992 | 1011 | name = names.nextElement(); | |
| 993 | - if (name != null && EXCEPT_HEADER_LIST.contains(name.toLowerCase()) == false) { | ||
| 1012 | + if (name != null && exceptHeaderList.contains(name.toLowerCase()) == false) { | ||
| 994 | 1013 | headers.add(name, request.getHeader(name)); | |
| 995 | 1014 | } | |
| 996 | 1015 | } | |
| 997 | 1016 | ||
| 998 | 1017 | @SuppressWarnings("unchecked") | |
| 999 | - List<String> cookie = session == null ? null : (List<String>) session.getAttribute("Cookie"); | ||
| 1018 | + List<String> cookie = session == null ? null : (List<String>) session.getAttribute(COOKIE); | ||
| 1000 | 1019 | if (cookie != null && cookie.isEmpty() == false) { | |
| 1001 | - List<String> c = headers.get("Cookie"); | ||
| 1020 | + List<String> c = headers.get(COOKIE); | ||
| 1002 | 1021 | if (c == null) { | |
| 1003 | 1022 | c = new ArrayList<>(); | |
| 1004 | 1023 | } | |
| 1005 | 1024 | c.addAll(cookie); | |
| 1006 | - headers.put("Cookie", c); | ||
| 1025 | + headers.put(COOKIE, c); | ||
| 1007 | 1026 | } | |
| 1008 | 1027 | } | |
| 1009 | 1028 | try { | |
@@ -1022,7 +1041,7 @@ public String delegate(@RequestParam("$_delegate_url") String url, @RequestBody | |||
| 1022 | 1041 | if (session != null && hs != null) { | |
| 1023 | 1042 | List<String> cookie = hs.get("Set-Cookie"); | |
| 1024 | 1043 | if (cookie != null && cookie.isEmpty() == false) { | |
| 1025 | - session.setAttribute("Cookie", cookie); | ||
| 1044 | + session.setAttribute(COOKIE, cookie); | ||
| 1026 | 1045 | } | |
| 1027 | 1046 | } | |
| 1028 | 1047 | return entity.getBody(); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments