struts自己定义拦截器--登录权限控制

说明:该自己定义的拦截器实现用户登录的权限控制。

login.jsp--->LoginAction--重定向-->MainAction--->main.jsp

一.1.整体的步骤:

(1).定义拦截器类.LoginInterceptor(完毕登录拦截)
   方式1:实现com.opensymphony.xwork2.interceptor.Interceptor接口并覆写方法.
   方式2:继承com.opensymphony.xwork2.interceptor.AbstractInterceptor类,覆写intercept方法.
   
(2).在struts.xml中注冊该拦截器

(3).告诉<action>使用该拦截器
<action name="login" class="cn.wwh.www.web.interceptor.LoginAction">
<!-- 该Action引用哪一个拦截器 -->
<interceptor-ref name="loginInterceptor" />
<result type="redirectAction">
<param name="actionName">main.action</param>
</result>

</action>


可是第三步操作完,Action中再也获取不到请求參数.
<default-interceptor-ref name="defaultStack"/> 在struts-default.xml配置
<action>都有默认的拦截器,一旦显示的引用了其它的拦截器,默认的就被取消了.



解决方式:先引用defaultStack,再引用loginInterceptor.
<action name="login" class="cn.wwh.www.web.interceptor.LoginAction">
<!-- 引用默认拦截器(接受请求參数等功能), -->
<interceptor-ref name="defaultStack"/>
<!-- 该Action引用loginInterceptor拦截器 -->
<interceptor-ref name="loginInterceptor" />

<result type="redirectAction">
<param name="actionName">main.action</param>
</result>
</action>

上述引用的两个拦截器都定义在某一个<action>元素中,若此时同<package>中多个<action>也要引用这两个拦截器.
那么的都要拷贝这两行代码.

解决方式:我们也定义拦截器栈

<!--告诉当前<package>默认使用的拦截器栈 -->
<default-interceptor-ref name="myStack" />

二.代码实战:

1.LoginInterceptor.java:

<strong>
</strong>
<strong>package cn.wwh.www.web.interceptor;

import java.util.Arrays;
import java.util.List;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 *类的作用:自己定义拦截器类,继承AbstractInterceptor。
 * 
 * 
 *@author 一叶扁舟
 *@version 1.0
 *@创建时间: 2014-8-16 下午05:10:23
 */
public class LoginInterceptor extends AbstractInterceptor {
	private static final long serialVersionUID = 1L;
	private List<String> actionNames;

	// 在系统启动自己主动为actionNames属性设置值
	public void setActionNames(String actionNames) {
		System.out.println("--->" + actionNames);
		// 将配置文件里的參数,利用Arrays工具拆一个集合
		<span style="color:#ff0000;">this.actionNames = Arrays.asList(actionNames.split(","));</span>
	}

	// 拦截方法
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		// 若Session中有key为USER_IN_SESSION的对象,就应该放行
		ActionContext ctx = invocation.getInvocationContext();
		Object user = ctx.getSession().get("USER_IN_SESSION");// 从Session中去获取
		// 若当前Action的名字等于login也应该放行
		String actionName = invocation.getProxy().getActionName();// 获取当前Action的名字
		System.out.println(actionName);
		System.out.println("LoginInterceptor:" + user);
		// 当前的action是login,或者有正确的用户登录时。程序放行。然而是main时。并不包括在actionNames中。
		// 则不能放其通过,所以if的推断条件中的应该是||而不是&&
		if <span style="color:#3333ff;">(user != null</span><span style="color:#ff0000;"> ||</span><span style="color:#3333ff;"> this.actionNames.contains(actionName))</span> {// 登陆
			System.out.println("通过。进入程序的主界面!");
			return invocation.invoke();// 放行
		}
		System.out.println("还没登陆,请登录!");
		// 否则到登陆界面
		return Action.LOGIN;// "login"
	}
}</strong>


2.登录界面:login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>


