Forums de discussion

Getting logged user in a servlet

thumbnail
Robert Kákoš, modifié il y a 9 années.

Getting logged user in a servlet

New Member Publications: 5 Date d'inscription: 13/09/11 Publications récentes
Hi, I need to get the logged user in a servlet (within a portlet plugin), but PortalUtil.getUser(request) returns null everytime. I searched over internet and I found an article saying that it should be done via a filter.
So, my idea is to create a filter to handle a specific URL, detect current user and put him into the session, and then process the request in the servlet. Is this a good approach? If so, where in the filter chain my filter should be injected so that I can obtain current user?
Thanks.
thumbnail
David H Nebinger, modifié il y a 9 années.

RE: Getting logged user in a servlet

Liferay Legend Publications: 14917 Date d'inscription: 02/09/06 Publications récentes
If it's not available to the servlet code, I highly doubt it would be available in a filter.

Did you try the request.getRemoteUser() or getUserPrincipal()?
thumbnail
Jitendra Rajput, modifié il y a 9 années.

RE: Getting logged user in a servlet

Liferay Master Publications: 875 Date d'inscription: 07/01/11 Publications récentes
Try PrincipalThreadLocal to get logged in user's userId

PrincipalThreadLocal.getName();
thumbnail
David H Nebinger, modifié il y a 9 années.

RE: Getting logged user in a servlet

Liferay Legend Publications: 14917 Date d'inscription: 02/09/06 Publications récentes
Jitendra Rajput:
Try PrincipalThreadLocal to get logged in user's userId

PrincipalThreadLocal.getName();


That won't work. The thread used to service the servlet request is going to be different than the thread assigned to handle the Liferay request. Liferay servlet filters assign the PrincipalThreadLocal value and that makes it available to all of the portlets since that thread is going to be rendering the content. But you're in a completely different war in a completely different thread, so it just won't be available or valid.
thumbnail
Robert Kákoš, modifié il y a 9 années.

RE: Getting logged user in a servlet

New Member Publications: 5 Date d'inscription: 13/09/11 Publications récentes
Thanks for your help guys, finally I found out that I can do everything I need in my filter. The problem I had was the combination of <url-pattern>/article/*</url-pattern> and requested URL (http://localhost:8080/article?resourcePrimKey=12345).
When I changed the url-pattern to <url-pattern>/web/guest/article</url-pattern>, my filter intercepts requests going to URL http://localhost:8080/web/guest/article?resourcePrimKey=12345 and PortalUtil.getUser(request) returns current user now.