gpt4 book ai didi

angular - 没有 'Access-Control-Allow-Origin' 与 Spring-boot/Http call angular 2 的 Angular

转载 作者:行者123 更新时间:2023-12-03 23:30:22 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





How to enable CORS at Spring Security level in Spring boot [closed]

(2 个回答)


3年前关闭。




I'm trying to get data to angular application from spring boot server via angular http call. Also I have added security dependency for spring boot application.



当我尝试使用浏览器时,这会出现。

enter image description here

一旦我在 Intellij 的控制台中输入密码,它就会显示响应。

enter image description here

When I'm trying with angular http call it fails and show error on topic.



我的 Angular 组件具有以下代码。
constructor(private http: HttpClient) { 
http.get('http://localhost:8080/resource').subscribe(data => this.greeting = data);
}

我的 Spring Boot 应用程序有以下代码
@RequestMapping("/resource")
public Map<String,Object> home() {
Map<String,Object> model = new HashMap<String,Object>();
model.put("id", UUID.randomUUID().toString());
model.put("content", "Hello World");
return model;
}

我也读过 跨域资源共享 (CORS) 但尚不清楚如何将这些应用于 Angular 。

我的错误是:

enter image description here

最佳答案

What I have done in angular application was correct and had to correct my spring boot application only.



我所做的唯一更改是创建名为 的配置文件网络安全配置。
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors();
}

@Bean
CorsConfigurationSource corsConfigurationSource() {
UrlBasedCorsConfigurationSource source = new
UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", new CorsConfiguration().applyPermitDefaultValues());
return source;
}
}

关于angular - 没有 'Access-Control-Allow-Origin' 与 Spring-boot/Http call angular 2 的 Angular ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48919460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com