Fórum

If user is already login and he manually enters login page url?

Dhruv Pal, modificado 11 Anos atrás.

If user is already login and he manually enters login page url?

Junior Member Postagens: 82 Data de Entrada: 24/01/13 Postagens Recentes
Hi all

I need that if a user is already login and he manually enters login path he redirects to home page?.Please let me know how to how to perform this.
Dhruv Pal, modificado 11 Anos atrás.

RE: If user is already login and he manually enters login page url?

Junior Member Postagens: 82 Data de Entrada: 24/01/13 Postagens Recentes
Just to help i did it by using Filters.If anybody needs help let me know.
thumbnail
Manish Yadav, modificado 11 Anos atrás.

RE: If user is already login and he manually enters login page url?

Expert Postagens: 493 Data de Entrada: 26/05/12 Postagens Recentes
it would be great if you share any sample code
Dhruv Pal, modificado 11 Anos atrás.

RE: If user is already login and he manually enters login page url?

Junior Member Postagens: 82 Data de Entrada: 24/01/13 Postagens Recentes
ok first i created a hook


<hook>
	<servlet-filter-mapping>
		<servlet-filter-name>Login Filter</servlet-filter-name>
		 <before-filter>SSO Open SSO Filter</before-filter> 
		 <!--
		   <url-pattern>/web/*</url-pattern> 
		   <url-pattern>/user/*</url-pattern>
		   <url-pattern>*.jsp</url-pattern>
	   		<url-pattern>/group/*</url-pattern>
		 -->
		<url-pattern>/web/test/login</url-pattern>
 		<dispatcher>REQUEST</dispatcher>
		<dispatcher>FORWARD</dispatcher>
	</servlet-filter-mapping>
<hook>
</hook></hook>


i created custom login so i only checked for login page
then a class like this


public class Filter implements javax.servlet.Filter 
{
	public static String HOME_URL = null;
	public static String LOGIN_URL = ??
	 public static String GROUP_URL=??;

	private String _hello;
	/**
	 * Default constructor. 
	 */
	public Filter() 
	{
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() 
	{
		// TODO Auto-generated method stub
		System.out.println("SampleFilter.destroy()");
	}

	
	
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException 
	{
		String current_uri=null;
		String user_id = null;
		HttpServletResponse res = null;
		HttpServletRequest req = null;
		
	
		
		try{
			user_id= servletRequest.getAttribute("USER_ID").toString();
		}
		catch(Exception e)
		{
			System.err.println("error user id is null");
		}
		
		current_uri=((HttpServletRequest)servletRequest).getRequestURI();
		res = (HttpServletResponse)servletResponse;
		req = (HttpServletRequest)servletRequest;

		System.out.println("User id is        : "+user_id);
		System.out.println("current uri is    : "+current_uri);
		System.out.println("login uri is      : "+LOGIN_URL);
		if(user_id!=null)
		{
			if(current_uri.equals(LOGIN_URL))
			{
				HOME_URL = req.getScheme()+"://"+req.getHeader("host")+"/group/test/home";
				System.out.println(" in filter redirecting to home page ");
				System.out.println("&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; "+HOME_URL);
				res.sendRedirect(HOME_URL);
			}
		}
		else if(user_id==null)
		{
			if(current_uri.contains("/group/"))
			{
			  System.out.println("System. redirect to login page ");	
			   res.sendRedirect(LOGIN_URL);
			}
		}
		
		filterChain.doFilter(servletRequest, servletResponse);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException 
	{
		// TODO Auto-generated method stub
	}


	/**
	 * @see Filter#init(FilterConfig)
	 */

}



and after you login and hit url you can see Filter class is called dependin on your url pattern
thumbnail
Enrique Valdes Lacasa, modificado 9 Anos atrás.

RE: If user is already login and he manually enters login page url?

Junior Member Postagens: 92 Data de Entrada: 29/07/14 Postagens Recentes
Thank you SO MUCH for that code Dhruv Pal. Quite interesting to implement a servlet filter hook mapping to specific URLs. I wanted to do the same thing that this post was asking for, so it saved me a lot of time.

I used your code to implement it and wanted to point out a couple of details that I ran into, which might help someone in the same situation:

1) First of all, if you use the code and see an IllegalStateException being thrown, the reason is probably due to this: java.lang.IllegalStateException: Cannot forward after response has been committed. Try adding return; after the sendRedirect()s. That fixed it in my case.

