Zuul CORS过滤器不起作用 [英] Zuul CORS filter doesn't work

查看:110
本文介绍了Zuul CORS过滤器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,基本上,前端使用reactJS,后端使用Spring Boot(微服务,以zuul作为网关),当然,如果没有CORS的东西,它们将无法做任何事情.
我们使用了这段代码

So, basically, the front-end uses reactJS, the back-end Spring Boot (microservices, with zuul acting as the gateway), and of course they can't do anything without CORS stuff.
We used this piece of code

@Bean
public CorsFilter corsFilter() {
    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    final CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(true);
    config.addAllowedOrigin("*");
    config.addAllowedHeader("*");
    config.addAllowedMethod("OPTIONS");
    config.addAllowedMethod("HEAD");
    config.addAllowedMethod("GET");
    config.addAllowedMethod("PUT");
    config.addAllowedMethod("POST");
    config.addAllowedMethod("DELETE");
    config.addAllowedMethod("PATCH");
    source.registerCorsConfiguration("/**", config);
    return new CorsFilter(source);
}

并且有效,但是不再有效.我正在使用邮递员检查发送回的标头,而这些标头不存在.
我应该怎么办?我可以在每个微服务中使用一个简单的CORS过滤器,但是让Zuul处理所有事情会更容易.由于所有请求都将通过它.
有什么建议么?拜托,我非常绝望.

and it worked, but it doesn't work anymore. I'm using Postman to check the headers that are sent back, and these are just not there.
What should I do? I could use a simple CORS filter in every microservice, but it would be way easier to let Zuul handle it all. Since all the requests are going to go through it anyway.
Any suggestions? Please, I'm extremely desperate.

推荐答案

尝试一下

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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class SimpleCORSFilter implements Filter {

private final Logger logger = LoggerFactory.getLogger(SimpleCORSFilter.class);

public SimpleCORSFilter() {
    logger.info("SimpleCORSFilter init");
}

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
        throws IOException, ServletException {

    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;

    response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
    response.setHeader("Access-Control-Allow-Credentials", "true");
    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, HEAD, PATCH, PUT");
    response.setHeader("Access-Control-Max-Age", "3600");
    response.setHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With, remember-me");

    chain.doFilter(req, res);
}

@Override
public void init(FilterConfig filterConfig) {
}

@Override
public void destroy() {
}

}

这篇关于Zuul CORS过滤器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