用拦截器实现登录验证功能AuthorizationInterceptor_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 用拦截器实现登录验证功能AuthorizationInterceptor

用拦截器实现登录验证功能AuthorizationInterceptor

 2011/11/1 8:17:48  xp9802  http://xp9802.iteye.com  我要评论(0)
  • 摘要:用拦截器实现登录验证功能AuthorizationInterceptor:packagecom.interceptor;importcom.opensymphony.xwork2.ActionInvocation;importcom.opensymphony.xwork2.interceptor.*;importcom.opensymphony.xwork2.*;importjava.util.*;/***@authorhttp://xp9802.iteye
  • 标签:功能 实现
用拦截器实现登录验证功能AuthorizationInterceptor:
package com.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.*;
import com.opensymphony.xwork2.*;
import java.util.*;
/**
 * @author http://xp9802.iteye.com/
 */
public class AuthorizationInterceptor extends AbstractInterceptor {
	private String ignoreActions;

	// ignoreActions属性的getter方法
	public String getIgnoreActios() {
		return ignoreActions;
	}

	// ignoreActions属性的setter方法
	public void setIgnoreActions(String ignoreActions) {
		this.ignoreActions = ignoreActions;
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		ActionContext ctx = invocation.getInvocationContext();

		Map session = ctx.getSession();
		String user = (String) session.get("username");

		boolean ignore = false;
		String currentAction = invocation.getProxy().getActionName();
		String[] actions = ignoreActions.split(",");

		for (String action : actions) {
			if (currentAction.matches(action.trim())) {
				ignore = true;
				break;
			}
		}

		if (user != null || ignore == true) {

			return invocation.invoke();
		} else {
			return Action.LOGIN;
		}

	}
}


struts.xml文件里面配置该拦截器:

  <interceptors> 
  	<interceptor name="authorization" class="com.interceptor.AuthorizationInterceptor" /> 
  	<interceptor-stack name="myStack"> 
  		<interceptor-ref name="authorization"> 
  			<param name="ignoreActions"> validate_code,register.*,.*login.*,upload,connector</param> 
 		 </interceptor-ref> <interceptor-ref name="defaultStack" />
  </interceptor-stack> 
 </interceptors> 
  <default-interceptor-ref name="myStack"/>
发表评论
用户名: 匿名