2017-11-02 10 views
0

this Spring tutorial次のBeanを設定ロジックに追加しました。私はSpring Bootを使用しています。Angular AppとSpringバックエンド間のCORS半作業

あなたは春のブートを使用している場合、ちょうど、次のよう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) 
    ); 
} 

た会話:

enter image description here

私は問題が何であるか見当がつかない。

答えて

0

承認ヘッダーは特別です。 別の方法で追加する必要があります。

.allowCredentials(false)

関連する問題