我正在構建一個Angular 2 Web客戶端,該客戶端嘗試使用SpringBoot Security向服務器執行POST。我應該如何編寫我的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
}
}
看起來像一個春天的問題。我在你的Angular代碼中看不到任何錯誤。 – AngularChef