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

标签 angular spring-boot cors

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





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/

相关文章:

javascript - 使用绑定(bind)函数删除 EventListener

spring-boot - Dockerizing Spring Boot应用程序时发生ClassNotFoundException

oauth-2.0 - Asp.net Core、JWT 和 CORS header

rest - 请求的资源上不存在 'Access-Control-Allow-Origin' header -Resteasy

angular - 在 Angular 6 中从控制台隐藏 IE 警告

css - 展开时具有可展开行背景颜色的 Angular Material 表

angular - 如何在 Angular 4.0 中隐藏和显示

testing - Spring Boot 中所有测试的测试配置文件?

java - 简单的承载 token 身份验证?

javascript - Ajax Jquery CORS 不工作