我使用Spring作为我的web应用程序的后端,使用Angular 2.0.0-beta.15作为我的前端。现在我正在尝试登录,下面是我的前端代码。我在userAccount中传递详细信息,这个函数是从组件调用的。我成功地在下面的功能中获取了登录详细信息。
authenticate(userAccount) {
var headers = new Headers();
headers.append('Authorization', 'Basic ' + btoa(userAccount.username + ':' + userAccount.password));
headers.append('X-Requested-With', 'XMLHttpRequest');
return this.http.get('/authenticate', {headers: headers}).map(this.extractData).catch(this.handleError);
}
我的spring安全文件包含以下信息。
@Autowired
protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception{
auth.userDetailsService(customUserDetailsService).passwordEncoder(passwordEncoder);
}
@Configuration
public static class UserSecurityConfig extends WebSecurityConfigurerAdapter{
@Override
public void configure(WebSecurity web) throws Exception{
web
.ignoring()
.antMatchers("/front-end/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception{
http
.requestMatchers()
.antMatchers("/**","/login")
.and()
.httpBasic().and().authorizeRequests()
.antMatchers("/login").permitAll()
.antMatchers("/angular2/**").permitAll()
.antMatchers("/node_modules/**").permitAll()
.antMatchers("/**").access("hasRole('" + ROLE_EMPLOYEE + "') or hasRole('" + ROLE_ADMIN + "') ")
.and()
.exceptionHandling().authenticationEntryPoint(restAuthenticationEntryPoint()).accessDeniedHandler(customAccessDeniedHandler())
.and()
.rememberMe().tokenRepository(persistentTokenRepository()).tokenValiditySeconds(REMEMBER_ME_TOKEN_EXPIRATION).and().csrf()
.csrfTokenRepository(csrfTokenRepository()).and()
.addFilterAfter(csrfHeaderFilter(), CsrfFilter.class);;
}
现在,当我尝试使用有效凭据登录时,在customUserDetailsService的后端,我会得到"未定义"作为我的用户名。因此,该请求将以401未经授权结束。有人能告诉我可能出了什么问题吗?
这是一个角度方面的问题。btoa函数返回了错误的用户名和密码。休息,一切都很好。