@@ -968,6 +968,7 @@ public JSONObject putBalance(@RequestBody String request, HttpSession session) {
968968 }
969969
970970
971+ public static final String COOKIE = "Cookie" ;
971972 public static final List <String > EXCEPT_HEADER_LIST ;
972973 static {
973974 EXCEPT_HEADER_LIST = Arrays .asList ( //accept-encoding 在某些情况下导致乱码,origin 和 sec-fetch-mode 等 CORS 信息导致服务器代理失败
@@ -981,29 +982,47 @@ public JSONObject putBalance(@RequestBody String request, HttpSession session) {
981982 @ Autowired
982983 HttpServletResponse response ;
983984
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+ */
984993 @ 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+
9861001 Enumeration <String > names = request .getHeaderNames ();
9871002 HttpHeaders headers = null ;
9881003 String name ;
9891004 if (names != null ) {
9901005 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+
9911010 while (names .hasMoreElements ()) {
9921011 name = names .nextElement ();
993- if (name != null && EXCEPT_HEADER_LIST .contains (name .toLowerCase ()) == false ) {
1012+ if (name != null && exceptHeaderList .contains (name .toLowerCase ()) == false ) {
9941013 headers .add (name , request .getHeader (name ));
9951014 }
9961015 }
9971016
9981017 @ 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 );
10001019 if (cookie != null && cookie .isEmpty () == false ) {
1001- List <String > c = headers .get ("Cookie" );
1020+ List <String > c = headers .get (COOKIE );
10021021 if (c == null ) {
10031022 c = new ArrayList <>();
10041023 }
10051024 c .addAll (cookie );
1006- headers .put ("Cookie" , c );
1025+ headers .put (COOKIE , c );
10071026 }
10081027 }
10091028 try {
@@ -1022,7 +1041,7 @@ public String delegate(@RequestParam("$_delegate_url") String url, @RequestBody
10221041 if (session != null && hs != null ) {
10231042 List <String > cookie = hs .get ("Set-Cookie" );
10241043 if (cookie != null && cookie .isEmpty () == false ) {
1025- session .setAttribute ("Cookie" , cookie );
1044+ session .setAttribute (COOKIE , cookie );
10261045 }
10271046 }
10281047 return entity .getBody ();
0 commit comments