Use the struts Interceptor to implement the login verification function authorizationinterceptor

Source: Internet
Author: User

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" />

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.