Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Dhruv Pal
If user is already login and he manually enters login page url?
February 7, 2013 2:51 AM
Answer

Dhruv Pal

Rank: Junior Member

Posts: 82

Join Date: January 23, 2013

Recent Posts

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
RE: If user is already login and he manually enters login page url?
March 12, 2013 9:56 PM
Answer

Dhruv Pal

Rank: Junior Member

Posts: 82

Join Date: January 23, 2013

Recent Posts

Just to help i did it by using Filters.If anybody needs help let me know.
Manish Yadav
RE: If user is already login and he manually enters login page url?
March 13, 2013 12:04 AM
Answer

Manish Yadav

Rank: Expert

Posts: 467

Join Date: May 26, 2012

Recent Posts

it would be great if you share any sample code
Dhruv Pal
RE: If user is already login and he manually enters login page url?
March 13, 2013 12:33 AM
Answer

Dhruv Pal

Rank: Junior Member

Posts: 82

Join Date: January 23, 2013

Recent Posts

ok first i created a hook

 1
 2<hook>
 3    <servlet-filter-mapping>
 4        <servlet-filter-name>Login Filter</servlet-filter-name>
 5         <before-filter>SSO Open SSO Filter</before-filter>
 6         <!--
 7           <url-pattern>/web/*</url-pattern>
 8           <url-pattern>/user/*</url-pattern>
 9           <url-pattern>*.jsp</url-pattern>
10               <url-pattern>/group/*</url-pattern>
11         -->
12        <url-pattern>/web/test/login</url-pattern>
13         <dispatcher>REQUEST</dispatcher>
14        <dispatcher>FORWARD</dispatcher>
15    </servlet-filter-mapping>
16<hook>


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

 1
 2public class Filter implements javax.servlet.Filter
 3{
 4    public static String HOME_URL = null;
 5    public static String LOGIN_URL = ??
 6     public static String GROUP_URL=??;
 7
 8    private String _hello;
 9    /**
10     * Default constructor.
11     */
12    public Filter()
13    {
14        // TODO Auto-generated constructor stub
15    }
16
17    /**
18     * @see Filter#destroy()
19     */
20    public void destroy()
21    {
22        // TODO Auto-generated method stub
23        System.out.println("SampleFilter.destroy()");
24    }
25
26   
27   
28    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException
29    {
30        String current_uri=null;
31        String user_id = null;
32        HttpServletResponse res = null;
33        HttpServletRequest req = null;
34       
35   
36       
37        try{
38            user_id= servletRequest.getAttribute("USER_ID").toString();
39        }
40        catch(Exception e)
41        {
42            System.err.println("error user id is null");
43        }
44       
45        current_uri=((HttpServletRequest)servletRequest).getRequestURI();
46        res = (HttpServletResponse)servletResponse;
47        req = (HttpServletRequest)servletRequest;
48
49        System.out.println("User id is        : "+user_id);
50        System.out.println("current uri is    : "+current_uri);
51        System.out.println("login uri is      : "+LOGIN_URL);
52        if(user_id!=null)
53        {
54            if(current_uri.equals(LOGIN_URL))
55            {
56                HOME_URL = req.getScheme()+"://"+req.getHeader("host")+"/group/test/home";
57                System.out.println(" in filter redirecting to home page ");
58                System.out.println(">>>>>>>> "+HOME_URL);
59                res.sendRedirect(HOME_URL);
60            }
61        }
62        else if(user_id==null)
63        {
64            if(current_uri.contains("/group/"))
65            {
66              System.out.println("System. redirect to login page ");   
67               res.sendRedirect(LOGIN_URL);
68            }
69        }
70       
71        filterChain.doFilter(servletRequest, servletResponse);
72    }
73
74    @Override
75    public void init(FilterConfig arg0) throws ServletException
76    {
77        // TODO Auto-generated method stub
78    }
79
80
81    /**
82     * @see Filter#init(FilterConfig)
83     */
84
85}



and after you login and hit url you can see Filter class is called dependin on your url pattern
Rahul Sharma
RE: If user is already login and he manually enters login page url?
March 13, 2013 3:18 AM
Answer

Rahul Sharma

Rank: Junior Member

Posts: 60

Join Date: January 10, 2012

Recent Posts

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.
Enrique Valdes Lacasa
RE: If user is already login and he manually enters login page url?
February 24, 2015 12:15 PM
Answer

Enrique Valdes Lacasa

Rank: Junior Member

Posts: 69

Join Date: July 29, 2014

Recent Posts

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:
1user_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:
1user_id = PortalUtil.getUser(req).getUserId();
Enrique Valdes Lacasa
RE: If user is already login and he manually enters login page url?
April 23, 2015 12:14 PM
Answer

Enrique Valdes Lacasa

Rank: Junior Member

Posts: 69

Join Date: July 29, 2014

Recent Posts

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!
Vishal Kumar
RE: If user is already login and he manually enters login page url?
April 24, 2015 3:57 AM
Answer

Vishal Kumar

Rank: Regular Member

Posts: 198

Join Date: December 11, 2012

Recent Posts

Thanks Enrique.
Enrique Valdes Lacasa
RE: If user is already login and he manually enters login page url?
April 27, 2015 8:05 AM
Answer

Enrique Valdes Lacasa

Rank: Junior Member

Posts: 69

Join Date: July 29, 2014

Recent Posts

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.