gpt4 book ai didi

angular - 在Spring Boot中使用Angular启用了cors,仍然出现cors错误

转载 作者:行者123 更新时间:2023-12-03 15:47:57 24 4
gpt4 key购买 nike

我为所有原点和 header 启用了cors,但是当我从我的 Angular 应用程序调用get方法到spring boot时,仍然出现cors错误。
来自控制台的Cors错误:

Access to XMLHttpRequest at 'http://localhost:8080/api/users/test@ronny.nl' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
我的 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);
}

}
我在我的 Angular 应用程序中调用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
但是还是没有运气...

最佳答案

这可能是由于Spring库中的更改,从而影响了Spring Boot 2.4.0。
在这里查看https://github.com/spring-projects/spring-framework/issues/26111及其相关票证https://github.com/spring-projects/spring-framework/pull/26108
编辑
这里是来自第一个链接的原始消息:

#25016 introduced the ability to configure allowedOriginPatterns in addition to just allowedOrigins. It lets you define more flexiblepatterns while the latter is literally the value to return in theAccess-Control-Allow-Origin header and for that "*" is not allowed incombination with allowCredentials=true. The change introducedequivalent allowedOriginPatterns methods in the WebMvc and the WebFluxconfig, but not in the SockJS config and the AbstractSocketJsService.

I'll add those for 5.3.2. You'll then need to switch toallowedOriginPatterns instead of allowedOrigins but that gives you anoption to define more precisely the allowed domain patterns. In themean time, you might be able to work around by listing specificdomains if that's feasible.

关于angular - 在Spring Boot中使用Angular启用了cors,仍然出现cors错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64892592/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com