简体   繁体   English

无法使用我的 Angular 应用程序在 spring 启动中启用 CORS

[英]Unable to enable CORS in spring boot with my Angular app

I've tried the options suggested, for some reason my Angular client on port 4200 can not make the call to my spring boot app on port 8081. I am unsure if its a CORS issue or if I am missing something with my HttpClient on my front end... I've tried the options suggested, for some reason my Angular client on port 4200 can not make the call to my spring boot app on port 8081. I am unsure if its a CORS issue or if I am missing something with my HttpClient on my前端...

my component:我的组件:

import { Component, OnInit } from '@angular/core';
import { HttpService } from './../http.service';
import { Video } from './../video';


@Component({
  selector: 'app-video-details-upload',
  templateUrl: './video-details-upload.component.html',
  styleUrls: ['./video-details-upload.component.scss']
})
export class VideoDetailsUploadComponent implements OnInit {

 video: Video = new Video(); 

  constructor(private httpService: HttpService) { }

  ngOnInit() {
  }

  saveVideo() {
    return this.httpService.postVideo(this.video);
  }

my app.module and my http.service class:我的 app.module 和我的 http.service class:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { FormsModule } from '@angular/forms';

import { AppComponent } from './app.component';
import { VideoDetailsUploadComponent } from './video-details-upload/video-details-upload.component';

@NgModule({
  declarations: [
    AppComponent,
    VideoDetailsUploadComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    FormsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }



import { Injectable } from '@angular/core';
import { Video } from './video';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class HttpService {

  urlVid = 'http://localhost:8081/video';

  constructor(private http: HttpClient) { }

  public postVideo(video: Video) {
    return this.http.post(this.urlVid, video, {responseType: 'text' as 'json'});
  }
}

My Video is a simple typescript class that is equivalent to my video POJO to be created by my springboot app...:我的视频是一个简单的 typescript class 相当于我的视频 POJO 将由我的 springboot 应用程序创建...:

@Entity
@Table(name="video")
public class Video {
    @Id
    private long id;
    private String title;
    private long duration;
    private String location;
    private String subject;
    private String contentType;

...and iv added CORS to my controller, as well as creating a config class(i tried them both...): ...并且 iv 将 CORS 添加到我的 controller 中,并创建了一个配置类(我都尝试了它们...):

@RestController
@CrossOrigin(origins = "http://localhost:4200")
public class VideoController {
    
    @Autowired
    VideoRepository videoRepository;
    
    private List<Video> videos = new ArrayList<Video>();

    @PostMapping("/video")
    public Video saveVideo(@RequestBody Video vid) {
        System.out.println("iv at least MADE IT HERE to the spboot controller....");

-> iv ommitted my logic after here because iv not been able to get this far, I dont see why not, also, ill include the config class i abruptly added but to no avail... -> iv 在这里之后省略了我的逻辑,因为 iv 无法做到这一点,我不明白为什么不包括我突然添加的配置 class 但无济于事......

@Configuration
public class AppConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:4200")
                .allowedMethods("POST");
    }
}

...any assistance very much appreciated, I have spent too long trying to fix this... ...非常感谢任何帮助,我花了太长时间试图解决这个问题...

In the main class, add the following config.在主 class 中,添加以下配置。 The origin is set to "http://localhost:4200" , please configure your origin and headers来源设置为"http://localhost:4200" ,请配置您的来源和标头

@Bean
CorsConfigurationSource corsConfigurationSource() {
    CorsConfiguration configuration = new CorsConfiguration();
    configuration.setAllowedOrigins(Arrays.asList("http://localhost:4200"));
    configuration.setAllowedMethods(Arrays.asList("GET", "POST", "OPTIONS", "DELETE", "PUT", "PATCH"));
    configuration.setAllowedHeaders(Arrays.asList("X-Requested-With", "Origin", "Content-Type", "Accept",
            "Authorization", "X-Authorization-Firebase"));
    configuration.setAllowCredentials(true);
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/**", configuration);
    return source;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM