CORS阻止了我的后端服务器,如何解决?使用 Springboot java 作为后端,使用 react js 作为我的


import React, { Component } from "react";
import axios from 'axios';
class App extends Component {

handleSubmit(event) {
axios.post('http://localhost:3050/login', {
"username": "username",
"password": "password"
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
event.preventDefault();
}
render() {
return(
<form onSubmit={this.handleSubmit}>
<input type="submit" value="Submit" />
</form>
);
}
}
export default App;

只需检查用户名设置为"用户名"且密码设置为"密码"的 json 的后端

我的后端是 Spring 启动,使用带有"用户名"和"密码"的结束链接/登录应该会给出一些响应。所以这段代码有效,除了 CORS 阻止连接,所以它永远停留在处理上。我找到的解决方案是禁用chrome中的所有安全性,并且可以正常工作。但是我正在寻找一种永久的解决方案,而不必禁用chrome设置的安全性。不确定我是通过弹簧靴还是反应来做

尝试在 Spring REST 端点上使用 '@CrossOrigin' 注释,在 '@RequestMapping' 注释上方。

例如:-

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

在请求 POST 时,您可能需要其他配置,如下所示;

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

在 u 配置中创建此 bean

@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;
}

请在存在 Main Spring 引导类的包中的项目中添加以下文件。这对我适用于春季启动,React生态系统中的所有CORS问题。

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/");
}}

最新更新