1

アプリケーションのパフォーマンスを監視するために、Spring フレームワーク 4.0.2 + Spring MVC、JavaMelody を使用してアプリケーションを構築しました。LoginInterceptor を使用すると、ログイン セッションのない他のすべてのリクエスト URL がログイン ページにリダイレクトされます。Interceptor を Filter に置き換える必要はありますか? RequestParameterFilter に設定されたブレークポイントは /monitoring でうまく機能するため...助けてください、ありがとう。

コード参考までに。

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>

spring-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> 
4

1 に答える 1