yanzili 3 years ago
commit f0a3a95cee

@ -1,38 +1,36 @@
package com.anjiplus.template.gaea.business.filter; package com.anjiplus.template.gaea.business.filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import javax.servlet.*; import javax.servlet.*;
import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
/** /**
* Created by raodeming on 2021/6/24. * Created by raodeming on 2021/6/24.
*/ */
@Configuration @Component
@WebFilter(filterName = "CorsFilter ")
@Order(Ordered.HIGHEST_PRECEDENCE) @Order(Ordered.HIGHEST_PRECEDENCE)
public class CORSFilter implements Filter { public class CORSFilter implements Filter {
@Override @Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException { throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response; HttpServletResponse res = (HttpServletResponse) response;
// 设置允许Cookie // 设置允许Cookie
res.addHeader("Access-Control-Allow-Credentials", "true"); res.addHeader("Access-Control-Allow-Credentials", "true");
// 允许http://www.xxx.com域自行设置这里只做示例发起跨域请求 // 允许http://www.xxx.com域自行设置这里只做示例发起跨域请求
res.addHeader("Access-Control-Allow-Origin", "*"); res.addHeader("Access-Control-Allow-Origin", req.getHeader("Origin"));
// 设置允许跨域请求的方法 // 设置允许跨域请求的方法
res.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT"); res.addHeader("Access-Control-Allow-Methods", "*");
// 允许跨域请求包含content-type // 允许跨域请求包含content-type
res.addHeader("Access-Control-Allow-Headers", "*"); res.addHeader("Access-Control-Allow-Headers", "*");
// if (((HttpServletRequest) request).getMethod().equals("OPTIONS")) { res.addHeader("Access-Control-Expose-Headers", "*");
// response.getWriter().println("ok"); chain.doFilter(req, res);
// }
chain.doFilter(request, response);
} }
@Override @Override

@ -10,7 +10,6 @@ const service = axios.create({
timeout: 60000, timeout: 60000,
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Authorization': getItem('token')
} }
}) })

@ -12,9 +12,7 @@ const service = axios.create({
// request拦截器 // request拦截器
service.interceptors.request.use( service.interceptors.request.use(
config => { config => {
if (store.getters.token) { config.headers['Authorization'] = (getToken() == null || getToken() == undefined) ? '': getToken()
config.headers['X-Token'] = getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
}
return config return config
}, },
error => { error => {

Loading…
Cancel
Save