以编程方式使用 j_security_check
我有类似 localhost:7001/MyServlet 的页面。我正在发出如下所示的 http 连接请求,
String url = "http://localhost:7001/MyServlet"
PostMethod method = new PostMethod(url);
HttpClient client = new HttpClient();
但是“MyServlet”受 j_security_check 保护。因此,当我建立连接时,被重定向到登录页面。
如何在一个 HttpConnection 中进行身份验证并访问我的 url
注意:我使用 apache common httpclient
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
I have page like localhost:7001/MyServlet. I am making a http connection request from like below
String url = "http://localhost:7001/MyServlet"
PostMethod method = new PostMethod(url);
HttpClient client = new HttpClient();
However "MyServlet" is protected by j_security_check. So when I am making my connection , getting redirected to login page.
How to get authenticated and access my url , in one HttpConnection
Note: I use apache common httpclient
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑您是否可以在单个请求中登录并调用服务器,除非启用了 HTTP BASIC 身份验证。虽然我还不知道 HTTPClient 的 API 的详细信息,但基本上您需要使用 cookie 来跟踪会话;将您的登录信息发布到
/j_security_check
;然后访问servlet。 (如果使用 ACEGI Security,则相同的基本过程也适用于/j_acegi_security_check
。)Tomcat中的一个令人讨厌的问题是,直接发布到
/j_security_check
会给出 400“错误请求” ;它的验证器对状态转换相当挑剔,并且显然在设计时没有考虑到程序化客户端。您需要首先访问/loginEntry
(您可以丢弃除会话cookie之外的响应); 然后将您的登录信息发布到/j_security_check
; 然后遵循生成的重定向(我认为返回到/loginEntry
),这将实际存储您的新登录信息; 终于发布到所需的servlet! NetBeans #5c3cb7fb60fe 显示了使用 Tomcat 的容器身份验证登录到 Hudson 服务器的实际情况。I doubt you can log in and call the server in a single request, unless HTTP BASIC authentication is enabled. While I do not know the details of HTTPClient's API yet, basically you will need to track a session using cookies; POST your login to
/j_security_check
; then access the servlet. (The same basic process works for/j_acegi_security_check
if using ACEGI Security.)A nasty wrinkle in Tomcat is that just posting right away to
/j_security_check
gives a 400 "bad request"; its authenticator is rather finicky about state transitions and was clearly not designed with programmatic clients in mind. You need to first access/loginEntry
(you can throw away the response other than session cookies); then post your login information to/j_security_check
; then follow the resulting redirect (back to/loginEntry
I think) which will actually store your new login information; finally post to the desired servlet! NetBeans #5c3cb7fb60fe shows this in action logging in to a Hudson server using Tomcat's container authentication.