Struts2 Interceptor-user session judgment

Source: Internet
Author: User

Struts2 Interceptor-user session judgment

Almost every J2EE project involves a user module. As long as it involves some management and judgment of user login and logout and its session, I have written a lot of code about this, I just wrote but did not make a summary. Today I will make a simple summary to help new people learn or learn from it. well, I don't want to talk much nonsense.

Today, we mainly explain how struts2 judges and intercepts sessions.

First, we write the configuration information of the interceptor in the xml configuration file of struts, as follows:

 

  
  
   
   
    
/Login. jsp
   
  
 

As shown in the code, I defined a package named struts2, which defines the interceptors and Global View redirection. The sessionout defined in the interceptor is our session judgment function, it points to the class as follows:

 

 

Package com. smartsoft. interceptor; import javax. servlet. http. httpServletResponse; import org. apache. struts2.ServletActionContext; import com. opensymphony. xwork2.ActionInvocation; import com. opensymphony. xwork2.interceptor. abstractInterceptor; import com. smartsoft. model. user; import com. smartsoft. util. constants; public class SessionIterceptor extends actinterceptor {@ Override public String intercept (ActionInvocation actionInvocation) throws Exception {// get the request URL String url = ServletActionContext. getRequest (). getRequestURL (). toString (); HttpServletResponse response = ServletActionContext. getResponse (); response. setHeader (Pragma, No-cache); response. setHeader (Cache-Control, no-cache); response. setHeader (Cache-Control, no-store); response. setDateHeader (Expires, 0); User loginUser = null; // direct to login and logout requests If (url. indexOf (login) is not blocked )! =-1 | url. indexOf (logout )! =-1) {return actionInvocation. invoke ();} else {// verify whether the Session has expired if (! ServletActionContext. getRequest (). isRequestedSessionIdValid () {// session expired. Switch to the session expiration prompt page and finally jump to the logon page System. out. println (session expired); return toLogin;} else {loginUser = (User) ServletActionContext. getRequest (). getSession (). getAttribute (Constants. USER_SESSION); // verify if you have logged on to if (loginUser = null) {// if you have not logged on, go to the logon page to return toLogin;} else {return actionInvocation. invoke ();}}}}}

The above code logic is relatively simple. It gets the user of the current session and determines whether the user exists. If yes, the user is allowed to determine whether the user is logged on or not. Otherwise, the user is redirected to the defined logon interface.

 

The above is only the whole definition process of the Interceptor. How can we apply it to the connection we need? You must add the following configuration to each struts action, for example

 

        
 
  
   /index.jsp
  
    
  
  
   index
  
    
  
 

For example, this is a login xml configuration defined by me, because I did not write it in the same package as the interceptor, so I need to use the previous interceptor here, you need to inherit the package of the interceptor from another package. Enter the package name of the interceptor in the extends of packge, then add the configuration in each action that needs to be intercepted and judged. If the configuration is directly written in the interceptor package, it will be used directly and there will be no inheritance.

 

 

 

 

Related Article

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.