2) On the other hand, I am not sure why ...but when I used this call:
user_id= servletRequest.getAttribute("USER_ID").toString();
The user_id was null sometimes even if the user was logged in. Maybe it has to do with retrieving the user from the servletRequest. Not sure why, it would be nice to know. Instead, I use this method call using HttpServletRequest which is working in my case:
user_id = PortalUtil.getUser(req).getUserId();
Rahul Sharma, modificado 11 Anos atrás.

RE: If user is already login and he manually enters login page url?

Junior Member Postagens: 59 Data de Entrada: 11/01/12 Postagens Recentes
Hi Dhruv,

You can also try to achieve this by going to control panel ->under 'Portal' category->click Setting tab->goto general setting option set the home page url in the box named "Home URL" which is present in Navigation setting column.
thumbnail
Enrique Valdes Lacasa, modificado 9 Anos atrás.

RE: If user is already login and he manually enters login page url?

Junior Member Postagens: 92 Data de Entrada: 29/07/14 Postagens Recentes
I highly recommend checking out this Stack Overflow question to implement the functionality described in this post.
Servlet Redirection Filter
They really do a good job explaining how to implement the filter, but it is also a good link in case that anyone is interested in learning more about Servlet Filters.

So here is the thing thoug: I am using the filter, but came up with a problem. Somehow, the LDAP or NTLM authentication needed for the Portal I am working with is not working...and I am thinking that maybe is because of this Filter. First of all, I noticed that Liferay calls many filters (some of which can be deactivated for performance purposes). Secondly, I noticed that liferay-hook.xml can specify the name of the filter that gets called after or before iit with the tags <after-filter> or <before-filter>. And finally, I notice that the server have the Liferay filters for LDAP or NTLM disabled in the portal properties...which make me think that well...that is why it might not be working!

As far as I understand, the liferay-web.xml in ROOT presents the order of the filters that get called defined by their list in order using the filter-mapping tags (in where the LDAP and NTLM filters are also being called). So I am running into several questions that I would appreciate if someone could answer:

1) Can the problem of LDAP and NTLM not working be related to the Redirection Servlet Filter hook I created?
2) Should the LDAP and NTLM filters be enabled in the properties so that the authentication works properly?
3) Do I need to specify via the <before-filter> or <after-filter> tags a precise filter in the chain of filters where I need to call the redirection filter? and about this last question, if someone could suggest where in the filter chain would be the best place for us to add this redirection filter, that would be great.

Thanks a lot!
thumbnail
Vishal Kumar, modificado 9 Anos atrás.

RE: If user is already login and he manually enters login page url?

Regular Member Postagens: 198 Data de Entrada: 12/12/12 Postagens Recentes
Thanks Enrique.
thumbnail
Enrique Valdes Lacasa, modificado 9 Anos atrás.

RE: If user is already login and he manually enters login page url?

Junior Member Postagens: 92 Data de Entrada: 29/07/14 Postagens Recentes
You are welcome Vishal. By the way, I ended up adding in my liferay-hook.xml the tag <before-filter>Valid Host Name Filter</before-filter> . Liferay 6.2.

I did some debugging of Liferay requests and noticed that there is an instance of the InvokerFilterChain class that has an attribute called _filters. Well, this _filters is an array list that contains all the enabled filters to be called by Liferay in order (I checked it together with liferay-web.xml as well).

I noticed that the first filter in the list is Valid Host Name filter, so I felt that since our filter needs to redirect depending on the URL, the sooner it is called the better. But maybe someone has a better suggestion at this point though.