-3

嗨,我正在研究 spring boot、angular 8 和 mongodb。我正面临错误

Access to XMLHttpRequest at 'http://localhost:8080/employee/activeemployeesummary' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource

当我在邮递员上测试相同的代码时,它工作得非常好,但是它不能工作,因为 chrome 使用 CORS 策略。

我的代码:

package com.sani.springbootrestfulapi;
public class SpringBootMongoApplication extends SpringBootServletInitializer {
    public static void main(String args[]) {
        SpringApplication.run(SpringBootMongoApplication.class, args);
    }
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**").allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "PATCH")
                    .allowedHeaders("Origin, X-Requested-With, Content-Type, Accept")
                        .allowedOrigins("http://localhost:4200");
            }
        };
    }
}

下面:员工控制器代码

package com.sani.springbootrestfulapi.controller;
@RestController
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    private EmployeeService empService;
    @Autowired
    private OrganizationService organizationService;
    @PostMapping("/save") 
    public ResponseEntity<EmployeeEntity> save(@RequestBody EmployeeEntity emp) {
        if (empService.findByrNumber(emp.getrNumber())) 
            return new ResponseEntity<EmployeeEntity>(HttpStatus.FOUND);
        else {
            organizationService.joinOrganization(emp);
            return new ResponseEntity<EmployeeEntity>(HttpStatus.OK);
        }
    }
    @PutMapping("/update") /* here we need to pass id, the spring will consider as update */
    public ResponseEntity<EmployeeEntity> update(@RequestBody EmployeeEntity emp) {
        EmployeeEntity employee = empService.getOne(emp.getId());
        if (employee != null) {
            organizationService.joinOrganization(emp);
            return new ResponseEntity<EmployeeEntity>(HttpStatus.OK);
        } else
            return new ResponseEntity<EmployeeEntity>(HttpStatus.NOT_FOUND);
    }
    @GetMapping("/activeemployeesummary")
    public List<EmployeeEntity> getActiveEmployeeSummary() {
        List<EmployeeEntity> employee = new ArrayList<>();
        empService.getActiveEmployeeSummary().forEach(employee::add);
        return employee;
    }
    @GetMapping("/inactiveemployeesummary")
    public List<EmployeeEntity> getInactiveEmplo`enter code here`yeeSummary() {
        List<EmployeeEntity> employee = new ArrayList<>();
        empService.getInactiveEmployeeSummary().forEach(employee:`enter code here`:add);
        return employee;
    }
}
4

2 回答 2

1

将此添加@Bean到您@Configuration或您的主要课程中。

@Bean
    public CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOrigins(Arrays.asList("*"));
        configuration.setAllowedMethods(
            Arrays.asList("GET","POST","HEAD","DELETE","PUT","OPTIONS"));
        configuration.setMaxAge(1l);
        configuration.setAllowCredentials(true);
        configuration.setAllowedHeaders(Arrays.asList("*"));
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }
于 2020-02-13T09:31:07.873 回答
-1

我想你只是错过了那个标题=>

Access-Control-Allow-Origin: *
于 2020-02-04T13:39:13.567 回答