`
zhangfeilo
  • 浏览: 392236 次
  • 性别: Icon_minigender_1
  • 来自: 昆明
社区版块
存档分类
最新评论

SSH中在Action中用Spring的aop来验证用户是否已经登录的拦截器

阅读更多

1.Spring的aop来验证用户是否已登录的拦截器

 

package angus.interceptor;

import org.aopalliance.intercept.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import angus.vo.ClientVO;

public class ClientInterceptor 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];
    }
   }
   HttpSession session = request.getSession();
   ClientVO cvo = (ClientVO)session.getAttribute("clientInfo");
   if (cvo == null)
   {
    request.setAttribute("pleaseLogin", "请先登录");
    String url = request.getRequestURI() + "?" + request.getQueryString();
    session.setAttribute("clientUrl", url);
    request.setAttribute("nologin", "请先登录");
    ActionForward af = new ActionForward("/myalbum.do");
    return af;
   }
   else
   {
    return invocation.proceed();
   }
}
}

 2.相应的在Spring中的applicationContext.xml中的配置

<bean id="commentInter" class="angus.interceptor.CommentInterceptor"/>
    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
     <property name="beanNames">
            <list>
                <value>/addComment</value>
            </list>
     </property>
        <property name="interceptorNames">
            <list>
                <value>commentInter</value>
            </list>
        </property>
    </bean>

2
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics