[英]CORS policy: Cannot parse Access-Control-Allow-Headers response header field in preflight response
I have a CORS issue with my angular & spring boot app.我的 angular & spring boot 应用程序存在 CORS 问题。 I don't understand what it is.我不明白它是什么。
My angular code :我的角度代码:
login(user : User) { return this.http.post<User>(this.apiURL+'/login', user , {observe:'response'}); }
and to the server side, i allow cross origin like that在服务器端,我允许像那样交叉原点
response.addHeader("Access-Control-Allow-Origin", "*"); response.addHeader("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT,DELETE"); response.addHeader("Access-Control-Allow-Headers", "Access-Control-Allow-Headers," + "Origin, Accept, X - Requested - With, Content - Type, Access - Control - Request - Method," + "Access - Control - Request - Headers, Authorization"); response.addHeader("Access-Control-Expose-Headers", "Authorization, Access-ControlAllow-Origin,Access-Control-Allow-Credentials "); if (request.getMethod().equals("OPTIONS")) { response.setStatus(HttpServletResponse.SC_OK); return; }
In your Spring application you should have a security configuration file, you can call it SecurityConfig.java.在您的 Spring 应用程序中,您应该有一个安全配置文件,您可以将其命名为 SecurityConfig.java。
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.cors()
.configurationSource(corsConfigurationSource())
// other config here
}
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration.applyPermitDefaultValues());
return source;
}
} }
Try this and see if it works.试试这个,看看它是否有效。
In your Spring Controller you should also have this annotation before your controller class @CrossOrigin(origins = "http://localhost:4200") Check if 4200 is the port you use for your Angular app, if not change it with your port.在您的 Spring Controller 中,您还应该在控制器类之前添加此注释 @CrossOrigin(origins = "http://localhost:4200") 检查 4200 是否是您用于 Angular 应用程序的端口,如果不是用您的端口更改它。
@CrossOrigin(origins = "http://localhost:4201")
@RestController
public class YourController {
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.