Use an interceptor to check whether a user logs on

Source: Internet
Author: User

In any system, there will be a logon interface, because only authenticated users can access resources on the network. In table applications, there is only one portal for the system, that is the main function, but in the B/s structure, we can request any page from the server, that is, there are multiple entries, so how can we restrict users from logging on to the system only through this interface? There are many implementation methods. Now we introduce a common method to use the interceptor in structs2:
 
First, we define an interceptor:
[Java]
Public class MyInterceptor implements Interceptor
{
 
Public void destroy ()
{
// TODO Auto-generated method stub
System. out. println ("Interceptor destroyed! ");
 
}
 
Public void init ()
{
// TODO Auto-generated method stub
System. out. println ("Interceptor initialization ");
 
}
 
Public String intercept (ActionInvocation invocation) throws Exception
{
// TODO Auto-generated method stub
System. out. println ("the interceptor starts verification! ");
If (LoginAction. class = invocation. getAction (). getClass ())
{
Return invocation. invoke ();
}
User user = (User) invocation. getInvocationContext (). getSession (). get ("user ");
If (user = null)
Return "fail ";
System. out. println ("user logged on ");
Return invocation. invoke ();
}
 
}

Configure the interceptor in struts. xml.
[Html]
<Interceptors>
<Interceptor name = "login" class = "com. interceptor. MyInterceptor"> </interceptor>
<Interceptor-stack name = "mystack">
<Interceptor-ref name = "login"> </interceptor-ref>
<Interceptor-ref name = "defaultStack"> </interceptor-ref>
</Interceptor-stack>
</Interceptors>
<Default-interceptor-ref name = "mystack"> </default-interceptor-ref>

In this way, if the user requests the login page, it can pass smoothly. If the user requests other pages, it needs to check whether the user has logged on. If the user does not log on, it will jump back to the login page.


Author: yuanzeyao2008

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.