Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.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
Javascript CORS阻塞我的后端服务器,如何修复?使用Springboot java作为后端,react js作为前端 import React,{Component}来自“React”; 从“axios”导入axios; 类应用程序扩展组件{ handleSubmit(事件){ 轴心柱http://localhost:3050/login', { “用户名”:“用户名”, “密码”:“密码” }) .然后(功能(响应){ 控制台日志(响应); }) .catch(函数(错误){ console.log(错误); }); event.preventDefault(); } render(){ 返回( ); } } 导出默认应用程序;_Javascript_Java_Reactjs_Spring Boot_Cors - Fatal编程技术网

Javascript CORS阻塞我的后端服务器,如何修复?使用Springboot java作为后端,react js作为前端 import React,{Component}来自“React”; 从“axios”导入axios; 类应用程序扩展组件{ handleSubmit(事件){ 轴心柱http://localhost:3050/login', { “用户名”:“用户名”, “密码”:“密码” }) .然后(功能(响应){ 控制台日志(响应); }) .catch(函数(错误){ console.log(错误); }); event.preventDefault(); } render(){ 返回( ); } } 导出默认应用程序;

Javascript CORS阻塞我的后端服务器,如何修复?使用Springboot java作为后端,react js作为前端 import React,{Component}来自“React”; 从“axios”导入axios; 类应用程序扩展组件{ handleSubmit(事件){ 轴心柱http://localhost:3050/login', { “用户名”:“用户名”, “密码”:“密码” }) .然后(功能(响应){ 控制台日志(响应); }) .catch(函数(错误){ console.log(错误); }); event.preventDefault(); } render(){ 返回( ); } } 导出默认应用程序;,javascript,java,reactjs,spring-boot,cors,Javascript,Java,Reactjs,Spring Boot,Cors,只需将用户名设置为“username”,密码设置为“password”的json检查后端 我的后端是spring boot,使用带有“用户名”和“密码”的结束链接/登录应该会给出一些响应。因此,这段代码可以工作,除了CORS阻止连接,所以它永远无法处理。我发现的一个解决方案是禁用chrome中的所有安全性,它可以正常工作。但我正在寻找一个永久性的解决方案,而不必禁用chrome设置的安全性。不确定我是通过springboot还是react完成的尝试在SpringREST端点上使用注释“@Cros

只需将用户名设置为“username”,密码设置为“password”的json检查后端


我的后端是spring boot,使用带有“用户名”和“密码”的结束链接/登录应该会给出一些响应。因此,这段代码可以工作,除了CORS阻止连接,所以它永远无法处理。我发现的一个解决方案是禁用chrome中的所有安全性,它可以正常工作。但我正在寻找一个永久性的解决方案,而不必禁用chrome设置的安全性。不确定我是通过springboot还是react完成的

尝试在SpringREST端点上使用注释“@CrossOrigin”,在“@RequestMapping”注释上方

例如:-


在请求POST时,您可能需要以下附加配置:

    @CrossOrigin
    @RequestMapping(value="/login",method=RequestMethod.POST)

在u配置中创建这个bean

axio({
  method: 'put',
  headers: {
    'Content-Type': 'application/json',
  },
  data: JSON.stringify({
    "username": "username",
    "password": "password"
  }),
});

请在主spring引导类所在的包中的项目中添加以下文件。这对我在春季开机,反应生态系统的所有CORS问题的工作

 @Bean
public CorsConfigurationSource corsConfigurationSource() {

    final CorsConfiguration configuration = new CorsConfiguration();
    configuration.setAllowedOrigins(ImmutableList.of(
            "http://example.net",
            "http://example.com",
           ));
    configuration.setAllowedMethods(ImmutableList.of("HEAD", "OPTIONS", "GET", "POST", "PUT", "DELETE", "PATCH"));
    configuration.setAllowCredentials(true);
    configuration.setAllowedHeaders(ImmutableList.of("*"));
    configuration.setExposedHeaders(ImmutableList.of("Content-Disposition"));
    configuration.setMaxAge(3600L);

    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/**", configuration);

    return source;
}
答案是从这里开始
 @Bean
public CorsConfigurationSource corsConfigurationSource() {

    final CorsConfiguration configuration = new CorsConfiguration();
    configuration.setAllowedOrigins(ImmutableList.of(
            "http://example.net",
            "http://example.com",
           ));
    configuration.setAllowedMethods(ImmutableList.of("HEAD", "OPTIONS", "GET", "POST", "PUT", "DELETE", "PATCH"));
    configuration.setAllowCredentials(true);
    configuration.setAllowedHeaders(ImmutableList.of("*"));
    configuration.setExposedHeaders(ImmutableList.of("Content-Disposition"));
    configuration.setMaxAge(3600L);

    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/**", configuration);

    return source;
}
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@EnableWebMvc
public class CorsWebConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**");
        registry.addMapping("/*.html");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController("/api/v2/api-docs", "/v2/api-docs");
        registry.addRedirectViewController("/api/swagger-resources/configuration/ui",
                "/swagger-resources/configuration/ui");
        registry.addRedirectViewController("/api/swagger-resources/configuration/security",
                "/swagger-resources/configuration/security");
        registry.addRedirectViewController("/api/swagger-resources", "/swagger-resources");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/swagger-ui.html**")
                .addResourceLocations("classpath:/META-INF/resources/swagger-ui.html");
        registry.addResourceHandler("/api/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }}