【问题标题】:How to access the JWT token value from a response object getting back in axios?如何从返回 axios 的响应对象访问 JWT 令牌值?
【发布时间】:2020-10-20 19:30:33
【问题描述】:

我正在开发一个 react JS - spring boot 应用程序,所以我使用 JWT + Spring Security 来保护其余端点。所以我开发了 spring boot 应用程序以在响应标头中返回如下所示的 JWT 令牌。现在我需要使用 react js 访问它。我怎样才能做到这一点?我正在使用 axios 从反应中调用端点。

这是我在使用 Spring Boot 登录端点时得到的示例响应。

【问题讨论】:

    标签: reactjs spring-security axios jwt


    【解决方案1】:

    您的问题是如何使用 axios 访问响应标头:

    这是一个示例代码:

    export const REST_API = axios.create({
      baseURL: 'http://localhost:8080/api',
      timeout: 10000,
      headers: {
        'Content-Type': 'application/json',
      },
    });
    
    

    然后使用你的 axios 配置的客户端

      REST_API.post(`/authenticate`, {
                      username: 'admin',
                      password: 'admin',
                    })
                  )
                  .then((res: AxiosResponse<any>) => {
                  console.log(response.headers.Authorization);
                  });
    
    

    希望对你有帮助

    【讨论】:

    • 感谢您的回答,但我没有像邮递员那样在响应对象中获得授权值
    猜你喜欢
    • 1970-01-01
    • 2020-05-25
    • 2019-04-15
    • 2019-12-08
    • 2018-01-13
    • 2018-11-03
    • 2019-11-29
    • 1970-01-01
    • 2021-04-20
    相关资源
    最近更新 更多