You can deploy a standard webapp in Liferay without making it a portlet.
If you do that, it is not so easy to get the currently logged in Liferay user in your standalone webapp.
There is another article which suggests reconstructing the user with the information the Liferay login cookie provides.
This solution has one blemish: It does only work when the user checks the “Remember me” box at the login screen. Otherwise the cookie won’t contain the password that is used in this approach for retrieving the user from Liferay.
I changed the solution mentioned above to only use the company key and the user id. This works even if the user did not check the “Remember me” box at the login screen.
I made this a servlet filter which can be easily used by registering it in your web.xml.
packagecom.codebrickie.filter;importjava.io.IOException;importjava.io.UnsupportedEncodingException;importjava.security.Key;importjavax.servlet.Filter;importjavax.servlet.FilterChain;importjavax.servlet.FilterConfig;importjavax.servlet.ServletException;importjavax.servlet.ServletRequest;importjavax.servlet.ServletResponse;importjavax.servlet.http.Cookie;importjavax.servlet.http.HttpServletRequest;importcom.liferay.portal.kernel.util.WebKeys;importcom.liferay.portal.model.Company;importcom.liferay.portal.model.User;importcom.liferay.portal.security.auth.PrincipalThreadLocal;importcom.liferay.portal.security.permission.PermissionChecker;importcom.liferay.portal.security.permission.PermissionCheckerFactoryUtil;importcom.liferay.portal.security.permission.PermissionThreadLocal;importcom.liferay.portal.service.CompanyLocalServiceUtil;importcom.liferay.portal.service.UserLocalServiceUtil;importcom.liferay.util.Encryptor;publicclassLiferayUserFilterimplementsFilter{@OverridepublicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainfilterChain)throwsIOException,ServletException{if(requestinstanceofHttpServletRequest){Cookie[]cookies=((HttpServletRequest)request).getCookies();StringuserId=null;StringcompanyId=null;if(cookies!=null){for(Cookiec:cookies){if("COMPANY_ID".equals(c.getName())){companyId=c.getValue();}elseif("ID".equals(c.getName())){userId=hexStringToStringByAscii(c.getValue());}}if(userId!=null&&companyId!=null){try{Companycompany=CompanyLocalServiceUtil.getCompany(Long.parseLong(companyId));Keykey=company.getKeyObj();StringuserIdPlain=Encryptor.decrypt(key,userId);UserliferayUser=UserLocalServiceUtil.getUser(Long.valueOf(userIdPlain));// Now you can set the liferayUser into a thread local for later use or // something like that.}catch(ExceptionpException){thrownewRuntimeException(pException);}}}}filterChain.doFilter(request,response);}privateStringhexStringToStringByAscii(StringhexString){byte[]bytes=newbyte[hexString.length()/2];for(inti=0;i<hexString.length()/2;i++){StringoneHexa=hexString.substring(i*2,i*2+2);bytes[i]=Byte.parseByte(oneHexa,16);}try{returnnewString(bytes,"ASCII");}catch(UnsupportedEncodingExceptione){thrownewRuntimeException(e);}}@Overridepublicvoiddestroy(){}@Overridepublicvoidinit(FilterConfigpArg0)throwsServletException{}}