Quellcode durchsuchen

增加Cookie、Header设置

mxd vor 4 Jahren
Ursprung
Commit
332d49f44b

+ 2 - 0
src/main/java/org/ssssssss/magicapi/config/WebUIController.java

@@ -11,6 +11,7 @@ import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.context.request.RequestContextHolder;
 import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
 import org.ssssssss.magicapi.functions.DatabaseQuery;
 import org.ssssssss.magicapi.logging.MagicLoggerContext;
@@ -293,6 +294,7 @@ public class WebUIController {
 			try {
 				context.setBreakpoints((List<Integer>) breakpoints);    //设置断点
 				context.setTimeout(this.debugTimeout);    //设置断点超时时间
+				RequestContextHolder.setRequestAttributes(RequestContextHolder.getRequestAttributes(), true);
 				if (sessionId != null) {
 					context.setId(sessionId.toString());
 					context.onComplete(() -> {

+ 108 - 0
src/main/java/org/ssssssss/magicapi/functions/ResponseFunctions.java

@@ -1,13 +1,21 @@
 package org.ssssssss.magicapi.functions;
 
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.http.HttpHeaders;
 import org.springframework.http.MediaType;
 import org.springframework.http.ResponseEntity;
+import org.springframework.web.context.request.RequestAttributes;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
 import org.ssssssss.magicapi.provider.ResultProvider;
+import org.ssssssss.script.functions.ObjectConvertExtension;
 
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletResponse;
 import java.io.UnsupportedEncodingException;
 import java.net.URLEncoder;
 import java.util.List;
+import java.util.Map;
 
 public class ResponseFunctions {
 
@@ -36,6 +44,106 @@ public class ResponseFunctions {
 		return ResponseEntity.ok(value);
 	}
 
+	/**
+	 * 添加Header
+	 */
+	public void addHeader(String key, String value) {
+		if (StringUtils.isNotBlank(key)) {
+			HttpServletResponse response = getResponse();
+			if (response != null) {
+				response.addHeader(key, value);
+			}
+		}
+	}
+
+	/**
+	 * 设置header
+	 */
+	public void setHeader(String key, String value) {
+		if (StringUtils.isNotBlank(key)) {
+			HttpServletResponse response = getResponse();
+			if (response != null) {
+				response.setHeader(key, value);
+			}
+		}
+	}
+
+	/**
+	 * 添加cookie
+	 */
+	public void addCookie(String name, String value) {
+		if (StringUtils.isNotBlank(name)) {
+			addCookie(new Cookie(name, value));
+		}
+	}
+
+	/**
+	 * 批量添加cookie
+	 */
+	public void addCookies(Map<String, String> cookies, Map<String, Object> options) {
+		if (cookies != null) {
+			for (Map.Entry<String, String> entry : cookies.entrySet()) {
+				addCookie(entry.getKey(), entry.getValue(), options);
+			}
+		}
+	}
+
+	/**
+	 * 批量添加cookie
+	 */
+	public void addCookies(Map<String, String> cookies) {
+		addCookies(cookies, null);
+	}
+
+	/**
+	 * 添加cookie
+	 */
+	public void addCookie(String name, String value, Map<String, Object> options) {
+		if (StringUtils.isNotBlank(name)) {
+			Cookie cookie = new Cookie(name, value);
+			if (options != null) {
+				Object path = options.get("path");
+				if (path != null) {
+					cookie.setPath(path.toString());
+				}
+				Object httpOnly = options.get("httpOnly");
+				if (httpOnly != null) {
+					cookie.setHttpOnly("true".equalsIgnoreCase(httpOnly.toString()));
+				}
+				Object domain = options.get("domain");
+				if (domain != null) {
+					cookie.setDomain(domain.toString());
+				}
+				Object maxAge = options.get("maxAge");
+				int age;
+				if (maxAge != null && (age = ObjectConvertExtension.asInt(maxAge, Integer.MIN_VALUE)) != Integer.MIN_VALUE) {
+					cookie.setMaxAge(age);
+				}
+			}
+			addCookie(cookie);
+		}
+	}
+
+	/**
+	 * 添加cookie
+	 */
+	public void addCookie(Cookie cookie) {
+		if (cookie != null) {
+			HttpServletResponse response = getResponse();
+			if (response != null) {
+				response.addCookie(cookie);
+			}
+		}
+	}
+
+	private HttpServletResponse getResponse() {
+		RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
+		if (requestAttributes instanceof ServletRequestAttributes) {
+			return ((ServletRequestAttributes) requestAttributes).getResponse();
+		}
+		return null;
+	}
+
 	/**
 	 * 展示图片
 	 *