spring mvc interceptor不能与javamelody一起工作

时间:2014-12-25 14:45:11

标签: spring spring-mvc java-melody

我用Spring框架4.0.2 + Spring MVC构建我的应用程序,JavaMelody用于监控应用程序性能。 使用LoginInterceptor,没有登录会话的每个其他请求URL将被重定向到登录页面,但javamelody仪表板除外:mydomain.com/monitoring; 我需要用Filter替换Interceptor吗?因为RequestParameterFilter中设置的断点适用于/ monitoring ... 请帮帮我,谢谢。

代码FYI。

LoginInterceptor.java

public class LoginInterceptor implements HandlerInterceptor {

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

        User user = (User)request.getSession().getAttribute(BmcConstants.LOGIN_USER);
        if(user==null){
            response.sendRedirect(request.getContextPath() + "/login");
            return false;
        }

        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
            Object handler, Exception exp)
            throws Exception {
    }

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

}

的web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" >
    <display-name>Servlet 3.0 Web Application</display-name>

    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>
            classpath:spring-config.xml
            classpath:net/bull/javamelody/monitoring-spring.xml
        </param-value>  
    </context-param>

    <filter>  
        <filter-name>encodingFilter</filter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
        <async-supported>true</async-supported>  
        <init-param>  
            <param-name>encoding</param-name>  
            <param-value>UTF-8</param-value>  
        </init-param>  
    </filter>  
    <filter-mapping>  
        <filter-name>encodingFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>

    <filter>  
        <filter-name>requestParameterFilter</filter-name>  
        <filter-class>xxx.xxx.xxx.filter.RequestParameterFilter</filter-class>  
        <async-supported>true</async-supported>  
    </filter>  
    <filter-mapping>  
        <filter-name>requestParameterFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>  

    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  

    <!-- Spring MVC servlet -->  
    <servlet>  
        <servlet-name>SpringMVC</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <init-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>classpath:spring-mvc.xml</param-value>  
        </init-param>  
        <load-on-startup>1</load-on-startup>  
        <async-supported>true</async-supported>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>SpringMVC</servlet-name>  
        <url-pattern>/</url-pattern>  
    </servlet-mapping>

</web-app>

弹簧mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
                        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd    
                        http://www.springframework.org/schema/context    
                        http://www.springframework.org/schema/context/spring-context-4.0.xsd    
                        http://www.springframework.org/schema/mvc    
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd ">

    <mvc:annotation-driven />
    <context:component-scan base-package="xxx.xxx.xxx" />

    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**" />
            <mvc:exclude-mapping path="/login"/>
            <bean class="xxx.xxx.xxx.interceptor.LoginInterceptor" />
        </mvc:interceptor>
    </mvc:interceptors>

    <bean id="mappingFastjsonHttpMessageConverter"
        class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
    </bean>

    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="mappingFastjsonHttpMessageConverter" />
            </list>
        </property>
    </bean>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/WEB-INF/views/" p:suffix=".jsp" />

</beans> 

1 个答案:

答案 0 :(得分:0)

尝试在web.xml中添加旋律过滤器:

<filter>
    <filter-name>monitoring</filter-name>
    <filter-class>net.bull.javamelody.MonitoringFilter</filter-class>
    <async-supported>true</async-supported>
</filter>

<filter-mapping>
    <filter-name>monitoring</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>