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