Spring Angular2身份验证失败,返回401

Spring Angular2身份验证失败,返回401,spring,spring-mvc,authentication,angular,http-status-code-401,Spring,Spring Mvc,Authentication,Angular,Http Status Code 401,我正在用Spring MVC构建Angular2应用程序。我用SpringSecurity添加了身份验证,当我用postman查询REST服务器时,一切都很好。当我在Angular2中将授权添加到我的标题时,我得到一个401错误 错误: 请求的资源上不存在“Access Control Allow Origin”头。起源'http://localhost:4200因此,不允许访问。响应的HTTP状态代码为401 我的Angular2服务: @Injectable() export class B

我正在用Spring MVC构建Angular2应用程序。我用SpringSecurity添加了身份验证,当我用postman查询REST服务器时,一切都很好。当我在Angular2中将
授权
添加到我的标题时,我得到一个401错误

错误:
请求的资源上不存在“Access Control Allow Origin”头。起源'http://localhost:4200因此,不允许访问。响应的HTTP状态代码为401

我的Angular2服务:

@Injectable()
export class BookService {
  headers = new Headers();
  options = new RequestOptions({ headers: this.headers });

  constructor(
    private http: Http) {
      this.headers.append('Content-Type', 'application/json');
      this.headers.append('Authorization', 'Basic ' + btoa('nirgn:password'));
  }

  getBooksByCategory(categoryId: number) {
    return this.http.get(environment.baseUrl + 'categories/' + categoryId + '/books', this.options)
      .map((res: Response) => <Book[]>res.json()._embedded.books)
      .catch(this.handleError);
  }
}
当我删除
和()

所以,总而言之,我很确定它不是
访问控制允许源站
,这是因为服务器没有得到我的授权头。当我尝试在postman中查询服务器时,如果没有auth,我也会得到401,但是当我添加基本auth时,我会得到200

我还检查了postman和angualr2中的密码输出,以确保angualr2中的
'Basic'+btoa('nirgn:password')
输出的密码与postman的密码相同,并且是相同的。这是:
所以我设法弄明白了。毕竟是科尔斯(春天)

我必须告诉我的
configure
类使用我创建的
CORSFilter

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.httpBasic().and().authorizeRequests().anyRequest().authenticated()
        .and().addFilterAfter(new CORSFilter(), CsrfFilter.class);
}
奇怪的是,我仍然不明白为什么邮递员成功了,而在浏览器中(使用angular)我得到了错误

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.httpBasic().and().authorizeRequests().anyRequest().authenticated()
        .and().addFilterAfter(new CORSFilter(), CsrfFilter.class);
}