当前位置: 首页 > 知识库问答 >
问题:

使用AOP的书签重定向-spring拦截器

满才
2023-03-14

我试着用spring AOP写一个拦截器,拦截器会发现一个请求URL是否是书签,如果是的话会重定向到认证页面。代码段:

public Object invoke(MethodInvocation invocation)抛出Throwable{Logger.Entering(This.GetClass().GetSimPlename(),“invoke”,invocation);

    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;
}

当我调试时,控件按预期进入else块,但在我返回结果之后,控件转到书签URl ratehr的handle方法,而不是转到重定向视图的处理程序。

请帮我做这件事...提前谢谢。

共有1个答案

田修为
2023-03-14

为什么拦截器需要AOP。使用常规拦截器可以很容易地重定向。

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;

    }
}
 类似资料:
  • 本文向大家介绍详解spring面向切面aop拦截器,包括了详解spring面向切面aop拦截器的使用技巧和注意事项,需要的朋友参考一下 spring中有很多概念和名词,其中有一些名字不同,但是从功能上来看总感觉是那么的相似,比如过滤器、拦截器、aop等。 过滤器filter、spring mvc拦截器Interceptor 、面向切面编程aop,实际上都具有一定的拦截作用,都是拦截住某一个面,然后

  • 我正在使用spring AOP拦截这些方法。我在spring配置文件中有以下配置。 方面类: 上面的方法不拦截私有方法?要求方面既拦截私有方法,又拦截公有方法,怎么办?

  • 本文向大家介绍spring boot如何使用spring AOP实现拦截器,包括了spring boot如何使用spring AOP实现拦截器的使用技巧和注意事项,需要的朋友参考一下 在spring boot中,简单几步,使用spring AOP实现一个拦截器: 1、引入依赖: 2、创建拦截器类(在该类中,定义了拦截规则:拦截com.xjj.web.controller包下面的所有类中,有@Req

  • 问题内容: 任何人都知道为什么显然无法将AOP与带注释的MVC控制器一起使用吗?我有一个@Controller,一旦添加切入点,它就会停止工作。问题不在于没有调用拦截器,而是@Controller只是停止工作(在日志中,你可以看到“没有URL而不是“将URL路径[/ xx]映射到处理程序’Yyy””)确定的路径”)。 我知道有一种通过handlerMapping将拦截器添加到控制器的机制,但是我的

  • 在 imi 中更加推荐使用 AOP 来拦截请求。 不要忘记把 Aspect 类加入 beanScan! Demo <?php namespace ImiApp\ApiServer\Aop; use Imi\RequestContext; use Imi\Aop\Annotation\Around; use Imi\Aop\Annotation\Aspect; use Imi\Aop\Annota

  • 在Android WebView中有没有拦截重定向请求的方法?似乎不应该调用InterceptRequest(WebView,WebResourceRequest)! 我希望通过以下方式自动捕获所有web视图请求中的令牌过期: 从服务器发出重定向而不是401 使用shouldOverrideUrlLoading()在需要登录的情况下调用AppAuth 使用shouldInterceptReques