【发布时间】:2018-04-15 11:54:06
【问题描述】:
在this Spring tutorial 之后,我已将以下 bean 添加到我的配置逻辑中。请注意,我使用的是 Spring Boot。
如果您使用的是 Spring Boot,建议只声明一个 WebMvcConfigurer bean,如下所示:
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
// Just slightly modified
.allowedMethods("HEAD", "GET", "PUT", "POST",
"DELETE", "PATCH", "OPTIONS")
.allowedOrigins("*")
.allowedHeaders("*");
}
};
}
事情是这样的:我可以从客户端注册用户:
onSubmit() {
this.http.post('http://localhost:8080/register', this.registerForm.value)
.subscribe(
data => this.onRegisterSuccess(),
err => alert(err.error.message)
);
}
但我无法访问我的 OAuth2 令牌的端点:
onSubmit() {
const headers = new HttpHeaders()
.set('Content-Type', 'application/x-www-form-urlencoded')
.set('Authorization', 'Basic dGVzdGp3dGNsaWVudGlkOlhZN2ttem9OemwxMDA=');
this.http.post('http://localhost:8080/oauth/token', this.loginForm.value, {headers: headers})
.subscribe(
data => this.onLoginSuccess(),
err => alert(err.error.message)
);
}
由此产生的对话:
我不知道问题是什么。
【问题讨论】:
标签: angular spring-boot cors spring-oauth2