【问题标题】:curl submit basic auth and website user authentification in one commandcurl 在一个命令中提交基本身份验证和网站用户身份验证
【发布时间】:2020-06-10 17:58:02
【问题描述】:

我有一个受基本身份验证保护的网站 dev.example.com。

我可以通过允许基本身份验证用户和密码访问该网站

https://ba_user:ba_pw@dev.example.com/

现在我还想在通过基本身份验证后以网站用户 (fe_user/fe_password) 的身份登录。

有没有办法提交网站用户登录数据以及基本身份验证数据?与

curl -u fe_user:fe_password https://ba_user:ba_pw@dev.example.com/import_log/1234/status

我收到 401 Unauthorized。

【问题讨论】:

    标签: linux authentication session curl


    【解决方案1】:

    假设您的站点接受用户登录凭据作为基本身份验证 (Authorization: Basic ...),并且它支持多个授权标头:

    basic_auth_header() 
    { 
        BASIC_AUTH=$(printf "%s:%s" "$1" "$2" | base64);
        echo "Authorization: Basic $BASIC_AUTH"
    }
    
    curl -H "$(basic_auth_header ba_user ba_pw)" -H "$(basic_auth_header fe_user fe_password)" https://dev.example.com/import_log/1234/status
    

    也就是说,通常网站在访问令牌(Authorization: Bearer ... 标头)中接受登录凭据,因此不能使用 curl -u user:password ...curl https://user:password@... 登录这些网站。

    如果是这种情况,那么您需要弄清楚如何从您的登录凭据生成访问令牌并使用:

    curl -H Authorization: Bearer <access token> https://ba_user:ba_pw@dev.example.com/import_log/1234/status
    

    您可以按如下方式测试这些情况:

    在一个终端窗口中:

    nc -l -p 50505
    

    在另一个终端窗口中:

    curl http://localhost:50505/
    

    以下是一些结果:

    curl http://localhost:50505/
    
    GET / HTTP/1.1
    Host: localhost:50505
    User-Agent: curl/7.47.0
    Accept: */*
    
    curl http://user1:password1@localhost:50505/
    
    GET / HTTP/1.1
    Host: localhost:50505
    Authorization: Basic dXNlcjE6cGFzc3dvcmQx
    User-Agent: curl/7.47.0
    Accept: */*
    
    curl -u user1:password1 http://localhost:50505/
    
    GET / HTTP/1.1
    Host: localhost:50505
    Authorization: Basic dXNlcjE6cGFzc3dvcmQx
    User-Agent: curl/7.47.0
    Accept: */*
    
    curl -u user1:password1 http://user1:password1@localhost:50505/
    
    GET / HTTP/1.1
    Host: localhost:50505
    Authorization: Basic dXNlcjE6cGFzc3dvcmQx
    User-Agent: curl/7.47.0
    Accept: */*
    
    curl -u user2:password2 http://localhost:50505/
    
    GET / HTTP/1.1
    Host: localhost:50505
    Authorization: Basic dXNlcjI6cGFzc3dvcmQy
    User-Agent: curl/7.47.0
    Accept: */*
    
    curl -u user1:password1 http://user2:password2@localhost:50505/
    
    GET / HTTP/1.1
    Host: localhost:50505
    Authorization: Basic dXNlcjE6cGFzc3dvcmQx
    User-Agent: curl/7.47.0
    Accept: */*
    
    curl -u user1:password1 -u user2:password2 http://localhost:50505/
    
    GET / HTTP/1.1
    Host: localhost:50505
    Authorization: Basic dXNlcjI6cGFzc3dvcmQy
    User-Agent: curl/7.47.0
    Accept: */*
    
    curl -H 'Authorization: Basic dXNlcjE6cGFzc3dvcmQx' -H 'Authorization: Basic dXNlcjI6cGFzc3dvcmQy' http://localhost:50505/
    
    GET / HTTP/1.1
    Host: localhost:50505
    User-Agent: curl/7.47.0
    Accept: */*
    Authorization: Basic dXNlcjE6cGFzc3dvcmQx
    Authorization: Basic dXNlcjI6cGFzc3dvcmQy
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-18
      • 2021-01-25
      • 1970-01-01
      • 2021-06-20
      相关资源
      最近更新 更多