From 0f08c7e8c75ce2b04719b1ed50590697af7bb5e6 Mon Sep 17 00:00:00 2001 From: Raod <1130305001@qq.com> Date: Fri, 25 Jun 2021 10:46:12 +0800 Subject: [PATCH 1/6] cors --- .../anjiplus/template/gaea/business/filter/CORSFilter.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/filter/CORSFilter.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/filter/CORSFilter.java index 90e4dd67..b705990e 100644 --- a/report-core/src/main/java/com/anjiplus/template/gaea/business/filter/CORSFilter.java +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/filter/CORSFilter.java @@ -3,6 +3,7 @@ package com.anjiplus.template.gaea.business.filter; import org.springframework.context.annotation.Configuration; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; +import org.springframework.http.server.reactive.ServerHttpRequest; import javax.servlet.*; import javax.servlet.annotation.WebFilter; @@ -20,13 +21,14 @@ public class CORSFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + ServerHttpRequest req = (ServerHttpRequest) request; HttpServletResponse res = (HttpServletResponse) response; // 设置允许Cookie res.addHeader("Access-Control-Allow-Credentials", "true"); // 允许http://www.xxx.com域(自行设置,这里只做示例)发起跨域请求 - res.addHeader("Access-Control-Allow-Origin", "*"); + res.addHeader("Access-Control-Allow-Origin", req.getHeaders().getOrigin()); // 设置允许跨域请求的方法 - res.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT"); + res.addHeader("Access-Control-Allow-Methods", "*"); // 允许跨域请求包含content-type res.addHeader("Access-Control-Allow-Headers", "*"); // if (((HttpServletRequest) request).getMethod().equals("OPTIONS")) { From 23741e36a24832ed1012b14f93c903fa3dabdf5d Mon Sep 17 00:00:00 2001 From: Raod <1130305001@qq.com> Date: Fri, 25 Jun 2021 10:54:37 +0800 Subject: [PATCH 2/6] cors --- .../template/gaea/business/filter/CORSFilter.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/filter/CORSFilter.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/filter/CORSFilter.java index b705990e..4e7972a4 100644 --- a/report-core/src/main/java/com/anjiplus/template/gaea/business/filter/CORSFilter.java +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/filter/CORSFilter.java @@ -3,10 +3,10 @@ package com.anjiplus.template.gaea.business.filter; import org.springframework.context.annotation.Configuration; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; -import org.springframework.http.server.reactive.ServerHttpRequest; import javax.servlet.*; import javax.servlet.annotation.WebFilter; +import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @@ -21,19 +21,17 @@ public class CORSFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { - ServerHttpRequest req = (ServerHttpRequest) request; + HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; // 设置允许Cookie res.addHeader("Access-Control-Allow-Credentials", "true"); // 允许http://www.xxx.com域(自行设置,这里只做示例)发起跨域请求 - res.addHeader("Access-Control-Allow-Origin", req.getHeaders().getOrigin()); + res.addHeader("Access-Control-Allow-Origin", req.getHeader("Origin")); // 设置允许跨域请求的方法 res.addHeader("Access-Control-Allow-Methods", "*"); // 允许跨域请求包含content-type res.addHeader("Access-Control-Allow-Headers", "*"); -// if (((HttpServletRequest) request).getMethod().equals("OPTIONS")) { -// response.getWriter().println("ok"); -// } + res.addHeader("Access-Control-Expose-Headers", "*"); chain.doFilter(request, response); } From 458bb47d179f0332f277386d3365ee81133e76d9 Mon Sep 17 00:00:00 2001 From: Raod <1130305001@qq.com> Date: Fri, 25 Jun 2021 11:00:37 +0800 Subject: [PATCH 3/6] cors --- .../com/anjiplus/template/gaea/business/filter/CORSFilter.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/filter/CORSFilter.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/filter/CORSFilter.java index 4e7972a4..739c8814 100644 --- a/report-core/src/main/java/com/anjiplus/template/gaea/business/filter/CORSFilter.java +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/filter/CORSFilter.java @@ -15,7 +15,6 @@ import java.io.IOException; */ @Configuration @WebFilter(filterName = "CorsFilter ") -@Order(Ordered.HIGHEST_PRECEDENCE) public class CORSFilter implements Filter { @Override @@ -32,7 +31,7 @@ public class CORSFilter implements Filter { // 允许跨域请求包含content-type res.addHeader("Access-Control-Allow-Headers", "*"); res.addHeader("Access-Control-Expose-Headers", "*"); - chain.doFilter(request, response); + chain.doFilter(req, res); } @Override From 2db54e58bc07dc351c6532dcf00500496a605402 Mon Sep 17 00:00:00 2001 From: Raod <1130305001@qq.com> Date: Fri, 25 Jun 2021 11:04:03 +0800 Subject: [PATCH 4/6] cors --- .../template/gaea/business/filter/CORSFilter.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/filter/CORSFilter.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/filter/CORSFilter.java index 739c8814..a1618e01 100644 --- a/report-core/src/main/java/com/anjiplus/template/gaea/business/filter/CORSFilter.java +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/filter/CORSFilter.java @@ -1,11 +1,8 @@ package com.anjiplus.template.gaea.business.filter; -import org.springframework.context.annotation.Configuration; -import org.springframework.core.Ordered; -import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; import javax.servlet.*; -import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @@ -13,8 +10,7 @@ import java.io.IOException; /** * Created by raodeming on 2021/6/24. */ -@Configuration -@WebFilter(filterName = "CorsFilter ") +@Component public class CORSFilter implements Filter { @Override From e5cb74a540c6c9ef73c1eda68be83b6771db82cf Mon Sep 17 00:00:00 2001 From: Raod <1130305001@qq.com> Date: Fri, 25 Jun 2021 11:07:37 +0800 Subject: [PATCH 5/6] cors --- .../com/anjiplus/template/gaea/business/filter/CORSFilter.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/filter/CORSFilter.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/filter/CORSFilter.java index a1618e01..e5eea41f 100644 --- a/report-core/src/main/java/com/anjiplus/template/gaea/business/filter/CORSFilter.java +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/filter/CORSFilter.java @@ -1,5 +1,7 @@ package com.anjiplus.template.gaea.business.filter; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import javax.servlet.*; @@ -11,6 +13,7 @@ import java.io.IOException; * Created by raodeming on 2021/6/24. */ @Component +@Order(Ordered.HIGHEST_PRECEDENCE) public class CORSFilter implements Filter { @Override From ecfa4a310b82a9a5e9d91e34cb22c3248f978675 Mon Sep 17 00:00:00 2001 From: Raod <1130305001@qq.com> Date: Fri, 25 Jun 2021 13:11:17 +0800 Subject: [PATCH 6/6] cors --- report-ui/src/api/axios.js | 1 - report-ui/src/utils/request.js | 4 +--- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/report-ui/src/api/axios.js b/report-ui/src/api/axios.js index 458975f3..93561583 100644 --- a/report-ui/src/api/axios.js +++ b/report-ui/src/api/axios.js @@ -10,7 +10,6 @@ const service = axios.create({ timeout: 60000, headers: { 'Content-Type': 'application/json', - 'Authorization': getItem('token') } }) diff --git a/report-ui/src/utils/request.js b/report-ui/src/utils/request.js index 7744512e..cd85d485 100644 --- a/report-ui/src/utils/request.js +++ b/report-ui/src/utils/request.js @@ -12,9 +12,7 @@ const service = axios.create({ // request拦截器 service.interceptors.request.use( config => { - if (store.getters.token) { - config.headers['X-Token'] = getToken() // 让每个请求携带自定义token 请根据实际情况自行修改 - } + config.headers['Authorization'] = (getToken() == null || getToken() == undefined) ? '': getToken() return config }, error => {