读书人

Struts2兑现权限控制拦截器

发布时间: 2012-10-13 11:38:17 作者: rapoo

Struts2实现权限控制拦截器

???? 进行权限控制是很多应用都会用到,本示例要求用户登录,且为指定用户名才可以查看系统中的某个视图资源,否则系统直接转入登录页面。

??? 检查用户是否登录,通常都是通过跟踪用户的session来完成的,通过ActionContext即可访问到session中的属性,拦截器的intercept(ActionInvocation invocation)方法的invocation参数就可以很轻易地访问到请求相关的ActionContext实例。

?

下面就以登录为例来演示:

1、创建登录页面

login.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title>登录页面</title>    <script type="text/javascript">    function register(){    //获取jsp页面中的一个表单元素    targetForm = document.form1;    //动态修改目标表单的ation属性    targetForm.action = "login!register.action";    //提交表单    targetForm.submit();    }    </script>  </head>    <body>    <form name="form1" action="login.action" method="post">    <table align="center">    <caption>用户登录</caption>    <tr>    <td>用户名:</td>    <td><input type="userName" name="userName"/></td>    </tr>    <tr>    <td>密  码:</td>    <td><input type="password" name="password"/></td>    </tr>    <tr align="center">    <td colspan="2">    <input type="submit" value="登录"/>    <input type="button" onClick="javascript:register();" value="注册"/>    </td>    </tr>    </table>    </form>  </body></html>

?

?

2、创建pojo和Action

UserBean.java:

package com.lee.action;/** * @author lijunjie */import java.text.SimpleDateFormat;import java.util.Date;public class UserBean {private String userName;private String password;private String tip;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;}public String getTip() {return tip;}public void setTip(String tip) {this.tip = tip;}}

?

?

LoginAction.java:

package com.lee.action;/** * @author lijunjie */import java.util.Map;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;public class LoginAction extends ActionSupport implements ModelDriven<UserBean> {private UserBean model = new UserBean();@Overridepublic String execute() throws Exception {System.out.println("开始执行execute()");if("sortec".equals(model.getUserName()) && "sortec".equals(model.getPassword())){this.getModel().setTip("服务器提示:登录成功...嘎嘎");ActionContext ctx = ActionContext.getContext();Map session = ctx.getSession();session.put("user", this.getModel().getUserName());return SUCCESS;}else{return ERROR;}}public void hello(){System.out.println("hello...");}public UserBean getModel() {return model;}}

?

?

3、创建权限拦截器

AuthorityInterceptor.java:

package com.lee.action;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;/** * 定义权限检查拦截器 * @author lijunjie * */public class AuthorityInterceptor extends AbstractInterceptor {private static final long serialVersionUID = 7367211037602072409L;//拦截Action处理的拦截方法@Overridepublic String intercept(ActionInvocation invocation) throws Exception {//取得请求相关的ActionContext实例ActionContext ctx = invocation.getInvocationContext();Map session = ctx.getSession();//取出名为user的session属性String user = (String) session.get("user");//如果没有登录,或者登录所用的用户名不是sortec,都返回重新登录if(null != user && "sortec".equals(user)){return invocation.invoke();}//没有登录,将服务器提示设置成一个HttpServletRequest属性//ctx.put("tip", "您还没有登录,请先登录系统。。。");//直接返回LOGIN的逻辑视图return Action.LOGIN;}}

?

?

?

4、配置拦截器

struts.xml:

<!DOCTYPE struts PUBLIC        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"        "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>    <include file="struts-default.xml"/>        <package name="base" extends="struts-default">    <global-results>    <result name="login">/login.jsp</result>    </global-results>    </package>        <package name="lee" extends="base">    <interceptors>    <!-- 定义了一个名为authority的拦截器 -->    <interceptor name="authority" class="com.lee.action.LoginAction"><!-- 返回SUCCESS逻辑视图时,转入showbooks.jsp物理视图 --><result>/showbooks.jsp</result><!-- 拦截器一般配置在result之后 --><interceptor-ref name="defaultStack"/><!-- 自定义拦截器 <interceptor-ref name="authority"/>--></action>    </package>    </struts>

??? 注:考虑到这个拦截器的重复使用,可能多个Action都需要跳转到login逻辑视图,故将login Result定义成一个全局Result。

?

5、测试,当用户名和密码都输入sortec,点击登录按钮,系统会跳转到showbooks.jsp,看到页面内容为:服务器提示:登录成功...嘎嘎 ;否则,系统都会跳转到login.jsp登录页面。

1 楼 张晨光 2010-05-14 404 找不到login.action

读书人网 >软件架构设计

热点推荐