Search code examples
authenticationjakarta-eeliferayhookliferay-hook

authentication in liferay without login hook


I have a problem, I get the user and password of a view and I check if this data is correct in the data of liferay, when it's correct my method return 1 if the validation is true, but I don't know how to make the successful login in liferay, this is my method:

try {
        long companyId = PortalUtil.getDefaultCompanyId();
        System.out.println(companyId + " id company");
        User user1;
        try {
            user1 = UserLocalServiceUtil.getUserByEmailAddress(companyId,     name);
            long cmp = user1.getCompanyId();
            Company company = CompanyLocalServiceUtil.getCompany(cmp);
            int a =      UserLocalServiceUtil.authenticateByUserId(company.getCompanyId(), user.getId(), pass, null,
                    null, null);
            if (a == 1) {

                System.out.println("Log in successful");

            }
        } catch (PortalException e) {
            e.printStackTrace();
        } catch (SystemException e) {
            e.printStackTrace();
        }
    } catch (Exception e) {
        System.out.println("algo salio mal");
    }

Solution

  • This seems to be a case where you would need an auto-login hook. In Liferay 7, you just need components like in: https://www.e-systems.tech/blog/-/blogs/autologin-in-liferay-7

    You can use an indicator within the user session, like a token, and check it in a custom logic:

    @Override
    protected String[] doLogin(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    
        final long companyId = portal.getCompanyId(request);
        final HttpSession session = request.getSession();
    
            // code your logic here..
    
        final String[] credentials = new String[3];
    
        credentials[0] = String.valueOf(user.getUserId());
        credentials[1] = user.getPassword();
        credentials[2] = Boolean.FALSE.toString();
    
        return credentials;
    }
    

    This solution is also valid for LR6, the difference is that you are not using OSGi there, so you have to create a hook through the SDK.