2017-02-13 10 views
1

私はSpringBoot Securityを使用してサーバーにPOSTを実行しようとするAngular 2 Webクライアントを構築しています。 Springのセキュリティ設定をどのように書くべきですか?Angular 2のSpringセキュリティの設定

認証のための私の角度コール:

public login(username, password) { 
    let body = JSON.stringify({username: username, password: password}); 
    let headers = new Headers({'Content-Type': 'application/json'}); 
    let options = new RequestOptions({headers: headers}); 
    this.http.post("http://localhost:8080/login", body, options) 
    .subscribe(
     res => this.loggedIn = true, 
     err => console.error("failed authentication: " + err), 
    () => console.log("tried authentication") 
    ); 
} 

認証がエラーで失敗します。

{"timestamp":1487007177889,"status":401,"error":"Unauthorized","message":"Authentication Failed: Empty Username","path":"/login"}

私の春のセキュリティ設定:

@Configuration 
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {  
    @Autowired 
    private RestAuthenticationEntryPoint restAuthenticationEntryPoint; 
    @Autowired 
    private RestAuthenticationSuccessHandler restAuthenticationSuccessHandler; 
    @Autowired 
    private RestAuthenticationFailureHandler restAuthenticationFailureHandler; 
    @Autowired 
    private RestLogoutSuccessHandler restLogoutSuccessHandler; 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.csrf().disable() 
       .exceptionHandling() 
       .authenticationEntryPoint(restAuthenticationEntryPoint) 

       .and().formLogin() 
       .loginProcessingUrl("/login") 
       .usernameParameter("username") 
       .passwordParameter("password") 
       .successHandler(restAuthenticationSuccessHandler) 
       .failureHandler(restAuthenticationFailureHandler) 
       .permitAll() 

       .and().logout() 
       .logoutUrl("/logout") 
       .logoutSuccessHandler(restLogoutSuccessHandler) 
       .permitAll() 

       .and().authorizeRequests().anyRequest().authenticated() 
     ; 
    } 

    @Override 
    public void configure(AuthenticationManagerBuilder builder) throws Exception { 
     // This configuration has been tested, it works. 
     // It has been removed for better readability 
    } 

    @Bean 
    public LdapContextSource contextSource() { 
     // This configuration has been tested, it works. 
     // It has been removed for better readability 
    } 
} 
+0

春の問題のようです。あなたのAngularコードに間違いはありません。 – AngularChef

答えて

1

あなたが使うことになっているapplication/x-www-form-urlencoded JSONではなくフォーム・ログインのパラメータ。そのため、Spring SecurityはHttpServletRequest#getParametersから取得しようとしているため、ユーザー名が不足しているというエラーが表示されます。角度でフォームパラメータを送信するには、あなたは、HTTP要求のボディパラメータとして設定した場合、それは(私が覚えているから)が自動的に正しい形式にシリアル化する必要がある

import { URLSearchParams } from '@angular/http'; 

let params = new URLSearchParams(); 
params.set('username', username); 
params.set('password', password); 

を行うことができ、すなわち

username=xxx&password=xxx 

そして、私はあなたがいずれかのヘッダapplicatio/x-www-form-urlencodedContent-Typeを設定する必要はないと思います。角度がURLSearchParamsを体として検出すると、あなたにも設定する必要があります。

+0

それはトリックをした、多くのありがとう。 男 'URLSearchParam'、何ですか?どのように直感的ですか? –

+0

それはクエリ文字列にも使用されます。私はその名前がもっと理にかなっていると思う。 –

+0

URLSearchParamsはWebkitでは動作しない – Anatoly