【问题标题】:Empty HTTP response headers in browser but filled in POSTMAN浏览器中的 HTTP 响应标头为空,但在 POSTMAN 中填写
【发布时间】:2018-07-17 11:59:31
【问题描述】:

我创建了一个带有 React 前端的 Spring Boot 后端。

这对我来说没有意义。

代码: https://github.com/The-Taskmanager/SelfServiceWebwizard

后端映射

@PostMapping("/signin")

public ResponseEntity<?> authenticateUser(@Valid @RequestBody LoginRequest loginRequest) {

    Authentication authentication = authenticationManager.authenticate(
            new UsernamePasswordAuthenticationToken(
                    loginRequest.getUsernameOrEmail(),
                    loginRequest.getPassword()
            )
    );

    SecurityContextHolder.getContext().setAuthentication(authentication);

    String jwt = tokenProvider.generateToken(authentication);
    HttpHeaders headers = new HttpHeaders();
    headers.add("Mustermann", "Max");
    headers.add("Content-Type", "application/json");
    headers.add("Authorization", new JwtAuthenticationResponse(jwt).getAccessToken());
    return ResponseEntity.ok().headers(headers).body(new JwtAuthenticationResponse(jwt));

}

前端请求

login(username: string, password: string) {

    fetch('http://localhost:8080/api/auth/signin', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            "usernameOrEmail": username,
            "password": password
        })
    }).then(response => {
        console.log(response);
        console.log(response.text());
        console.log(response.headers);
    })
        .catch(error => console.error(error));
}

更新1: 添加了此方法,但浏览器中的标题仍然为空:

CorsConfigurationSource corsConfigurationSource() {
    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration config = new CorsConfiguration().applyPermitDefaultValues();
    source.registerCorsConfiguration("/**", config);
    return source;
}

更新 2:

当我使用 React 请求时,Spring Boot 向我显示此错误:

ERROR 8876 --- [nio-8080-exec-4] c.s.jwt.JwtAuthenticationEntryPoint:  Responding with unauthorized error. Message - full authentication is required to access this resource

浏览器控制台:

当我向 Postman 请求时,Spring Boot 没有显示错误。

更新3:

React 发送的请求头:

{
host=[localhost:8080],
content-type=[application/json],
cache-control=[no-cache],
user-agent=[Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0],
accept-encoding=[gzip, deflate],
content-length=[58],
referer=[http://localhost:8080/],
origin=[http://localhost:8080],
dnt=[1],
connection=[keep-alive],
accept-language=[de,en-US;q=0.7,en;q=0.3],
accept=[*/*]
}

POSTMAN 从 POSTMAN 控制台发送的请求标头:

host:"localhost:8080"
content-type:"application/json"
cache-control:"no-cache"
user-agent:"PostmanRuntime/7.1.5"
accept-encoding:"gzip, deflate"
content-length:68
postman-token:"b00fd7a8-bd34-4a32-8681-990b04012e3b"
cookie:"JSESSIONID=2CAEC9280432DD13AABA53B73B7874AC"
accept:"*/*"

【问题讨论】:

  • 这可能和你的问题一样。我自己目前无法对此进行测试。 stackoverflow.com/questions/44501476/…
  • 尝试添加以下内容。输入您要公开的标题 - 以逗号分隔。 headers.add("Access-Control-Expose-Headers", "Authorization, Ziethen, OtherCustomHeaders...");
  • @kkflf 谢谢。完成,但没有解决问题
  • 好的。对不起,那我帮不了你。另一个 stackoverflow 帖子看起来与您的非常相似。我以前没有使用过 react,但我认为它收到了标头,但不会将标头暴露给浏览器。在浏览器中查看标题重要吗?
  • @kkflf 我想在标头中存储一个 JSON 令牌。我不认为 React 无法在浏览器中显示标题,我认为 React 在请求后没有收到带有标题的响应。因为当我尝试在 React 中解析响应时,我得到一个 JSON 解析错误。

标签: spring rest spring-boot http-headers httpwebresponse


【解决方案1】:

自己解决。

修改了前端 fetch 方法中的代码

console.log(response.headers)

// changed to:

console.log(response.headers.get('Authorization')

所以标题一直都在那里,但我无法使用 React 将它们记录到浏览器控制台中。

尽管如此,UPDATE2 中仍存在 Spring Boot(完全身份验证)和浏览器 (401) 错误。不知道这是从哪里来的。

【讨论】:

    猜你喜欢
    • 2015-11-30
    • 2014-08-24
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 2023-04-10
    • 2019-04-26
    • 2015-12-05
    • 1970-01-01
    相关资源
    最近更新 更多