我为所有来源和标头启用了 cors,但当我调用 a 时,我仍然收到 cors 错误get
从我的角度应用程序到 Spring Boot 的方法。
来自控制台的 Cors 错误:
Access to XMLHttpRequest at 'http://localhost:8080/api/users/[email protected]' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
My controller
(我称之为 getbyemail):
@CrossOrigin(origins = "*", allowedHeaders = "*")
@RestController
@RequestMapping(value = "/api/users", produces = MediaType.APPLICATION_JSON_VALUE)
public class UserController {
private final UserService userService;
@Autowired
public UserController(final UserService userService) {
this.userService = userService;
}
@GetMapping
public List<UserDTO> getAllUsers() {
return userService.findAll();
}
@GetMapping("/{id}")
public UserDTO getUser(@PathVariable final Long id) {
return userService.get(id);
}
@CrossOrigin(origins = "*", allowedHeaders = "*")
@GetMapping("/{email}")
public UserDTO getUserByMail(@PathVariable String email) {
return userService.getByEmail(email);
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Long createUser(@RequestBody @Valid final UserDTO userDTO) {
return userService.create(userDTO);
}
@PutMapping("/{id}")
public void updateUser(@PathVariable final Long id, @RequestBody @Valid final UserDTO userDTO) {
userService.update(id, userDTO);
}
@DeleteMapping("/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deleteUser(@PathVariable final Long id) {
userService.delete(id);
}
}
我从我的角度应用程序调用 get 的地方:
onSubmit(): void {
this.submitted = true;
this.wrongInput = false;
this.loginService.getLogin<User>(this.loginForm.value.email).subscribe((response) => {
this.tempUser = response;
console.log(this.tempUser);
if (this.loginForm.value.email === this.tempUser.email && this.loginForm.value.password === this.tempUser.password) {
this.localStorageService.save(this.tempUser);
this.router.navigate(['/home']);
console.log(true);
}
else {
this.wrongInput = true;
}
});
}
我还尝试添加一个DevCorsConfiguration
:
package com.team13.triviaquiz.triviaquizserver.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@Profile("development")
public class DevCorsConfiguration implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/**").allowedMethods("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS");
}
}
并将个人资料添加到我的application.properties
:
application.properties
spring.profiles.active=development
#enabling h2 console
spring.h2.console.enabled=true
#fixed URL for H2 (necessary from Spring 2.3.0)
spring.datasource.url=jdbc:h2:mem:triviaquizserver
#turn statistics on
spring.jpa.properties.hibernate.generate_statistics = true
logging.level.org.hibernate.stat=debug
#show all queries
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
logging.level.org.hibernate.type=trace
但还是没有运气...