|
|
|
@ -1,38 +1,36 @@
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Created by raodeming on 2021/6/24.
|
|
|
|
|
*/
|
|
|
|
|
@Configuration
|
|
|
|
|
@WebFilter(filterName = "CorsFilter ")
|
|
|
|
|
@Component
|
|
|
|
|
@Order(Ordered.HIGHEST_PRECEDENCE)
|
|
|
|
|
public class CORSFilter implements Filter {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
|
|
|
|
|
throws IOException, ServletException {
|
|
|
|
|
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", "*");
|
|
|
|
|
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
|
|
|
|
|
res.addHeader("Access-Control-Allow-Headers", "*");
|
|
|
|
|
// if (((HttpServletRequest) request).getMethod().equals("OPTIONS")) {
|
|
|
|
|
// response.getWriter().println("ok");
|
|
|
|
|
// }
|
|
|
|
|
chain.doFilter(request, response);
|
|
|
|
|
res.addHeader("Access-Control-Expose-Headers", "*");
|
|
|
|
|
chain.doFilter(req, res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|