JavaWeb -- Struts 自定义拦截器, 登录权限拦截

1. 自定义拦截器, 登录权限拦截

login.jsp 登录JSP

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>login.jsp</title>
	</head>
	<body>
		<s:form action="LoginAction_reg" name="login">
			<s:textfield name="name" label="UserName" />
			<s:submit />
		</s:form>
	</body>
</html>

LoginAction.java 登录处理action

/**
 * 登录action
 */
public class LoginAction extends ActionSupport implements SessionAware{
	private static final long serialVersionUID = -6933309304624396640L;

	public String name;

	/* 接受session中的map */
	private Map<String, Object> sessionMap;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String reg() {
		if("tom".equals(name)){
			//存放登录信息
			sessionMap.put("username", name);
		}
		else{
			//回到登录页面
			return LOGIN ;
		}
		return "success";
	}

	public String toLoginView() {
		System.out.println("toRegView");
		return "loginView";
	}

	/**
	 * 注入session map
	 */
	public void setSession(Map<String, Object> session) {
		this.sessionMap = session ;
	}
}

LoginInterceptor.java 拦截器

/**
 * 登录拦截器
 */
public class LoginInterceptor implements Interceptor {
	private static final long serialVersionUID = 286019271181543787L;

	public void destroy() {
	}

	public void init() {
	}

	public String intercept(ActionInvocation invocation) throws Exception {
		Object action = invocation.getAction();
		//如果是达到登录或者注册的action,则直接放行
		if(action instanceof LoginAction || action instanceof RegAction){
			return invocation.invoke();
		}
		
		Map<String, Object> sessionMap = invocation.getInvocationContext().getSession();
		//如果没有登录
		if(sessionMap.get("username") == null){
			return "login" ;
		}
		//放行
		else{
			return invocation.invoke();
		}
	}
}

login.xml ,action interceptor 注册配置, 需要加到struts.xml (include)

<?xml version="1.0"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
    <package name="LoginPkg" namespace="/login" extends="struts-default">
        <interceptors>
            <!-- 注册拦截器 -->
            <interceptor name="loginInterceptor" class="cn.itcast.struts2.interceptor.LoginInterceptor" />
            
            <!-- 自定义拦截器栈 -->
            <interceptor-stack name="loginStack">
                <interceptor-ref name="loginInterceptor" />
                <interceptor-ref name="defaultStack" />
            </interceptor-stack>
        </interceptors>
        <!-- 定义默认栈 -->
        <default-interceptor-ref name="loginStack" />
        
        <!-- 定义全局结果 -->
        <global-results>
            <result name="login">/login.jsp</result>
        </global-results>
        
        <!-- 注册登录action -->
        <action name="LoginAction_*" class="cn.itcast.struts2.interceptor.LoginAction" method="{1}">
            <result name="success">/success.jsp</result>
            <result name="loginView">/login.jsp</result>
        </action>
    </package>
</struts>


 


posted @ 2014-01-11 14:58  今晚打酱油_  阅读(338)  评论(0编辑  收藏  举报