这个问题在这里已经有了答案:
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.
当我尝试使用浏览器时,这会出现。
一旦我在 Intellij 的控制台中输入密码,它就会显示响应。
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 。
我的错误是:
最佳答案
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/