public class AuthorityInterceptor 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];
}
Object admin = request.getSession().getAttribute("admin");
if ( admin != null && ((String)admin).equals("admin"))
{
return invocation.proceed();
}
else
{
request.setAttribute("msg" , "您还没有登录,请先登录");
return mapping.findForward("admin");
}
}
}
该应用是在*.xml中来调用的..(用struts的mvc,然后托管给spring的ioc容器来管理的,配置也是由spring来处理的)
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>/processAddStudent</value>
<value>/listStudent</value>
<value>/delStudent</value>
<value>/processAddTest</value>
<value>/addQuestion</value>
<value>/delQuestion</value>
<value>/processAddQuestion</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>authorityInterceptor</value>
</list>
</property>
</bean>
<bean id="authorityInterceptor" class="org.yeeku.action.authority.AuthorityInterceptor"/>
//解释后的代码
public class AuthorityInterceptor implements MethodInterceptor
{
public Object invoke(MethodInvocation invocation) throws Throwable
{
HttpServletRequest request = null;
ActionMapping mapping = null;
-----------注解1开始
通过invocation.getArguments()可以获取代理对象的参数
代理的参数是ActionMapping, ActionForm ,HttpServletRequest , HttpServletResponse 四个
只不过,这里根据实际情况,我们只需要使用HttpServletRequest,ActionMapping罢了
因为这里你要通过request获取session和通过mapping跳转页面
因为 Object[] args = invocation.getArguments(); 规定,返回的必须是一个数组
所以,没办法,只能迭代把要用的找出来
而我们在XML配置的对象都是Struts Action
每一个Struts Action方法都形如这样,
public ActionForward addBlog(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
所以,通过
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];
}
可以从那里迭代出来
你既然都有调用 Struts 了, mapping, form, request, response 肯定都已经注入了,咱们运行Struts, 是在 mapping, form, request,
response 都具备的情况下,使用的
所以, Object[] args = invocation.getArguments();
得到应该对象的所有参数是完全可以的
--------注解1结束
Object[] args = invocation.getArguments();
---------加这两句是因为要通过request获取session 并且在判断没有权限的时候要通过mapping跳转页面
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];
}
Object admin = request.getSession().getAttribute("admin");
----------
如果权限有问题,
那么invocation.procceed() 就不会执行,意味着你的那个 Action 对象**方法也不会执行, 但此时
mapping 是存在的, 来一下findForward("admin");
----------
if ( admin != null && ((String)admin).equals("admin"))
{
return invocation.proceed();
}
else
{
request.setAttribute("msg" , "您还没有登录,请先登录");
return mapping.findForward("admin");
}
}
}