donlih2986 2016-02-01 10:31
浏览 54
已采纳

了解JWT和HTTP授权标头? (客户端:Angular,服务器:php)

My angular app sends login credentials to my php server using HTTP Post. I receive the creds and create my JWT token fine. I can send the JWT token back in the response body, save it on the client side and use it to create an Authorization header for all subsequent client requests. That's all fine.

What I want to do is send the newly created JWT token back to the client in an Authorization header and have my client save it from the header rather than the body. That way I can refresh my token as need be and it can be seamless on the client side.

Searching around, it seems like the Authorization header is set when sending requests to a server. In php there is the curl functions, but I'm not sure if that's the right direction.

Is it possible to create an Authorization header for an Http Post response?

  • 写回答

1条回答 默认 最新

  • dongzhuo3202 2016-02-04 10:40
    关注

    Thilo's comment is right. More so, in php you can create any response header you want by simply using the "header" function:

    <?php
    ... POST api, generate $token, etc...
    header('MyCustomHeader: Bearer'.$token);
    ... 
    echo $http_body_response
    ?>
    

    My issue was that in Chrome, I was looking for my headers in the console response when I should have been looking under the Network tab. The network tab shows all the headers attached to the http response.

    Note: I'm using angular2-beta and I can only access my custom header in Angular2 if I add another response header exposing it. Once I do this, the header does show in the console. Therefore:

    <?php
    header("Access-Control-Expose-Headers: MyCustomHeader");
    ... POST api, generate $token, etc...
    header('MyCustomHeader: Bearer'.$token);
    ... 
    echo $http_body_response
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 vue使用gojs,需求在link中的虚线上添加方向箭头
  • ¥15 CSS通配符清除内外边距为什么可以覆盖默认样式?
  • ¥15 SPSS分类模型实训题步骤
  • ¥15 求解决扩散模型代码问题
  • ¥15 工创大赛太阳能电动车项目零基础要学什么
  • ¥20 limma多组间分析最终p值只有一个
  • ¥15 nopCommerce开发问题
  • ¥15 torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGKILL
  • ¥15 QuartusⅡ15.0编译项目后,output_files中的.jdi、.sld、.sof不更新怎么解决
  • ¥15 pycharm输出和导师的一样,但是标红