Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
正确的mvc:Spring中的拦截器配置_Spring_Interceptor - Fatal编程技术网

正确的mvc:Spring中的拦截器配置

正确的mvc:Spring中的拦截器配置,spring,interceptor,Spring,Interceptor,我有点问题。我需要调用此拦截器中的每个请求postHandle方法: public class DiMenuInterceptor extends HandlerInterceptorAdapter { @Autowired private IDiCategoryService categoryService; @Override public void postHandle(HttpServletRequest request, HttpServl

我有点问题。我需要调用此拦截器中的每个请求postHandle方法:

public class DiMenuInterceptor extends HandlerInterceptorAdapter {

   @Autowired
   private IDiCategoryService categoryService;


   @Override
   public void postHandle(HttpServletRequest request,
        HttpServletResponse response, Object handler,
        ModelAndView modelAndView) throws Exception {

       modelAndView.addObject("category", categoryService.getCategoryInTree());
   }
}
if (ClassUtils.isAssignableValue(ResourceHttpRequestHandler.class, handler)) {
    return;
}

// do interceptor logic here...
所以我将这行代码放入servlet配置中,一切都很好

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:interceptors-ref="menuInterceptor" />

<bean id="menuInterceptor" class="cz.cosi.DiMenuInterceptor" />
也许我找到了解决办法,但肯定不是最好的。目前看来,这是可行的

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/*" />
        <bean class="cz.cosi.DiMenuInterceptor" />
    </mvc:interceptor>
    <mvc:interceptor>
         <mvc:mapping path="/search/**" />
        <bean class="cz.cosi.DiMenuInterceptor" />
    </mvc:interceptor>

    <mvc:interceptor>
         <mvc:mapping path="/context/**" />
        <bean class="cz.cosi.DiMenuInterceptor" />
    </mvc:interceptor>
    <mvc:interceptor>
          <mvc:mapping path="/member/**" />
        <bean class="cz.cosi.DiMenuInterceptor" />
    </mvc:interceptor>

</mvc:interceptors>

您需要指定您的路径包含子路径:
/**“
而不是
/*

<mvc:interceptors>
   <mvc:interceptor>
     <mvc:mapping path="/**" />
     <bean class="cz.cosi.DiMenuInterceptor" />
   </mvc:interceptor>
</mvc:interceptors>


@请参阅Spring参考资料中的示例,

问题是在请求资源时会调用拦截器

本文讨论了如何防止使用xml配置调用拦截器。在公认的答案中,我不太喜欢依赖路径的语义构成(即使用.html或在路径中包含页面)。现在团队中的所有开发人员在创建控制器时都必须意识到这一点

我也不喜欢verbosebean配置,因此我决定向拦截器添加以下代码:

public class DiMenuInterceptor extends HandlerInterceptorAdapter {

   @Autowired
   private IDiCategoryService categoryService;


   @Override
   public void postHandle(HttpServletRequest request,
        HttpServletResponse response, Object handler,
        ModelAndView modelAndView) throws Exception {

       modelAndView.addObject("category", categoryService.getCategoryInTree());
   }
}
if (ClassUtils.isAssignableValue(ResourceHttpRequestHandler.class, handler)) {
    return;
}

// do interceptor logic here...

我也有类似的问题,但在访问Bootstrap 3的字体时遇到了问题。拦截器阻止了字体资源,如“glyphicons halflings regular.woff”或“glyphicons halflings regular.ttf”,它们在首页上不可见。 我用以下方法解决我的问题。 我的拦截器的定义如下所示:

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/**"/>
        <bean class="cz.cosi.DiMenuInterceptor"/>
    </mvc:interceptor>
</mvc:interceptors>

我的DiMenuInterceptor类:

public class DiMenuInterceptor extends HandlerInterceptorAdapter {

private List<String> nonAuthPaths;

public DiMenuInterceptor() {
    nonAuthPaths = new ArrayList<String>();
    nonAuthPaths.add("/login");
    nonAuthPaths.add("/resources/vendor/fonts/glyphicons-halflings-regular.woff");
    nonAuthPaths.add("/resources/vendor/fonts/glyphicons-halflings-regular.ttf");
}

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

    Object user = request.getSession().getAttribute(SessionManagement.USER_KEY);

    if (user == null && !nonAuthPaths.contains(request.getServletPath())) {
            response.sendRedirect(request.getContextPath() + "/login");
        }
        return false;
    } else {
        //ect ...
        return true;
    }
}
公共类DiMenuInterceptor扩展了HandlerInterceptorAdapter{
私有列表非授权路径;
公共侦听器(){
非授权路径=新的ArrayList();
添加(“/login”);
添加(“/resources/vendor/font/glyphions/halflings regular.woff”);
添加(“/resources/vendor/fonts/glyphions.ttf”);
}
@凌驾
公共布尔预处理(HttpServletRequest请求、HttpServletResponse响应、对象处理程序)引发异常{
Object user=request.getSession().getAttribute(SessionManagement.user_键);
if(user==null&&!NoAuthPaths.contains(request.getServletPath())){
sendRedirect(request.getContextPath()+“/login”);
}
返回false;
}否则{
//等等。。。
返回true;
}
}

我也有同样的问题。初始配置:

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/*"/>
        <bean class="cn.mmd.micro.common.TokenInterceptor">
            <property name="excludeUrls">
                <list>
                    <value>/app/token</value>
                </list>
            </property>
        </bean>
    </mvc:interceptor>
</mvc:interceptors>

/应用程序/令牌
我更改了值“mvc:mapping”,它成功了。我的新配置如下:

<mvc:interceptors>
<mvc:interceptor>
    <mvc:mapping path="/**"/>
    <bean class="cn.mmd.micro.common.TokenInterceptor">
        <property name="excludeUrls">
            <list>
                <value>/app/token</value>
            </list>
        </property>
    </bean>
</mvc:interceptor>

/应用程序/令牌

我也测试了这个版本的配置,但我得到的空指针与当前较短版本相同。
@rinzler:我正在解决这个问题:W“使用这个配置,它正在工作,但只适用于请求服务器地址/任何东西。对于请求serveraddress/anything/something,未调用postHandle。“是的,我知道。使用
/*
它是否工作,因为postHandle只调用一次。第一次打电话没关系。但对于第二个和更多的调用,会抛出空指针异常。使用
/**
时,postHandle再次被调用多次。您是否应该发布空指针。您的解决方案有效,而“/**”无效?--看起来像个虫子,没错。只有“/**”对我来说不起作用。
<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/*"/>
        <bean class="cn.mmd.micro.common.TokenInterceptor">
            <property name="excludeUrls">
                <list>
                    <value>/app/token</value>
                </list>
            </property>
        </bean>
    </mvc:interceptor>
</mvc:interceptors>
<mvc:interceptors>
<mvc:interceptor>
    <mvc:mapping path="/**"/>
    <bean class="cn.mmd.micro.common.TokenInterceptor">
        <property name="excludeUrls">
            <list>
                <value>/app/token</value>
            </list>
        </property>
    </bean>
</mvc:interceptor>