现在的位置: 首页 > 综合 > 正文

Struts2实现权限控制

2017年10月14日 ⁄ 综合 ⁄ 共 4024字 ⁄ 字号 评论关闭

IT程序员开发必备-各类资源下载清单,史上最全IT资源,个人收藏总结!

Struts2的核心功能是通过一个个interceptor来实现的,那么用户可以自定义一个interceptor来实现权限控制功能。

   步骤一:自定义一个类,该类实现Interceptor接口,重写其intercept()方法。如:

package edu.interceptor;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class PermissionInterceptor implements Interceptor {

	@Override
	public void destroy() {

	}
	@Override
	public void init() {
		
	}
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
       Object user = ActionContext.getContext().getSession().get("user");
       if(user!=null)
    	  return invocation.invoke();//有权限,调用方法
       ActionContext.getContext().put("message", "你还没有登陆,请先登录!");
	   return "message";//无权限,返回视图,其返回值也会匹配struts.xml总的result视图,然后返回页面。
	}
}


  步骤二:注册该interceptor类。

例如:

struts-interceptor.xml (方式一)

<struts>
  <package name="interceptor" namespace="/interceptor" extends="struts-default">
      <interceptors>
         <interceptor name="permission" class="edu.interceptor.PermissionInterceptor"></interceptor>
         <interceptor-stack name="myInterceptorStack">
            <!-- 系统定义的interceptor管理在defaultStack下,定义的时候一般写在自定义interceptor的前面 -->
            <interceptor-ref name="defaultStack"></interceptor-ref>
            <interceptor-ref name="permission"></interceptor-ref>
         </interceptor-stack>
      </interceptors>
       <global-results>
         <result name="message">/interceptor/message.jsp</result>
      </global-results>
      <action name="user_*" class="edu.action.LoginAction" method="{1}">
      </action>
      <action name="product_*" class="edu.action.ProductAction" method="{1}">
         <interceptor-ref name="myInterceptorStack"></interceptor-ref>
      </action>
  </package>
</struts>

等效于:(方式二)

<struts>
  <package name="interceptor" namespace="/interceptor" extends="struts-default">
      <interceptors>
         <interceptor name="permission" class="edu.interceptor.PermissionInterceptor"></interceptor>
      </interceptors>
      <!--  <default-interceptor-ref name="myInterceptorStack"></default-interceptor-ref>-->
       <global-results>
         <result name="message">/interceptor/message.jsp</result>
      </global-results>
      <action name="user_*" class="edu.action.LoginAction" method="{1}">
      </action>
      <action name="product_*" class="edu.action.ProductAction" method="{1}">
      <!-- 系统定义的interceptor管理在defaultStack下,定义的时候一般写在自定义interceptor的前面 -->
         <interceptor-ref name="defaultStack"></interceptor-ref>
         <interceptor-ref name="permission"></interceptor-ref>
      </action>
  </package>
</struts>

等效于:(方式三)

<?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" namespace="/interceptor" extends="struts-default">
      <interceptors>
         <interceptor name="permission" class="edu.interceptor.PermissionInterceptor"></interceptor>
         <interceptor-stack name="myInterceptorStack">
            <!-- 系统定义的interceptor管理在defaultStack下,定义的时候一般写在自定义interceptor的前面 -->
            <interceptor-ref name="defaultStack"></interceptor-ref>
            <interceptor-ref name="permission"></interceptor-ref>
         </interceptor-stack>
      </interceptors>
       <default-interceptor-ref name="myInterceptorStack"></default-interceptor-ref>
       <global-results>
         <result name="message">/interceptor/message.jsp</result>
      </global-results>
      <action name="user_*" class="edu.action.LoginAction" method="{1}">
      </action>
      <action name="product_*" class="edu.action.ProductAction" method="{1}">
        <!-- 每个包只能指定一个默认拦截器。另外,一旦我们为该包中的某个action显式指定了某个拦截器,则默认拦截器对该action不会起作用。除非在该action中在注册一次默认拦截器 -->
      </action>
  </package>
  
</struts>

这种使用<default-interceptor-ref name="myInterceptorStack"></default-interceptor-ref>方式为package所有action注册interceptor,不好,因为这样当处理该包下所有的action的请求时,都会经过拦截器。

总结:

    (1)因为struts2中如文件上传,数据验证,封装请求参数到action等功能都是由系统默认的defaultStack中的拦截器实现的,所以我们定义的拦截器需要引用系统默认的defaultStack,这样应用才可以使用struts2框架提供的众多功能。

    (2)一旦某个action中自定义了一个interceptor,那么该action就失去了struts2提供的系统interceptor,解决办法是在自定义<interceptor-ref>之前注册系统的<interceptor-ref name="defaultStack"> 

    (3)如果希望包下的所有action都使用自定义的拦截器,可以通过<default-interceptor-ref name="permissionStack"/>把拦截器定义为默认拦截器。注意:每个包只能指定一个默认拦截器。另外,一旦我们为该包中的某个action显式指定了某个拦截器,则默认拦截器对该action不会起作用。需要重新在action下注册

    (4)可以使用</interceptor-stack>来管理多个<interceptor>,如注册方式一。

    (5)defaultStack在\struts2-core-2.3.4.1.jar的struts-default.xml中定义。

抱歉!评论已关闭.