【发布时间】:2020-05-20 03:31:24
【问题描述】:
您好,我正在研究 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;
}
}
【问题讨论】:
-
由于浏览器中的同源策略限制,您只会在浏览器中发出 CORS。
-
感谢您的回复。你能给出一个示例代码,我必须在哪里写标题 Access-Control-Allow-Origin: *
-
您好,感谢您的回复。请在下面找到错误和标题:---GENERAL--- 请求 URL:localhost:8080/employee/activeemployeesummary 请求方法:OPTIONS 状态代码:200 远程地址:[::1]:8080 推荐人策略:no-referrer-when-降级---响应标题---允许:OPTIONS、GET、HEAD、POST 内容长度:0 日期:星期日,2020 年 2 月 9 日 07:05:28 GMT
-
--REQUEST HEADER-- Accept: / Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.9 Access-Control- Request-Headers: access-control-allow-origin Access-Control-Request-Method: GET Connection: keep-alive Host: localhost:8080 Origin: localhost:4200 Referer: localhost:4200/employee-master Sec-Fetch-Dest: empty Sec-Fetch -模式:cors Sec-Fetch-Site:同站点用户代理:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36
标签: spring-boot cors