From b6c312835b25e398cc0792230f8db2844a3372e2 Mon Sep 17 00:00:00 2001 From: wenc000 <450292408@qq.com> Date: Thu, 12 Mar 2020 18:33:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=B7=A8=E5=9F=9F=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cm/common/wechat/config/WechatFilterConfig.java | 5 +++++ .../com/cm/common/wechat/filter/WechatFilter.java | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/cloud-common-wechat/src/main/java/com/cm/common/wechat/config/WechatFilterConfig.java b/cloud-common-wechat/src/main/java/com/cm/common/wechat/config/WechatFilterConfig.java index ca3654a..657dc49 100644 --- a/cloud-common-wechat/src/main/java/com/cm/common/wechat/config/WechatFilterConfig.java +++ b/cloud-common-wechat/src/main/java/com/cm/common/wechat/config/WechatFilterConfig.java @@ -3,6 +3,7 @@ package com.cm.common.wechat.config; import com.cm.common.wechat.config.pojo.WechatOfficialAccountProperties; import com.cm.common.wechat.filter.WechatFilter; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -20,6 +21,9 @@ import org.springframework.context.annotation.Configuration; @Configuration public class WechatFilterConfig { + @Value(value = "${open-platform.wechat.apiCrossOrigin}") + private Boolean apiCrossOrigin; + @Autowired private WechatOfficialAccountProperties wechatOfficialAccountProperties; @@ -27,6 +31,7 @@ public class WechatFilterConfig { public FilterRegistrationBean wechatFilterRegister() { WechatFilter wechatFilter = new WechatFilter(); wechatFilter.setWechatOfficialAccountProperties(wechatOfficialAccountProperties); + wechatFilter.setApiCrossOrigin(apiCrossOrigin); FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); filterRegistrationBean.setFilter(wechatFilter); diff --git a/cloud-common-wechat/src/main/java/com/cm/common/wechat/filter/WechatFilter.java b/cloud-common-wechat/src/main/java/com/cm/common/wechat/filter/WechatFilter.java index 86079cf..553f184 100644 --- a/cloud-common-wechat/src/main/java/com/cm/common/wechat/filter/WechatFilter.java +++ b/cloud-common-wechat/src/main/java/com/cm/common/wechat/filter/WechatFilter.java @@ -41,6 +41,7 @@ public class WechatFilter implements Filter { private static final Logger LOG = LoggerFactory.getLogger(WechatFilter.class); private WechatOfficialAccountProperties wechatOfficialAccountProperties; + private Boolean apiCrossOrigin; private AntPathMatcher antPathMatcher; private static final String WECHAT_LOGIN_URL = "/**/wechat/sign/login"; @@ -74,6 +75,13 @@ public class WechatFilter implements Filter { public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; + + if (apiCrossOrigin != null && apiCrossOrigin) { + response.setHeader("Access-Control-Allow-Origin", "*"); + response.setHeader("Access-Control-Max-Age", "3600"); + response.setHeader("Access-Control-Allow-Credentials", "true"); + } + String requestUri = request.getRequestURI(); boolean isRelease = antPathMatcher.match(WECHAT_LOGIN_URL, requestUri) || antPathMatcher.match(WECHAT_RELEASE_URL, requestUri) @@ -209,6 +217,10 @@ public class WechatFilter implements Filter { this.wechatOfficialAccountProperties = wechatOfficialAccountProperties; } + public void setApiCrossOrigin(Boolean apiCrossOrigin) { + this.apiCrossOrigin = apiCrossOrigin; + } + /** * 错误返回 *