Struts2 interceptor for Logon Restrictions

Source: Internet
Author: User

Struts2 interceptor for Logon Restrictions

 

Generally, the logon function is available on webpages. Only logged-on users can have the permission to access resources in the system. Generally, you can check whether a user logs on to the server by tracking the user's session. You can use ActionContext to access the attributes in the session. The intercept (ActionInvocation invocation) of the interceptor) the invocation parameter of the method can easily access the ActionContext instance related to the request.

Take a look at the code of the action section as follows:

 

 

This method first checks whether the logon user name and password meet the logon requirements. If yes, the system logs on and creates a session for the current user name to monitor whether the user has logged on, whether you have the right to access resources.

 

Then let's look at the interceptor class. The sample code is as follows:

This code first needs to inherit the AbstractInterceptor class, and then rewrite the intercept method to access the relevant ActionContext instance through the ActionInvocation arg0 parameter. You can use the getInvocationContext method to obtain the current ActionContext instance, obtain the current session, and check whether the current user has logged on. If you are logged on, return success through the reflection method.

 

Finally, let's take a look at how to configure the interceptor tag in struts. xml. The sample code is as follows:

Pass Tag settings interceptor, where the sub-tag The property has the name attribute. You can give the interceptor a name and the class attribute. This attribute corresponds to the path of the interceptor method you write. Finally, it is associated with the Interceptor to be used in the action tag. Here, the default interceptor must appear. This must be declared, and then it is the custom interceptor.


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.