通过 wget 进行 Hudson 身份验证返回 http 错误 302

通过 wget 进行 Hudson 身份验证返回 http 错误 302

我正在尝试编写一个脚本,使用 wget 在 hudson 中进行身份验证并存储身份验证 cookie。

脚本的内容如下:

wget \
--no-check-certificate \
--save-cookies /home/hudson/hudson-authentication-cookie \
--output-document "-" \
'https://myhudsonserver:8443/hudson/j_acegi_security_check?j_username=my_username&j_password=my_password&remember_me=true'

不幸的是,当我运行该脚本时,我得到:

--2011-02-03 13:39:29--  https://myhudsonserver:8443/hudson/j_acegi_security_check?     j_username=my_username&j_password=my_password&remember_me=true
Resolving myhudsonserver... 127.0.0.1
Connecting to myhudsonserver|127.0.0.1|:8443... connected.
WARNING: cannot verify myhudsonserver's certificate, issued by `/C=Unknown/ST=Unknown/L=Unknown/O=Unknown/OU=Unknown/CN=myhudsonserver':
  Self-signed certificate encountered.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: https://myhudson:8443/hudson/;jsessionid=087BD0B52C7A711E0AD7B8BD4B47585F    [following]
--2011-02-03 13:39:29--      https://myhudsonserver:8443/hudson/;jsessionid=087BD0B52C7A711E0AD7B8BD4B47585F
Reusing existing connection to myhudsonserver:8443.
HTTP request sent, awaiting response... 404 Not Found
2011-02-03 13:39:29 ERROR 404: Not Found.

hudson 的任何 tomcat 日志文件中都没有错误日志。

有谁知道可能会发生什么吗?

谢谢。

答案1

以下是使用 curl 的等效操作:

curl --cookie-jar /home/hudson/hudson-authentication-cookie \
     --data-urlencode "j_username=my_username" \
     --data-urlencode "j_password=my_password" \
     --data-urlencode "remember_me=true" \
     --insecure https://myhudsonserver:8443/hudson/j_acegi_security_check

这应该可行,但根据您真正需要做的事情,可能会有更好的方法。

特别是如果您只需要一个查询,请在表单数据中包含“from=/hudson/...”,以便 302 重定向指向您真正感兴趣的资源。如果您添加 --location,curl 将遵循 302 重定向并使用 cookie(遗憾的是,仍然需要 --cookie-jar)。这是一个很好的单行代码:

curl --cookie-jar /home/hudson/hudson-authentication-cookie \
     --data-urlencode "j_username=my_username" \
     --data-urlencode "j_password=my_password" \
     --data-urlencode "from=/hudson/view/myview/api/json" \
     --insecure --location https://myhudsonserver:8443/hudson/j_acegi_security_check

相关内容