(转)实现struts2的CRUD中的权限控制(一)

本文介绍了如何在Struts2框架中实现权限控制,通过自定义拦截器的方式,结合角色和CRUD操作进行权限验证。文章详细展示了权限拦截器的具体实现,并说明了如何配置拦截器以确保每个操作都符合相应的权限要求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    继上篇《struts2的CRUD中的权限控制初探 》文章后,我们来实现具体的代码实现,在struts2中我们可以自定义拦截器。
       心中谨记基于接口编程的指导,考虑到我们的CRUD操作涉及到load,store,remove,list四个方法,而且要记录操作者的角色,我们提取了接口IRoleAndCRUD,内容如下:
/** *//**
 * 描述: CRUD操作接口,用于struts2拦截器实现权限控制
 * 
 * 
@author Stone yang 创建日期:2007-5-21
 * 
@version pattern Study 技术支持: <a
 *          href="
http://blog.youkuaiyun.com/yq76034150">http://blog.youkuaiyun.com/yq76034150</a>
 
*/

public interface IRoleAndCRUD ...{
    
public String load();

    
public String store();

    
public String remove();
    
    
public void setRole(String role);
    
    
public String list();
}
       下面我们就实现自己的权限拦截器,继承于实现了拦截器接口Interceptor的抽象类AbstractInterceptor,主要是实现intercept方法,该方法参数是ActionInvocation(action调用者?)可以通过ActionInvocation获取当前action相关信息,和webcontext相关信息,为around advice提供了可能,主要实现可以如下:
/** *//**
 * 描述:权限拦截器
 * 
 * 
@author Stone yang 创建日期:2007-5-21
 * 
@version pattern Study 技术支持: <a
 *          href="
http://blog.youkuaiyun.com/yq76034150">http://blog.youkuaiyun.com/yq76034150</a>
 
*/

public class AuthorizationInterceptor extends AbstractInterceptor ...{

    
private static final Logger log = Logger
            .getLogger(AuthorizationInterceptor.
class);

    
protected static Map<String, String> roleMethodMap = new HashMap<String, String>();

    
static ...{
        
if (roleMethodMap.size() <= 0...{
            roleMethodMap.put(
"list""view");
            roleMethodMap.put(
"store""edit");
            roleMethodMap.put(
"remove""remove");
        }

    }


    @Override
    
public String intercept(ActionInvocation ai) throws Exception ...{
        Map session 
= ai.getInvocationContext().getSession();
        String role 
= (String) session.get("ROLE");
        
if (null == role) ...{
            Object action 
= ai.getAction();
            
if (action instanceof IRoleAndCRUD) ...{
                IRoleAndCRUD crudAction 
= (IRoleAndCRUD) action;
                String methodName 
= ai.getProxy().getActionName();
                
if (role.equals(roleMethodMap.get(methodName))) ...//session中存储的角色和调用方法对应的权限一致
                    crudAction.setRole(role);
                    
return ai.invoke();
                }
 else ...{

                    
return Action.LOGIN;
                }

            }
 else ...{
                
return Action.LOGIN;
            }


        }
 else ...{
            
return Action.LOGIN;
        }

    }
       在该类中我们增加了一个静态map,来存储“角色”-“方法”的对应关系,在intercept方法中我们根据该map判断调用方法以及当前用户角色的对应关系,如果不符合我们将转向login页面,当然登陆用户可能具有多种权限,这个map还需要根据需求进一步重构。
       应用自定义拦截器也很简单,在struts.xml中定义即可,大致如下:
<package name="admin" extends="struts-default" namespace="/admin">
        
<!--  定义拦截器 -->
        
<interceptors>
            
<interceptor name ="auth" class ="com.waimai.utils.AuthorizationInterceptor" />
        
</interceptors > 
        
        
<action name="List" class="com.waimai.web.CaiTypeAction" method="list">
                 
<!--  调用拦截器 -->
            
<interceptor-ref name ="auth"/>
            
<result>listCaiType.jsp</result>
        
</action>
        
<action name="Edit" class="com.waimai.web.CaiTypeAction" method="load">
                 
<!--  调用拦截器 -->
            
<interceptor-ref name ="auth"/>
            
<result>editCaiType.jsp</result>
        
</action>
        
<action name="Store" class="com.waimai.web.CaiTypeAction" method="store">
                 
<!--  调用拦截器 -->
            
<interceptor-ref name ="auth"/>
               
<result name="input" type="dispatcher">editCaiType.jsp</result>
            
<result type="redirect">List.action</result>
        
</action>
        
<action name="Remove" class="com.waimai.web.CaiTypeAction" method="remove">
                 
<!--  调用拦截器 -->
            
<interceptor-ref name ="auth"/>
            
<result type="redirect">List.action</result>
        
</action>
    
</package>
          这样在调用这些定义了拦截器的action时都会检查用户权限。在我们的系统中,我们的crud相关的action的继承关系如下:具体action继承于AbstractCRUDAction实现IRoleAndCRUD接口,但是所有的需要权限控制的action方法都要在配制文件中加入<interceptor-ref name ="auth"/> 这么一行,真是很不爽呀,不知道能不能给接口设置拦截器,由于自己对struts2还不是很了解,查了一些资料没有好的解决方案,如果谁有好的解决方案请告知我。感觉要做到伸缩性强,维护性强的权限系统还是需要使用acegi集成进系统....

转载于:https://www.cnblogs.com/hya1109/archive/2008/01/28/1055512.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值