Hi Everyone,
I am trying to login with the j_security_check through a Servlet. The below is the code I have in my Servlet.
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(“http://localhost:4502/content/tuhin/home/j_security_check”);
postMethod.addParameter(“j_username”, “tuhin”);
postMethod.addParameter(“j_password”, “tuhin”);
postMethod.addParameter(“_charset_”, “UTF-8”);
httpClient.executeMethod(postMethod);
Now this code is returning 302 status code. That means its not failing, otherwise it should have returned 403.
But at the same time I can see its not logging in with the intended user.
Am I doing something wrong? Or there is any more steps that I am missing ?
Thanks and Regards
Tuhin
Views
Replies
Total Likes
Hi Tuhin,
Can you please explain how you are validating the user is not logged in once execute is done? because httpclient call is to make request and get response as object (you can use it for further request as you want - its like connection) but it will not create/update existing user session which was created using AEM authentication process (for that you have extend that servlet)
Thanks,
Pawan
Hi Pawan,
Thanks for the reply. I am printing the logged in user id in the page to see which/what user is logged in.
My initial Idea was calling j_security_check servlet with proper parameters would log in the user. But apparently that is not the case. And as you mentioned it is not updating the existing session. Could you please let me know how I could programmatically log the user in? Any code snipped would be very helpful.
Thanks and Regards,
Tuhin
Hi Tuhin,
We did somewhat similar approach using angular, it worked.
As you said, you will see 302 code but in the same response are you the getting valid logged in user cookie which should set in your browser and then refresh page should work.
Thanks
Hi Praveen,
Yes I figured that 302 response is the correct one and this should set the login cookie and I should be seeing the user logged in. But that part is not happening for me. Since you have done something similar would it be ok for you to share some snippets ?
Thanks
Tuhin
Hi Tuhin,
It wasn't some hi-fi code, just basic angular http
var queryParam = "j_username=" + j_username +"&j_password=" + j_password +"&j_staysigned=on"; return $http({ method: "POST", url: "/content/project/en/home.html/j_security_check", data: queryParam, headers:{ 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' } });
I hope it helps
edubey wrote...
Hi Tuhin,
It wasn't some hi-fi code, just basic angular http
var queryParam = "j_username=" + j_username +"&j_password=" + j_password +"&j_staysigned=on";
return $http({
method: "POST",
url: "/content/project/en/home.html/j_security_check",
data: queryParam,
headers:{
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
});
I hope it helps
I am also trying the similar thing, as you could see from the code i have attached. But somehow after returning 302 the user is not getting logged in. :(
Hi Tuhin,
Just to add, we have our custom auth handler. Not user if this is a major difference. But as another solution which will work is having a hidden form. Fill form with user details and submit it via JS
<form id="login-form" class="ui form sign-in-form auto-signin-form hideme" novalidate method="POST" action="${currentPage.path}.html/j_security_check"> <input type="email" tabindex="1" name="j_username" placeholder="Email Address"> <input type="password" tabindex="2" name="j_password" placeholder="password"> <input type="checkbox" name="j_staysigned" tabindex="0" class="hidden" checked> </form> $("#login-form").submit();
Hi Tuhin,
I think I had a similar issue where it was giving 302 as a status code but it was not generating the login-token cookie for me. What I did to fix the issue was - I extracted the value of login-token from the response headers and created login-token cookie my self.
Once I did that, it was able to log the user in JCR correctly. To double-check I even open crx in a new tab and it did login the user I was programmatically trying to login to JCR.
Thanks,
Khanjan
Views
Likes
Replies
Views
Likes
Replies