我有一个简单的角度应用程序,该应用程序以这种方式执行端点:
login(username:string, password:string){
return this.http.post<String>('localhost:9090/helios-admin/api/auth/login',{
"username": username,
"password":password
});
}
但是,CORS策略的错误被抛出。在我的春季应用程序中,这是公开端点的一个,我以这种方式设置了CORS映射:
@Configuration
@EnableWebMvc
public class MyWebMvcConfig implements WebMvcConfigurer{
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/**")
.allowedOrigins("*")
.allowedMethods("GET","POST", "OPTIONS");
}
}
我遇到了同样的问题,但是有了另一个应用程序,提出了一个问题,得到了响应并正确使用了。现在,有了这个角度项目,它行不通。我必须更改某些东西吗?
编辑这是我的Spring Boot中的控制器
@CrossOrigin
@PostMapping(PathConstants.LOGIN_ACTION)
@ApiOperation("Login into Helios administrational console.")
@Override
public ResponseEntity<?> autenticate(@Valid @RequestBody LoginRequest request) {
Authentication auth = authManager
.authenticate(new UsernamePasswordAuthenticationToken(request.getUsername(), request.getPassword()));
SecurityContextHolder.getContext().setAuthentication(auth);
String token = jwtProvider.generateToken(auth);
return ResponseEntity.ok(new JwtAuthResponse(token));
}
update
问题是在URL中:它想要一个'http://localhost:9090/helios-admin/api/auth/login'
,因此使用HTTP。愚蠢的错误。
现在我有一个新的。
它说:
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
所以我添加了:
.allowedHeaders("Content-Type", "X-Requested-With", "accept", "Origin", "Access-Control-Request-Method",
"Access-Control-Request-Headers")
.exposedHeaders("Access-Control-Allow-Origin", "Access-Control-Allow-Credentials")
,在角度
this.httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json',
'Access-Control-Allow-Origin':'*'})
};
return this.http.post<String>('http://localhost:9090/helios-admin/api/auth/login',{
"username": username,
"password":password
}, this.httpOptions).pipe(catchError(this.errorHandler));
但行不通。
添加 @crossorigin 在REST API控制器类方法之前注释。
示例
@CrossOrigin
@RequestMapping(value = "/login", method = {RequestMethod.POST}, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ResponseBean> postUserLogin(@RequestBody LoginDTO loginDTO) {
//TODO
}
您可以尝试这样的尝试
public httpOptions;
login(username:string, password:string){
this.httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json', 'Authorization': <some data> })
};
return this.http.post<String>(
'localhost:9090/helios-admin/api/auth/login',
{ "username": username, "password":password },
this.httpOptions
);
}
您应该将配置添加到Spring,以允许CORS
我这样做了:
java
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**");
}
}
kotlin
@EnableWebMvc
class WebConfig : WebMvcConfigurer {
override fun addCorsMappings(registry: CorsRegistry) {
registry.addMapping("/**")
}
}
您可以在这里看到春季的另一种方法。祝你好运
https://spring.io/blog/2015/06/08/cors-support-in-spring-framework
https://www.baeldung.com/spring-cors