【问题标题】:GET request is not called after successful OPTIONS call in angular with spring boot使用弹簧引导以角度成功调用 OPTIONS 后不调用 GET 请求
【发布时间】:2018-11-12 20:36:04
【问题描述】:

我正在尝试使用 Angular 和 Spring Boot 实现登录功能。

我正在关注春季教程https://spring.io/guides/tutorials/spring-security-and-angular-js/ 但就我而言,我的 Angular 项目托管在 localhost:4200 上,spring 托管在 localhost:8080 上

现在我正在向 Spring 服务器发送一个“/用户”请求。我的角度代码看起来像:

const headers = new HttpHeaders(credentials ? {
        authorization: 'Basic ' + btoa(credentials.username + ':' + credentials.password)
    } : {});

    this.http.get('http://localhost:8080'+'/user', { headers: headers }).subscribe(response => {
        if (response['name']) {
            this.authenticated = true;
        } else {
            this.authenticated = false;
        }
        return callback && callback();
    });

现在由于 CORS,它会发送 OPTIONS 请求,该请求成功,状态为 200。 在此之后,它不会发送实际的 GET 请求,该请求应该将凭据发送为 Angular $http is sending OPTIONS instead of PUT/POST

我的 spring 代码如下所示:

@CrossOrigin(origins = "*", maxAge = 3600, allowedHeaders={"x-auth-token", "x-requested-with", "x-xsrf-token"})
@RequestMapping("/user")
public Principal user(Principal user) {
    return user;
}

@Configuration
@Order(SecurityProperties.DEFAULT_FILTER_ORDER)
protected static class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.httpBasic().and().cors().and().authorizeRequests().antMatchers("/index.html", "/", "/home", "/login").permitAll()
                .anyRequest().authenticated().and().csrf()
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
    }
}

我还在 pom 文件中添加了 Spring Security。

请您帮忙看看为什么 GET 请求在 OPTIONS 请求后没有被触发。

【问题讨论】:

  • 你能检查浏览器开发工具(网络标签)
  • 我的响应头看起来像:Access-Control-Allow-Headers: x-requested-with Access-Control-Allow-Methods: GET,HEAD,POST Access-Control-Allow-Origin: * Access-Control-Max-Age: 3600 Cache-Control: no-cache, no-store, max-age=0, must-revalidate Content-Length: 0 Date: Sun, 03 Jun 2018 09:16:14 GMT Expires: 0 Pragma: no-cache Vary: Origin Vary: Access-Control-Request-Method Vary: Access-Control-Request-Headers X-Content-Type-Options: nosniff X-Frame-Options: DENY X-XSS-Protection: 1; mode=block
  • 这是您的 OPTIONS 回复吗??
  • 是的,它是我的 OPTIONS 请求的响应标头
  • OPTIONS成功后浏览器是否还有其他调用??

标签: java spring angular spring-boot


【解决方案1】:

根本原因: 在您的 Spring Security 中,您只允许三种类型的请求标头,即allowedHeaders={"x-auth-token", "x-requested-with", "x-xsrf-token"}

但是,在您的http.GET 请求中,您正在使用标头名称作为authorization 进行基本身份验证,这被Spring Security 拒绝。因此,spring 将允许的标头响应到您的浏览器(即使用 OPTIONS 方法的预检请求)并且您的浏览器会看到.. Ooops.. authorization 标头是不允许的,所以我无法继续进行原始 GET 调用。

解决方案: 在您的弹簧安全中也允许authorization 标头。它应该工作。!!

【讨论】:

  • 有什么方法可以在控制台上抛出异常,服务器拒绝了这个请求?
  • 实际上,服务器并没有拒绝它,服务器告诉你的浏览器我只允许这些请求。
  • @PriyeshKumar 那些预检请求实际上是浏览器在调用跨源服务器之前执行的安全检查。并且,作为响应,他们获得服务器允许的一组标准,基于这些标准浏览器检查您的请求,如果它对那些所谓的标准有效,那么只有浏览器进行原始调用。希望这是有道理的..!!
猜你喜欢
  • 2020-03-23
  • 1970-01-01
  • 1970-01-01
  • 2017-06-27
  • 2021-01-26
  • 1970-01-01
  • 2016-04-12
  • 2019-04-20
  • 2015-01-24
相关资源
最近更新 更多