Struts2 custom interception to prevent illegal operations

Source: Internet
Author: User
<Package name = "defaults" extends = "struts-Default"> <interceptors> <interceptor name = "login" class = "com. zqgame. interceptor. checklogininterceptor "/> <Interceptor-stack name =" myinterceptor "> <Interceptor-ref name =" login "> <Param name =" excludemethods "> validatelogin </param> </interceptor -Ref><Interceptor-ref name = "defaultstack"/></Interceptor-stack> </interceptors> <! -- Set the interceptor stack automatically called by all actions --> <default-interceptor-ref name = "myinterceptor"> </default-interceptor-ref> <global-Results> <result name = "error">/err/exception. JSP </result> <result name = "message">/err/message. JSP </result> </Global-Results> <global-exception-mappings> <exception-mapping exception = "Java. lang. nullpointerexception "result =" error "/> <exception-mapping exception =" Java. lang. exception "result =" error "/> </Global-exception-mappings> </package>

 

In the action configuration, defaults is inherited:

<package name="login" namespace="/login" extends="defaults">        <action name="login_*" class="loginAction" method="{1}">                </action>    </package>

 

Interceptor class implementation:

Public class checklogininterceptor extends methodfilterinterceptor {private logger log = loggerfactory. getlogger (checklogininterceptor. class); Private Static final long serialversionuid = 1l; @ override protected string dointercept (actioninvocation) throws exception {// check whether a login Map <string, object> session = actioninvocation. getinvocationcontext (). getsession (); string Lo Gin = (string) Session. Get (syskey. login_key); If (LOGIN! = NULL & login. Length ()> 0) {// login is not null, And the logon session is valid. Return actioninvocation. Invoke ();} else {// otherwise, the operation is invalid and login log. debug ("no login, forward login page! "); Return pagecode. login ;}}}

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.