Some time ago, the project used the struts Interceptor to implement the login verification function, authorizationinterceptor. java.
Package Com. interceptor;
Import COM. opensymphony. xwork2.actioninvocation;
Import COM. opensymphony. xwork2.interceptor. *;
Import COM. opensymphony. xwork2. *;
Import JAVA. util. *;
/**
* @ author http://www.cnblogs.com/jobscn/
*/
Public class authorizationinterceptor extends extends actinterceptor {
private string ignoreactions;
//Getter method of the ignoreactions attribute
PublicString getignoreactios (){
ReturnIgnoreactions;
}
//Setter method of the ignoreactions attribute
Public VoidSetignoreactions (string ignoreactions ){
This. Ignoreactions = ignoreactions;
}
@ Override
PublicString intercept (actioninvocation Invocation)ThrowsException {
Actioncontext CTX = invocation. getinvocationcontext ();
Map session = CTX. getsession ();
String user = (string) Session. Get ("username ");
Boolean ignore = false ;
string currentaction = invocation. getproxy (). getactionname ();
string [] actions = ignoreactions. split (",");
For(String action: Actions ){
If(Currentaction. Matches (action. Trim ())){
Ignore =True;
Break;
}
}
If(User! =Null| Ignore =True){
ReturnInvocation. Invoke ();
}Else{
ReturnAction. login;
}
}
} Configure the interceptor in the Struts. xml file: < Interceptors >
< Interceptor Name = "Authorization" Class = "Com. Interceptor. authorizationinterceptor" />
< Interceptor-Stack Name = "Mystack" >
< Interceptor-ref Name = "Authorization" >
< Param Name = "Ignoreactions" > Validate_code, register. *,. * login. *, upload, Connector </ Param >
</ Interceptor-ref > < Interceptor-ref Name = "Defaultstack" />
</ Interceptor-Stack >
</ Interceptors >
< Default-interceptor-ref Name = "Mystack" />