spring security - Android RestTemplate j_spring_security_check not redirecting -
in android app i'm working on using resttemplate post request log in (spring security):
resttemplate resttemplate = new resttemplate(); httpheaders headers = new httpheaders(); headers.add("j_username", uname); headers.add("j_password", password); headers.add("submit", "login"); httpentity<string> entity = new httpentity<string>(null, headers); responseentity<string> reply = resttemplate.exchange(baseurl + "/j_spring_security_check", httpmethod.post, entity, string.class);
the reply:
<302 found,{date=[wed, 30 sep 2015 08:10:15 gmt], server=[apache-coyote/1.1], location=[http://192.168.100.81/rest/loginstatus;jsessionid=a63589bf9985296c42a8fc49c858f2aa], content-length=[0], set-cookie=[jsessionid=a63589bf9985296c42a8fc49c858f2aa; path=/], keep-alive=[timeout=5, max=100], connection=[keep-alive], x-android-sent-millis=[1443600616746], x-android-received-millis=[1443600616756]}>
as can see, supposed redirect location, it's not. understand default behaviour redirect on requests.
how can make request follow redirect? i've seen lot of solutions using apache's httpclient, removed in api 23.
i guess workaround post request redirect location myself, hope there better solution.
to make resttemplate
follow redirects:
resttemplate resttemplate = new resttemplate(new simpleclienthttprequestfactory() { @override protected void prepareconnection(httpurlconnection connection, string httpmethod) throws ioexception { super.prepareconnection(connection, httpmethod); connection.setinstancefollowredirects(true); } });
update: construct httpentity
wrong, j_username
etc not headers. want:
multivaluemap<string, string> form = new linkedmultivaluemap<string, string>(); form.add("j_username", uname); form.add("j_password", password); httpentity<multivaluemap<string, string>> entity = new httpentity<multivaluemap<string, string>>(form, null);
Comments
Post a Comment