spring aop實現許可權控制,路徑控制

kukuqiu001發表於2008-02-18
spring aop 的許可權的管理是通過對路徑的控制來實現的
現在共有兩個角色,經理和員工
經理的許可權檢查的程式碼
MgrAuthorityInterceptor.java

public class MgrAuthorityInterceptor implements MethodInterceptor
{

public Object invoke(MethodInvocation invocation) throws Throwable
{
HttpServletRequest request = null;
ActionMapping mapping = null;
Object[] args = invocation.getArguments();
//解析目標方法的引數
for (int i = 0 ; i < args.length ; i++ )
{
if (args[i] instanceof HttpServletRequest) request = (HttpServletRequest)args[i];
if (args[i] instanceof ActionMapping) mapping = (ActionMapping)args[i];
}
//從session中得到使用者的級別
String level = (String)request.getSession().getAttribute("level");
//如是經理級別則繼續,否則,回到登陸頁面
if ( level != null && level.equals("mgr") )
{
return invocation.proceed();
}
else
{
return mapping.findForward("login");
}
}
}

員工的許可權的實現,EmpAuthorityInterceptor.java

public class EmpAuthorityInterceptor implements MethodInterceptor
{

public Object invoke(MethodInvocation invocation) throws Throwable
{
HttpServletRequest request = null;
ActionMapping mapping = null;
Object[] args = invocation.getArguments();
for (int i = 0 ; i < args.length ; i++ )
{
if (args[i] instanceof HttpServletRequest) request = (HttpServletRequest)args[i];
if (args[i] instanceof ActionMapping) mapping = (ActionMapping)args[i];
}
//從session中得到使用者的級別
String level = (String)request.getSession().getAttribute("level");
//如是經理或員工級別則繼續,否則,回到登陸頁面
if ( level != null && (level.equals("emp") || level.equals("mgr")))
{
return invocation.proceed();
}
else
{
return mapping.findForward("login");
}
}
}


員工,經理許可權的實現,在action-servlet.xml中

<!-- 以經理許可權攔截器生成代理 -->
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
action中的經理的操作
</list>
</property>
<property name="interceptorNames">
<list>
<value>mgrAuthorityInterceptor</value>
</list>
</property>
</bean>

<!-- 以普通員工許可權攔截器生成代理 -->
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
員工中的action操作
</list>
</property>
<property name="interceptorNames">
<list>
<value>empAuthorityInterceptor</value>
</list>
</property>
</bean>

<!-- 定義經理許可權檢查攔截器,class即前面的MgrAuthorityInterceptor.java-->
<bean id="mgrAuthorityInterceptor" class="org.***.MgrAuthorityInterceptor"/>
<!-- 定義普通員工許可權檢查攔截器 ,class即前面的EmpAuthorityInterceptor.java-->
<bean id="empAuthorityInterceptor" class="org.***.EmpAuthorityInterceptor"/>

相關文章