<body>	
		<form action="/StrutsExercise5/interceptor/login" method="post">
			名字:<input type="text" name="username"/><br/>
			密码:<input type="text" name="password"/><br/>
			<input type="submit" value="登陆"/><br/>
		</form>	
		
</body>
</html>

3.action类:

package cn.wwh.www.web.interceptor;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 *类的作用:
 *
 *
 *@author 一叶扁舟
 *@version 1.0
 *@创建时间: 2014-8-16   下午05:17:12
 */

public class LoginAction extends ActionSupport implements ModelDriven<User> {
	private static final long serialVersionUID = 1L;
	private User user = new User();

	public String execute() throws Exception {
		System.out.println(user);
		
		//两种方式将数据存储到session中,把User对象放入Session
		//ServletActionContext.getRequest().getSession().setAttribute(name, value)
		<span style="color:#ff0000;">ActionContext.getContext().getSession().put("USER_IN_SESSION", user);</span>
		
		return SUCCESS;
	}

	public User getModel() {
		return user;
	}
}

4.实体类:User.java

package cn.wwh.www.web.interceptor;

import java.io.Serializable;

/**
 *类的作用:
 * 
 * 
 *@author 一叶扁舟
 *@version 1.0
 *@创建时间: 2014-8-16 下午05:03:09
 */

public class User implements Serializable {
	private static final long serialVersionUID = 1L;

	private String username;
	private String password;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public String toString() {
		return "User --->
 username=" + username + "
 password=" + password;
	}

}


5.用户验证成功后的页面:main.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>	
			                         已经成功登录,进入程序的main界面!

</body> </html>


6.main的action类:

package cn.wwh.www.web.interceptor;

import com.opensymphony.xwork2.ActionSupport;

/**
 *类的作用:
 *
 *
 *@author 一叶扁舟
 *@version 1.0
 *@创建时间: 2014-8-16   下午05:18:16
 */
public class MainAction extends ActionSupport  {
	private static final long serialVersionUID = 1L;

	public String execute() throws Exception {
		return SUCCESS;
	}
}

7.interceptor.xml配置文件(这个要在struts.xml文件里用include包括进去)

<?

xml version="1.0" encoding="UTF-8" ?

> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="interceptor" extends="struts-default" namespace="/interceptor"> <span style="color:#3333ff;"><interceptors> <!-- 注冊一个拦截器,而且该拦截器必须在action之前 --> <interceptor name="loginInterceptor" class="cn.wwh.www.web.interceptor.LoginInterceptor"> <!-- 设置 LoginInterceptor,不须要拦截的action的name--> <param name="actionNames">login,userLogin,xxAction,ooAction</param> </interceptor> <!-- 定义一个拦截器栈 --> <interceptor-stack name="myStack"> <!-- 引用默认拦截器(接受请求參数等功能), --> <interceptor-ref name="defaultStack" /> <!-- 该Action引用loginInterceptor拦截器 --> <interceptor-ref name="loginInterceptor" /> </interceptor-stack> </interceptors></span> <span style="color:#ff0000;"><!--告诉当前<package>默认使用的拦截器栈 --> <default-interceptor-ref name="myStack" /> </span> <!-- 配置全局的结果视图 --> <global-results> <result name="login">/views/interceptor/login.jsp</result> </global-results> <action name="login" class="cn.wwh.www.web.interceptor.LoginAction"> <result type="redirectAction"> <!-- 由于是同一个命名空间的Action跳转,所以不是必需配置namespace: <param name="namespace">/interceptor</param> --> <param name="actionName">main.action</param> </result> </action> <action name="main" class="cn.wwh.www.web.interceptor.MainAction"> <result>/views/interceptor/main.jsp</result> </action> </package> </struts>



注意每一个包仅仅能指定一个默认拦截器。

另外,一旦我们为该包中的某个action显式指定了某个拦截器。

则默认拦截器不会起作用。



原文地址:https://www.cnblogs.com/yxysuanfa/p/7011120.html