Struts2 Interceptor - Implementing Login Interception Example

【1】struts.xml:

 

<!-- define an interceptor-->  
        <interceptors>  
            <interceptor name="authority"  
                class="org.interceptot.LoginInterceptor">  
            </interceptor>  
            
            <!-- Interceptor stack -->  
            <interceptor-stack name="mydefault">  
                <interceptor-ref name="defaultStack" />  
                <interceptor-ref name="authority" />  
            </interceptor-stack>  
        </interceptors>
        
         
        
         <!-- Define global Result -->  
        <global-results>  
            <!-- When the login view name is returned, go to the /login.jsp page-->  
            <result name="login">/login.jsp</result>  
        </global-results>
        
        
        <action name="show" class="org.action.showAction">  
            <result name="success">/main.jsp</result>  
            <!-- use this interceptor-->  
            <interceptor-ref name="mydefault" />  
        </action>  
        
        
        <!--Verify login user information-->
        <action name="login" class="org.action.loginAction" method="execute">
            <result name="error">/login.jsp</result>  
            <result name="input">/login.jsp</result>
        </action>
       

 [2] Custom interceptor org.interceptot.LoginInterceptor:

package org.interceptot;
import java.util.Map;  

import com.opensymphony.xwork2.Action;  
import com.opensymphony.xwork2.ActionContext;  
import com.opensymphony.xwork2.ActionInvocation;  
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;  
public class LoginInterceptor extends AbstractInterceptor {  
 
    @Override  
    public String intercept(ActionInvocation invocation) throws Exception {  
  
        // Get the ActionContext instance related to the request  
        ActionContext ctx = invocation.getInvocationContext();  
        Map session = ctx.getSession();  
        String user = (String) session.get("username");  
  
        // If there is no login, that is, the user name does not exist, return to re-login  
        System.out.println("user:"+user);
        if (user != null) {  
            System.out.println("test");  
            return invocation.invoke();  
        }  
        System.out.println("You are not logged in yet");
        ctx.put("tip", "You are not logged in");  
        return Action.LOGIN; //Return a result called login
  
    }  
  
}  

 [3] Enter the Action on the main page: org.action.showAction

package org.action;

import com.opensymphony.xwork2.ActionSupport;  

public class showAction extends ActionSupport {  
 public String execute() {  
  return "success";  
 }  
}  

 

 

【4】LoginAction:

private boolean isInvalid(String value) {     
return (value == null || value.length() == 0);     
}  
if (isInvalid(user.getUsername()))   
       return INPUT;     
 if (isInvalid(user.getPassword()))     
       return INPUT;    
 //The login is successful and the User is put into the session
HttpServletRequest request = ServletActionContext.getRequest();
Map  map=ActionContext.getContext().getSession();
map.put("username", user.getUsername());

 Reprinted from http://blog.sina.com.cn/s/blog_77cb836301015m3y.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326616938&siteId=291194637