2012-05-03 53 views
1

我试图用spring AOP编写拦截器。拦截器会发现请求URL是否为书签,如果是,将重定向到认证页面。 代码段:使用AOP-Spring拦截器的书签标记重定向

公共对象调用(的MethodInvocation调用)抛出的Throwable { logger.entering(this.getClass()getSimpleName(), “调用”,调用。);

Object result = null; 
    try { 
     // Logic to exclude the beans as per the list in the configuration. 
     boolean excluded = false; 
     for (String excludebean : excludedBeans) { 
      if (excludebean != null && excludebean.equalsIgnoreCase(invocation.getThis().getClass().getSimpleName())) { 
       excluded = true; 
       break; 
      } 
     } 

     // If the Target Method is "toString", then set EXCLUDE to TRUE and process the request 
     if(excluded == false && invocation.getMethod().getName().equalsIgnoreCase("toString")) 
     { 
      excluded = true; 
     } 

     // if user session object is available, then process the request or 
     // else forward to the configured view. 
     if (excluded || getSessionHolder().getUserVO() != null) { 
      result = invocation.proceed(); 
     } 
     else { 
      logger.logp(Level.INFO, this.getClass().getSimpleName(), 
        "invoke(MethodInvocation)", "User Object is "+ getSessionHolder().getUserVO() 
          + ". So redirecting user to home page"); 
      result = new ModelAndView("redirect:/security/authenticate.do"); 

     } 
    } 
    catch (Throwable ex) { 
     throw ex; 
    } 
    logger.exiting(this.getClass().getSimpleName(), "invoke"); 
    return result; 
} 

当我调试控制而来的其他块内的预期,但之后我返回结果,控制转到用于书签网址ratehr比处理程序重定向视图handle方法。

请帮我对此..预先感谢。

回答

1

为什么你需要拦截器的AOP。您可以使用Regular拦截器轻松重定向。

public class RedirectInterceptor extends HandlerInterceptorAdapter{ 

    private String redirectMapping; 

    public void setRedirectMapping(String redirectMapping) { 
     this.redirectMapping = **maintenanceMapping**; 
    } 


    //before the actual handler will be executed 
    public boolean preHandle(HttpServletRequest request, 
      HttpServletResponse response, Object handler) 
     throws Exception { 
         if (somethingHappened){ 
      response.sendRedirect(redirectMapping); 
      return false; 
         } else 
          return true; 

    } 
} 
+0

谢谢丹尼,我会试试这个。 – RVP

+0

我试过这种方式,我在拦截器内部有一个调试点,但控制器永远不会进入内部,我已将拦截器添加到中。我正在使用基于注解的url mapping.pls帮助。 – RVP

+0

请张贴您的代码和配置xml –