标签:options 问题 nts spring tor frame location user str
1、服务器端解决跨域问题:
配置filter在filter中设置请求头
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Component
public class AccessControlFilter implements Filter {
public static Logger logger = LoggerFactory.getLogger(AccessControlFilter.class);
public static final String CLIENT_CONFIG_FILE = "accessAllow.properties";
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*"); // 没部署nginx,线上环境不好使,暂时注释掉
response.setHeader("Access-Control-Allow-Methods",
"POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with,content-type");
response.setHeader("Access-Control-Allow-Credentials", "true");
chain.doFilter(req, res);
}
@Override
public void destroy() {
}
}
2、利用nginx反向代理解决跨域问题:
在nginx.conf配置中在http节点内添加
server {
listen 8888; #监听端口
server_name 172.20.15.81; #监听ip
location /maservlet/ {
add_header ‘Access-Control-Allow-Origin‘ ‘*‘;
add_header ‘Access-Control-Allow-Credentials‘ ‘true‘;
add_header ‘Access-Control-Allow-Methods‘ ‘GET, POST, OPTIONS‘;
add_header ‘Access-Control-Allow-Headers‘ ‘DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type‘;
proxy_pass http://172.20.15.81:9080; #跳转到路径
}
}
标签:options 问题 nts spring tor frame location user str
原文地址:http://www.cnblogs.com/xucheng/p/6024062.html