Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc 弹簧靴,角度2,交叉原点_Spring Mvc_Angular_Cross Domain - Fatal编程技术网

Spring mvc 弹簧靴,角度2,交叉原点

Spring mvc 弹簧靴,角度2,交叉原点,spring-mvc,angular,cross-domain,Spring Mvc,Angular,Cross Domain,我在Spring Boot中有一个web服务器。我可以向它发送HTTP请求,并从诸如SendHTTP之类的工具获取答案。我也有一个基于Angular2的网站,whoich也能工作。部分功能必须包括从Chrome中运行的Angular2到Spring引导服务器的HTTP请求。每次我这样做时,Chrome控制台中都会出现一个错误: XMLHttpRequest无法加载“URL服务器”。请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许访问源“URL客户端”

我在Spring Boot中有一个web服务器。我可以向它发送HTTP请求,并从诸如SendHTTP之类的工具获取答案。我也有一个基于Angular2的网站,whoich也能工作。部分功能必须包括从Chrome中运行的Angular2到Spring引导服务器的HTTP请求。每次我这样做时,Chrome控制台中都会出现一个错误:

XMLHttpRequest无法加载“URL服务器”。请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许访问源“URL客户端”

我将@CrossOrigin放在所有有意义的地方:在@Controller、@RequestMapping、@GetMapping之前,但它没有帮助。我错过了什么,如何克服? 非常感谢,有点绝望,Levi

试试下面的方法:

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("<<MAPPING>>").allowedOrigins("<<ORIGINS>>");
            }
        };
    }

您需要为应用程序添加CQRSFilter,并在spring引导配置类中设置该CORSFilter

你要做的是

1> 创建您自己的CQRSFilter,以实现过滤器

2> 您应该在过滤器中添加如下代码

@Component
public class CORSFilter implements Filter{
static Logger logger = LoggerFactory.getLogger(CORSFilter.class);

@Override
public void init(FilterConfig filterConfig) throws ServletException {
}

@Override
public void doFilter(ServletRequest request, ServletResponse res, FilterChain chain) throws IOException, ServletException {
    HttpServletResponse response = (HttpServletResponse) res;
    response.setHeader("Access-Control-Allow-Origin", "*");
    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");
    chain.doFilter(request, response);
}

public void destroy() {}
}
3> 将过滤器放在spring配置文件中

@EnableWebMvcSecurity
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {



@Override
protected void configure(HttpSecurity http) throws Exception {
  http
      .addFilterBefore(new CORSFilter(), ChannelProcessingFilter.class)

  }
 }

试试这个它对我有用

您需要按照所述设置后端代理

1在package.json旁边创建proxy.conf.json文件,其中包含

{
  "/api": {
    "target": "http://localhost:8080",
    "secure": false
  }
}
2编辑package.json中的启动脚本

"start": "ng serve --proxy-config proxy.conf.json"
3与

npm start

我尝试了手册中的所有内容以及您的建议,但仍然存在问题。以下操作成功:导入org.springframework.web.cors.corscoConfiguration;//+更多公共类RestConfiguration{@Bean公共CorsFilter CorsFilter{UrlBasedCorsConfigurationSource=新的UrlBasedCorsConfigurationSource;CorsConfiguration配置=新的CorsConfiguration;config.setAllowCredentialstrue;config.addAllowedOrigin*;config.addAllowedHeader*;config.addAllowedMethod*;source.registerCorsConfiguration/**,config;返回新的CorsFiltersource;}}您的答案似乎很有希望,因为缺少的是标题。谢谢,但您可以添加一些详细信息吗?我不明白在哪里写响应。setHeaderAccess-Control-Allow-Origin,http://localhost:8080;这也是spring配置文件的意思,抱歉这么密集。我添加了一些相关代码。您需要添加自己的代码如果需要,请提供其他配置。如果您觉得有帮助,请接受:
"start": "ng serve --proxy-config proxy.conf.json"
npm start