Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.

Not able to Login from a Servlet using j_security_check

Avatar

Level 7

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

9 Replies

Avatar

Level 7
        Hi Scott, I have seen this code. In there the code present is inside a jsp doing a post call from a HTML form and that works just fine. But I am trying to achieve the same inside from a servlet. I have imitated the same code in the servlet, see above. But its not working.

Avatar

Level 9

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

Avatar

Level 7

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

Avatar

Level 10

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

Avatar

Level 7

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

Avatar

Level 10

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

Avatar

Level 7

edubey wrote...

Hi Tuhin,

It wasn't some hi-fi code, just basic angular http

  1. var queryParam = "j_username=" + j_username +"&j_password=" + j_password +"&j_staysigned=on";
  2.  
  3. return $http({
  4. method: "POST",
  5. url: "/content/project/en/home.html/j_security_check",
  6. data: queryParam,
  7. headers:{
  8. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  9. 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
  10. }
  11. });

 

 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. :(

Avatar

Level 10

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

Avatar

Level 1

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