我试图对此标准的春季网址发帖,以模拟身份验证:
http://www.whateverAddress.is:8080/Test/j_spring_security_check
如果我使用插件海报它工作正常,我把两个参数j_username和j_password和一个头参数X-Requested-With = XMLHttpRequest,实际上我得到了一个json结果。
如果我尝试用Java中的代码来做这件事,Google中的大量示例似乎无法运行。目前我正在使用此代码,但我得不到任何回报:
HttpClient httpClient = new DefaultHttpClient();
List<NameValuePair> postParams = new ArrayList<NameValuePair>();
postParams.add(new BasicNameValuePair("j_username", "username"));
postParams.add(new BasicNameValuePair("j_password", "password"))
HttpPost httpPost = new HttpPost("http://www.whateverAddress.is:8080/Test/j_spring_security_check");
httpPost.setEntity(new UrlEncodedFormEntity(postParams, HTTP.UTF_8));
httpPost.addHeader("X-Requested-With", "XMLHttpRequest");
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");
System.out.println(responseString);
如果我打印回复,(不是实体)我有以下内容:
发现HTTP / 1.1 302 [服务器:Apache-Coyote / 1.1,Set-Cookie:grails_remember_me =&#34;&#34 ;; Expires = Thu,01-Jan-1970 00:00:10 GMT; Path = / Test,Set-Cookie:JSESSIONID = 247CF8B70D589F053DE4C937F2E16B9D;路径= /测试/; HttpOnly,位置:http://www.whateverAddress.is:8080/Test/login/authfail?ajax=true,内容长度:0,日期:星期二,2014年9月16日12:04:12 GMT]
代码302,就像我被重定向而不是代码200和json一样,正如Poster插件显示的那样。
更新 我创建了一个启动ajax post请求的按钮,因为我应该可以从java代码端执行,这里是代码:
$(function() {
$('#btnPush').on('click', function() {
$.ajax({
url: 'http://www.whateverAddress.is:8080/Test/j_spring_security_check',
type: 'POST',
data: {
j_username: 'username',
j_password: 'password'
},
success: function(data, textStatus, jqXHR) {
alert('Success!!!');
},
error: function(jqXHR, textStatus, errorThrown) {
alert('Fail!');
}
});
});
});
查看发送的请求,我看到标题和收到的JSON,一切都很好。如果我在java端复制相同的头仍然没有任何反应。我想念一些东西
最后更新 - 关闭 基本上,一旦完成帖子,我们需要使用auth cookie跟踪重定向以获取正确的信息:
def headers = ["X-Requested-With": "XMLHttpRequest"]
def postParams = ["j_username": user, "j_password": pwd]
def url = "http://www.whatever.is:8080/test/j_spring_security_check"
def returnstring = doHttpPost(url, postParams, headers)
private String doHttpPost(String url, Map<String, String> params, Map<String, String> headers) {
HttpClient client = new DefaultHttpClient()
HttpPost post = new HttpPost(url)
String result
try {
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>()
headers?.each {
post.setHeader(it.key, it.value)
}
params?.each {
urlParameters.add(new BasicNameValuePair(it.key, it.value))
}
if (params) {
post.setEntity(new UrlEncodedFormEntity(urlParameters))
}
HttpResponse response = client.execute(post)
if (response.getStatusLine()?.getStatusCode() == 200) {
result = response.getEntity().getContent()?.text
}
if (response.getStatusLine()?.getStatusCode() == 302) {
String redirectUrl = response.getHeaders("Location")?.value[0]
result = doHttpGet(redirectUrl, ["Cookie": response.getHeaders("Set-Cookie")?.value[0]])
}
} catch (Exception ex) {
log.error(ex)
} finally {
post.getEntity().consumeContent()
return result
}
}
答案 0 :(得分:1)
HttpClient httpClient = getHttpClient();
List<NameValuePair> postParams = new ArrayList<NameValuePair>();
postParams.add( new BasicNameValuePair( "j_username", "username" ) );
postParams.add( new BasicNameValuePair( "j_password", "password" ) )
HttpPost httppost = new HttpPost( url );
httppost.setEntity( new UrlEncodedFormEntity( postParams, HTTP.UTF_8 ) );
httpPost.addHeader( "X-Requested-With", "XMLHttpRequest" );
HttpResponse hr = httpClient.execute( httppost );
JSONObject res = readStreamUsingReader( hr );