1. 程式人生 > >Struts攔截器判斷使用者是否登陸

Struts攔截器判斷使用者是否登陸

package wge.learn.shop.interceptor;

import org.apache.struts2.ServletActionContext;

import wge.learn.shop.adminuser.vo.AdminUser;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

/**
 * 許可權攔截器:
 * @author 
 *
 */
public class PrivilegeInterceptor extends MethodFilterInterceptor{

	@Override
	protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
		// 判斷是否登入,如果登入,放行,沒有登入,跳轉到登入頁面.
		AdminUser adminUser = (AdminUser) ServletActionContext.getRequest()
				.getSession().getAttribute("existAdminUser");
		if(adminUser != null){
			// 已經登入過
			return actionInvocation.invoke();
		}else{
			// 跳轉到登入頁面:
			ActionSupport support = (ActionSupport) actionInvocation.getAction();
			support.addActionError("您還沒有登入!沒有許可權訪問!");
			return ActionSupport.LOGIN;
		}
	}

}