drake900918 2010-06-02 17:13
浏览 29
已采纳

Symfony Basic API Http身份验证

Can someone point me in the right direction in regards to making an api use basic http authentication?

I am creating a restful api with symfony but would like to require users to be logged in to get certain data. I would also like many of these methods be dependent on the the username in the authentication process in order to get some of the data (using the username from the credentials to get all of a users friends)

Thanks!

  • 写回答

1条回答 默认 最新

  • doudun3910 2010-06-02 17:24
    关注

    Something like this:

    function send401() {
        $realm = "Credentials for site xxx";
        header("HTTP/1.0 401 Authorization Required")
        header('WWW-Authenticate: Basic realm="'.$realm.'"');
        die();
    }
    
    
    if (!array_key_exists('PHP_AUTH_USER',$_SERVER) ||
            !array_key_exists('PHP_AUTH_PW',$_SERVER)) {
        send401();
    }
    else {
        $res = authenticate_user($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']);
        if (!$res)
            send401();
    }
    

    The username is stored in $_SERVER['PHP_AUTH_USER'].

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等