gpt4 book ai didi

spring - 在Grails中模拟帖子-Spring身份验证

转载 作者:行者123 更新时间:2023-12-02 15:27:24 31 4
gpt4 key购买 nike

我正在尝试发布此标准spring url,以模拟身份验证:

http://www.whateverAddress.is:8080/Test/j_spring_security_check

如果使用插件海报,则效果很好,我放置了两个参数j_username和j_password以及 header 参数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 =“”;过期时间=星期四,格林尼治标准时间1970年1月1日00:00:10;路径= /测试,Set-Cookie:JSESSIONID = 247CF8B70D589F053DE4C937F2E16B9D;路径= /测试/; HttpOnly,位置: http://www.whateverAddress.is:8080/Test/login/authfail?ajax=true,内容长度:0,日期:星期二,2014年9月16日12:04:12 GMT]

就像我已被重定向,而不是使用代码200和json一样,这是代码302,如Poster插件所示。

更新
我创建了一个触发ajax发布请求的按钮,因为我应该能够从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端复制相同的 header ,则什么也不会发生。我想念什么

最近更新-关闭
基本上,发布完成后,我们需要使用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
}
}

最佳答案

您似乎正在使用android SDK进行调用。这就是我用来将数据发布到REST API(由Grails支持)的目的:

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 );

关于spring - 在Grails中模拟帖子-Spring身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25866144/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com