Spring boot Cors:弹簧靴和角型

Spring boot Cors:弹簧靴和角型,spring-boot,Spring Boot,我正在创建一个简单的CRUD应用程序,使用SpringBoot作为后端API,使用Angular作为前端。然而,我被“CORS策略”错误所困扰。我已经尝试在controller类中添加@CrossOrigin注释,然后尝试在主类旁边添加Corsfilter,我的前端应用程序仍然无法访问Spring back end Java主Api类: @SpringBootApplication public class MongoProjectApplication { public static

我正在创建一个简单的CRUD应用程序,使用SpringBoot作为后端API,使用Angular作为前端。然而,我被“CORS策略”错误所困扰。我已经尝试在controller类中添加@CrossOrigin注释,然后尝试在主类旁边添加Corsfilter,我的前端应用程序仍然无法访问Spring back end

Java主Api类:

@SpringBootApplication
public class MongoProjectApplication {
    public static void main(String[] args) {
        SpringApplication.run(MongoProjectApplication.class, args);
    }
    
    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.setAllowCredentials(true);
        corsConfiguration.setAllowedOrigins(Arrays.asList("http://localhost:4200"));
        corsConfiguration.setAllowedHeaders(Arrays.asList("Origin", "Access-Control-Allow-Origin", "Content-Type",
                "Accept", "Authorization", "Origin, Accept", "X-Requested-With",
                "Access-Control-Request-Method", "Access-Control-Request-Headers"));
        corsConfiguration.setExposedHeaders(Arrays.asList("Origin", "Content-Type", "Accept", "Authorization",
                "Access-Control-Allow-Origin", "Access-Control-Allow-Origin", "Access-Control-Allow-Credentials"));
        corsConfiguration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS"));
        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(urlBasedCorsConfigurationSource);
    }
}
再控制员:

@SpringBootApplication
@RestController
public class StudentController {

    @Autowired
    public StudentRepository studentRepository;
    
    @Autowired
    public SequenceGeneratorService sequenceGeneratorService;
    
    @GetMapping(value = "/all")
    public List<Student> getAllStudents() {
        return studentRepository.findAll();
    }
    @PostMapping(value = "/create")
    public String createStudent(@RequestBody Student student) {
        student.setId(sequenceGeneratorService.generateSequence(Student.SEQUENCE_NAME));
        Student insertedStudent = studentRepository.insert(student);
        return "Student Created " + insertedStudent.getName();
    }
    
}
角度服务:

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

  endpoint: string = 'localhost:8080';
  headers = new HttpHeaders({
    'Content-Type':  'application/json',
    'Authorization': 'Basic ' + btoa('angel:123')
  });
  
  constructor(private http: HttpClient) { }

    // Get all student
    public getUsers() {
      let API_URL = `${this.endpoint}/all`;
      return this.http.get(API_URL, { headers: this.headers });
    }
}
错误: 从源站访问“localhost:8080/all”处的XMLHttpRequesthttp://localhost:4200'已被CORS策略阻止:跨源请求仅支持协议方案:http、data、chrome、chrome扩展、chrome untrusted、https。

您尝试过这个吗?->。它是否与包含通配符作为值的@CrossOrigin(value=“*”)一起工作?
@Injectable({
  providedIn: 'root'
})
export class StudentService {

  endpoint: string = 'localhost:8080';
  headers = new HttpHeaders({
    'Content-Type':  'application/json',
    'Authorization': 'Basic ' + btoa('angel:123')
  });
  
  constructor(private http: HttpClient) { }

    // Get all student
    public getUsers() {
      let API_URL = `${this.endpoint}/all`;
      return this.http.get(API_URL, { headers: this.headers });
    }
}