Below code will help you to login to the Liferay Portal using custom portlet or we can say custom logic
JSP Page :-
<portlet:actionURL var="createNewIndex" name="createNewIndex">
<portlet:param name="action" value="customLogin" />
</portlet:actionURL>
<form action="<%=createNewIndex %>" method="post">
Email: <input type="text" name="email"> </br>
Password: <input type="text" name="pasword"> </br>
<input type="submit" value="Submit">
</form>
Controller:-
package com.securtiy.sample;
import com.liferay.portal.CompanyMaxUsersException;
import com.liferay.portal.CookieNotSupportedException;
import com.liferay.portal.NoSuchUserException;
import com.liferay.portal.PasswordExpiredException;
import com.liferay.portal.UserEmailAddressException;
import com.liferay.portal.UserLockoutException;
import com.liferay.portal.UserPasswordException;
import com.liferay.portal.UserScreenNameException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.CompanyConstants;
import com.liferay.portal.security.auth.AuthException;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.bind.annotation.ActionMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;
@Controller
@RequestMapping("VIEW")
public class PortletViewController {
private static Log logger = LogFactoryUtil.getLog(PortletViewController.class);
private static final String NAMESPACE_SERVLET_REQUEST_FQCN = "com.liferay.portal.servlet.NamespaceServletRequest";
private String authType ="emailAddress";
private String handleLabel;
@RenderMapping
public String question(RenderRequest renderRequest, RenderResponse renderResponse, Model model){
System.out.println("Inside Render Method");
return "sample/view";
}
@ActionMapping(params = "action=createIndex")
public void authenticate(Model model,ActionRequest actionRequest,ActionResponse actionResponse) {
ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
HttpServletRequest httpServletRequest = PortalUtil.getHttpServletRequest(actionRequest);
System.out.println("---------- Testing for Log in ---------");
// If the request object is a wrapper that handles special namespacing considerations for portlet session
// attributes, then get the inner-most wrapped request. This will ensure that the following call to
// LoginUtil.login(...) will be able to work with a session that has an attribute map shared by the portal.
if (httpServletRequest.getClass().getName().equals(NAMESPACE_SERVLET_REQUEST_FQCN)) {
while (httpServletRequest instanceof HttpServletRequestWrapper) {
HttpServletRequestWrapper httpServletRequestWrapper = (HttpServletRequestWrapper) httpServletRequest;
httpServletRequest = (HttpServletRequest) httpServletRequestWrapper.getRequest();
}
}
HttpServletResponse httpServletResponse = PortalUtil.getHttpServletResponse(actionResponse);
String handle = "test@liferay.com"; // hardcode email for testing purpose
String password = "test"; // hardcoded password for testing purpose
boolean rememberMe = false; // made remember me check box as false
boolean authenticated = false; // boolean value to check authentication is true or false
try {
// Method which will use to perform the login
LoginUtilCompat.invokeLogin(httpServletRequest, httpServletResponse, handle, password, rememberMe,authType);
authenticated = true;
}
catch (Exception e) {
logger.error(e);
}
if (authenticated) {
System.out.println("Authentication is sucess");
/*
We must redirect the Page to particular location then only this method will work otherwise it will not work sometimes
try {
ExternalContext externalContext = liferayFacesContext.getExternalContext();
if (PropsValuesCompat.PORTAL_JAAS_ENABLE) {
externalContext.redirect(themeDisplay.getPathMain() + "/portal/protected");
}
else {
String redirect = ParamUtil.getString(actionRequest, "redirect");
if (Validator.isNotNull(redirect)) {
redirect = PortalUtilCompat.escapeRedirect(redirect);
if (!redirect.startsWith(Http.HTTP)) {
redirect = getCompleteRedirectURL(httpServletRequest, redirect);
}
externalContext.redirect(redirect);
}
else {
boolean doActionAfterLogin = ParamUtil.getBoolean(actionRequest, "doActionAfterLogin");
if (doActionAfterLogin) {
return;
}
else {
redirect = getCompleteRedirectURL(httpServletRequest, themeDisplay.getPathMain());
externalContext.redirect(redirect);
}
}
}
}
catch (IOException e) {
logger.error(e);
liferayFacesContext.addGlobalUnexpectedErrorMessage();
}
*/}
else {
System.out.println("Authentication Failed");
}
}
}
Util Class :
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.liferay.portal.kernel.util.ClassResolverUtil;
import com.liferay.portal.kernel.util.MethodKey;
import com.liferay.portal.kernel.util.PortalClassInvoker;
public class LoginUtilCompat {
// Private Constants
private static final String LOGIN_UTIL_FQCN = "com.liferay.portlet.login.util.LoginUtil";
private static final String LOGIN_METHOD = "login";
private static final Class[] LOGIN_PARAM_TYPES = new Class[] {
HttpServletRequest.class, HttpServletResponse.class, String.class, String.class, boolean.class, String.class
};
// Method which is used to Invoke the Login
public static Object invokeLogin(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
String handle, String password, boolean rememberMe, String authType) throws Exception {
Class loginUtilClass = ClassResolverUtil.resolveByPortalClassLoader(LOGIN_UTIL_FQCN);
MethodKey methodKey = new MethodKey(loginUtilClass, LOGIN_METHOD, LOGIN_PARAM_TYPES);
return PortalClassInvoker.invoke(false, methodKey, httpServletRequest, httpServletResponse, handle, password,
rememberMe, authType);
}
}
JSP Page :-
<portlet:actionURL var="createNewIndex" name="createNewIndex">
<portlet:param name="action" value="customLogin" />
</portlet:actionURL>
<form action="<%=createNewIndex %>" method="post">
Email: <input type="text" name="email"> </br>
Password: <input type="text" name="pasword"> </br>
<input type="submit" value="Submit">
</form>
Controller:-
package com.securtiy.sample;
import com.liferay.portal.CompanyMaxUsersException;
import com.liferay.portal.CookieNotSupportedException;
import com.liferay.portal.NoSuchUserException;
import com.liferay.portal.PasswordExpiredException;
import com.liferay.portal.UserEmailAddressException;
import com.liferay.portal.UserLockoutException;
import com.liferay.portal.UserPasswordException;
import com.liferay.portal.UserScreenNameException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.CompanyConstants;
import com.liferay.portal.security.auth.AuthException;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.bind.annotation.ActionMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;
@Controller
@RequestMapping("VIEW")
public class PortletViewController {
private static Log logger = LogFactoryUtil.getLog(PortletViewController.class);
private static final String NAMESPACE_SERVLET_REQUEST_FQCN = "com.liferay.portal.servlet.NamespaceServletRequest";
private String authType ="emailAddress";
private String handleLabel;
@RenderMapping
public String question(RenderRequest renderRequest, RenderResponse renderResponse, Model model){
System.out.println("Inside Render Method");
return "sample/view";
}
@ActionMapping(params = "action=createIndex")
public void authenticate(Model model,ActionRequest actionRequest,ActionResponse actionResponse) {
ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
HttpServletRequest httpServletRequest = PortalUtil.getHttpServletRequest(actionRequest);
System.out.println("---------- Testing for Log in ---------");
// If the request object is a wrapper that handles special namespacing considerations for portlet session
// attributes, then get the inner-most wrapped request. This will ensure that the following call to
// LoginUtil.login(...) will be able to work with a session that has an attribute map shared by the portal.
if (httpServletRequest.getClass().getName().equals(NAMESPACE_SERVLET_REQUEST_FQCN)) {
while (httpServletRequest instanceof HttpServletRequestWrapper) {
HttpServletRequestWrapper httpServletRequestWrapper = (HttpServletRequestWrapper) httpServletRequest;
httpServletRequest = (HttpServletRequest) httpServletRequestWrapper.getRequest();
}
}
HttpServletResponse httpServletResponse = PortalUtil.getHttpServletResponse(actionResponse);
String handle = "test@liferay.com"; // hardcode email for testing purpose
String password = "test"; // hardcoded password for testing purpose
boolean rememberMe = false; // made remember me check box as false
boolean authenticated = false; // boolean value to check authentication is true or false
try {
// Method which will use to perform the login
LoginUtilCompat.invokeLogin(httpServletRequest, httpServletResponse, handle, password, rememberMe,authType);
authenticated = true;
}
catch (Exception e) {
logger.error(e);
}
if (authenticated) {
System.out.println("Authentication is sucess");
/*
We must redirect the Page to particular location then only this method will work otherwise it will not work sometimes
try {
ExternalContext externalContext = liferayFacesContext.getExternalContext();
if (PropsValuesCompat.PORTAL_JAAS_ENABLE) {
externalContext.redirect(themeDisplay.getPathMain() + "/portal/protected");
}
else {
String redirect = ParamUtil.getString(actionRequest, "redirect");
if (Validator.isNotNull(redirect)) {
redirect = PortalUtilCompat.escapeRedirect(redirect);
if (!redirect.startsWith(Http.HTTP)) {
redirect = getCompleteRedirectURL(httpServletRequest, redirect);
}
externalContext.redirect(redirect);
}
else {
boolean doActionAfterLogin = ParamUtil.getBoolean(actionRequest, "doActionAfterLogin");
if (doActionAfterLogin) {
return;
}
else {
redirect = getCompleteRedirectURL(httpServletRequest, themeDisplay.getPathMain());
externalContext.redirect(redirect);
}
}
}
}
catch (IOException e) {
logger.error(e);
liferayFacesContext.addGlobalUnexpectedErrorMessage();
}
*/}
else {
System.out.println("Authentication Failed");
}
}
}
Util Class :
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.liferay.portal.kernel.util.ClassResolverUtil;
import com.liferay.portal.kernel.util.MethodKey;
import com.liferay.portal.kernel.util.PortalClassInvoker;
public class LoginUtilCompat {
// Private Constants
private static final String LOGIN_UTIL_FQCN = "com.liferay.portlet.login.util.LoginUtil";
private static final String LOGIN_METHOD = "login";
private static final Class[] LOGIN_PARAM_TYPES = new Class[] {
HttpServletRequest.class, HttpServletResponse.class, String.class, String.class, boolean.class, String.class
};
// Method which is used to Invoke the Login
public static Object invokeLogin(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
String handle, String password, boolean rememberMe, String authType) throws Exception {
Class loginUtilClass = ClassResolverUtil.resolveByPortalClassLoader(LOGIN_UTIL_FQCN);
MethodKey methodKey = new MethodKey(loginUtilClass, LOGIN_METHOD, LOGIN_PARAM_TYPES);
return PortalClassInvoker.invoke(false, methodKey, httpServletRequest, httpServletResponse, handle, password,
rememberMe, authType);
}
}
No comments:
Post a Comment