Spring MVC HandlerInterceptorAdapter

时间:2016-12-13 15:17:06

标签: java spring spring-mvc

我的Spring Web模型 - 视图 - 控制器(MVC)框架中有这个类。 Spring Web模型 - 视图 - 控制器(MVC)框架的版本是3.2.8。

这是我的servlet.xml

<bean id="sessionInterceptor" class="com.tdk.devices.support.context.SessionInterceptor" />


 <!-- Maps incoming URLs to classnames -->
    <bean name="controllerHandler" class="springext.web.servlet.mvc.support.ControllerClassNameHandlerMapping">        
        <property name="basePackage" value="com.tdk.devices.controller" />
        <property name="interceptors">
            <list>
                <!-- Checks a specific request parameter if the locale is changed. -->
                <ref bean="localeChangeInterceptor" />
                <ref bean="sessionInterceptor"      />
            </list>
        </property>
    </bean>

这是我的web.xml

   <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
        <display-name>devices</display-name>
        <description></description>
        <!-- This parameter specifies which log4j properties file to use for logging. -->
        <!-- Specifying this parameter will prevent conflicts with other frameworks that use the default log4j setup. -->
        <context-param>
                <param-name>log4jConfigLocation</param-name>
                <!-- param-value>classpath:com/tdk/devices/log4j.xml</param-value-->
                <param-value>classpath:com/tdk/devices/log4j.properties</param-value>
        </context-param>
        <!-- When the web app root is exposed, it is possible to set the log files in the web application context, -->
        <!-- which will allow the logs to be accessible in a browser. -->
        <!-- In this example we disable it for security reasons. -->
        <context-param>
                <param-name>log4jExposeWebAppRoot</param-name>
                <param-value>false</param-value>
        </context-param>
        <!-- The contextConfigLocation specifies all locations for the Spring bean factory to get its Spring bean definitions from. -->
        <context-param>
                <param-name>contextConfigLocation</param-name>
                    <param-value>       
                        classpath:com/tdk/devices/dao/dataAccessContext.xml
                        classpath:com/tdk/devices/services/impl/servicesContext.xml
                        /WEB-INF/applicationContext.xml 
                        /WEB-INF/dao/databaseMessageSource.xml
                        classpath:com/tdk/devices/controller/propertyeditors/propertyeditorsContext.xml
                </param-value>
        </context-param>

        <!-- Context init params to configure the Ecas client using Spring -->
        <!-- <context-param>
            <param-name>com.tdk.devices.client.filter.externalConfigurationKey</param-name>
            <param-value>devicesWeb.EcasConfigurationKey</param-value>
        </context-param>  -->

         <!-- ECAS security -->
    <!-- Here we specify which URLs are protected by ECAS and which application server role is used. -->
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>devicesWeb</web-resource-name>
            <description>Require users to authenticate</description>
            <url-pattern>*.do</url-pattern>

            <url-pattern>/newdesign/manage/manageapplications</url-pattern>    
            <url-pattern>/newdesign/manage/manageapplications/</url-pattern>
            <url-pattern>/newdesign/manage/manageapplications/*</url-pattern>
            <url-pattern>/newdesign/manage/manageapplications/*/</url-pattern>
            <url-pattern>/newdesign/manage/manageapplications/*/*</url-pattern>     
            <url-pattern>/newdesign/manage/manageapplications/*/*/</url-pattern>

            <url-pattern>/newdesign/manage/manageserviceapps</url-pattern>      
            <url-pattern>/newdesign/manage/manageserviceapps/</url-pattern>
            <url-pattern>/newdesign/manage/manageserviceapps/*</url-pattern>        
            <url-pattern>/newdesign/manage/manageserviceapps/*/</url-pattern>
            <url-pattern>/newdesign/manage/manageserviceapps/*/*</url-pattern>      
            <url-pattern>/newdesign/manage/manageserviceapps/*/*/</url-pattern>

            <url-pattern>/newdesign/manage/manageproductapps</url-pattern>
            <url-pattern>/newdesign/manage/manageproductapps/</url-pattern>
            <url-pattern>/newdesign/manage/manageproductapps/*</url-pattern>        
            <url-pattern>/newdesign/manage/manageproductapps/*/</url-pattern>
            <url-pattern>/newdesign/manage/manageproductapps/*/*</url-pattern>      
            <url-pattern>/newdesign/manage/manageproductapps/*/*/</url-pattern>

            <!-- <url-pattern>/newdesign/viewapplications/</url-pattern>                            
            <url-pattern>/newdesign/viewapplications/*</url-pattern>
            <url-pattern>/newdesign/viewapplications/*/</url-pattern>
            <url-pattern>/newdesign/viewapplications/*/*</url-pattern>
            <url-pattern>/newdesign/viewapplications/*/*/</url-pattern -->          

            <url-pattern>/newdesign/manage/home</url-pattern>    
            <url-pattern>/newdesign/manage/home/</url-pattern>
            <url-pattern>/newdesign/manage/home/*</url-pattern> 

            <url-pattern>*.jsp</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <description>ensures that the principal is authenticated</description>
            <role-name>*</role-name>
        </auth-constraint>
        <user-data-constraint>
            <description>Encryption is not required for the application in general.</description>
            <transport-guarantee>NONE</transport-guarantee>
        </user-data-constraint>
    </security-constraint>
    <!-- Here we specify the ECAS login action URL (called after successful ECAS login) and the ECAS error action URL. -->
        <login-config>
          <auth-method>FORM</auth-method> 
          <realm-name>myrealm</realm-name> 
            <form-login-config>
                  <form-login-page>/protected/login</form-login-page> 
                  <form-error-page>/ecas/ecasError.do</form-error-page> 
              </form-login-config>
      </login-config>


        <security-role>
                <description>Syntetic role that indicates if the principal is authenticated</description>
                <role-name>authenticated</role-name>
        </security-role>


        <!-- spring security csrf -->
        <filter>
            <filter-name>springSecurityFilterChain</filter-name>
            <filter-class>com.tdk.devices.support.context.devicesSecurityFilter</filter-class>
        </filter>

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




        <!-- The request filter puts the attribute 'requestUrl' in the session, which contains the action URL used for the request. -->
        <filter>
                <filter-name>Request url filter</filter-name>
                <filter-class>eu.europa.ec.ergonomics.filter.RequestUrlFilter</filter-class>
        </filter>
        <!-- The event dispatcher filter allows ergonomics tags to fire and react to user interface events. The most important feature is auto-saving the state of the UI component. -->
        <filter>
                <filter-name>Event dispatcher filter</filter-name>
                <filter-class>eu.europa.ec.ergonomics.filter.EventDispatcherFilter</filter-class>
        </filter>
        <!-- The ThreadContextFilter gets the userid from the HttpServletRequest and associates it with the current Thread. -->
        <!-- This can be very useful when a class in the service layer or the persistence layer needs the userid, but hasn't got -->
        <!-- the HttpServletRequest to get the userid from.-->
        <!-- filter>
                <filter-name>Thread context filter</filter-name>
                <filter-class>com.tdk.devices.support.context.ThreadContextFilter</filter-class>
        </filter-->

        <!-- This filter sets the character encoding of all values coming from the web browsers to UTF-8. -->
        <filter>
                <filter-name>Character encoding filter</filter-name>
                <filter-class>eu.europa.ec.ergonomics.filter.CharacterEncodingFilter</filter-class>
        </filter>
        <filter>
            <filter-name>Excel export filter</filter-name>    
            <filter-class>eu.europa.ec.ergonomics.taglib.datagrid.export.ExcelExportFilter</filter-class>
        </filter>
        <filter-mapping>
                <filter-name>Request url filter</filter-name>
                <url-pattern>/*</url-pattern>
        </filter-mapping>
        <filter-mapping>
                <filter-name>Event dispatcher filter</filter-name>
                <url-pattern>*.do</url-pattern>
                <url-pattern>/newdesign/manage/manageapplications</url-pattern>    
            <url-pattern>/newdesign/manage/manageapplications/</url-pattern>
            <url-pattern>/newdesign/manage/manageapplications/*</url-pattern>
            <url-pattern>/newdesign/manage/manageapplications/*/</url-pattern>
            <url-pattern>/newdesign/manage/manageapplications/*/*</url-pattern>     
            <url-pattern>/newdesign/manage/manageapplications/*/*/</url-pattern>

            <url-pattern>/newdesign/manage/manageserviceapps</url-pattern>      
            <url-pattern>/newdesign/manage/manageserviceapps/</url-pattern>
            <url-pattern>/newdesign/manage/manageserviceapps/*</url-pattern>        
            <url-pattern>/newdesign/manage/manageserviceapps/*/</url-pattern>
            <url-pattern>/newdesign/manage/manageserviceapps/*/*</url-pattern>      
            <url-pattern>/newdesign/manage/manageserviceapps/*/*/</url-pattern>

            <url-pattern>/newdesign/manage/manageproductapps</url-pattern>
            <url-pattern>/newdesign/manage/manageproductapps/</url-pattern>
            <url-pattern>/newdesign/manage/manageproductapps/*</url-pattern>        
            <url-pattern>/newdesign/manage/manageproductapps/*/</url-pattern>
            <url-pattern>/newdesign/manage/manageproductapps/*/*</url-pattern>      
            <url-pattern>/newdesign/manage/manageproductapps/*/*/</url-pattern>
        </filter-mapping>
        <filter-mapping>
                <filter-name>Thread context filter</filter-name>
                <url-pattern>/*</url-pattern>
        </filter-mapping>
        <filter-mapping>
                <filter-name>Character encoding filter</filter-name>
                <url-pattern>*.do</url-pattern>
                <url-pattern>/newdesign/manage/manageapplications</url-pattern>    
            <url-pattern>/newdesign/manage/manageapplications/</url-pattern>
            <url-pattern>/newdesign/manage/manageapplications/*</url-pattern>
            <url-pattern>/newdesign/manage/manageapplications/*/</url-pattern>
            <url-pattern>/newdesign/manage/manageapplications/*/*</url-pattern>     
            <url-pattern>/newdesign/manage/manageapplications/*/*/</url-pattern>

            <url-pattern>/newdesign/manage/manageserviceapps</url-pattern>      
            <url-pattern>/newdesign/manage/manageserviceapps/</url-pattern>
            <url-pattern>/newdesign/manage/manageserviceapps/*</url-pattern>        
            <url-pattern>/newdesign/manage/manageserviceapps/*/</url-pattern>
            <url-pattern>/newdesign/manage/manageserviceapps/*/*</url-pattern>      
            <url-pattern>/newdesign/manage/manageserviceapps/*/*/</url-pattern>

            <url-pattern>/newdesign/manage/manageproductapps</url-pattern>
            <url-pattern>/newdesign/manage/manageproductapps/</url-pattern>
            <url-pattern>/newdesign/manage/manageproductapps/*</url-pattern>        
            <url-pattern>/newdesign/manage/manageproductapps/*/</url-pattern>
            <url-pattern>/newdesign/manage/manageproductapps/*/*</url-pattern>      
            <url-pattern>/newdesign/manage/manageproductapps/*/*/</url-pattern>
        </filter-mapping>
        <filter-mapping>    
            <filter-name>Excel export filter</filter-name>    
            <url-pattern>*.do</url-pattern>
        </filter-mapping> 


        <!-- The context loader listener loads the Spring contexts. -->
        <listener>
                <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <listener>
                <listener-class>
                        org.springframework.web.context.request.RequestContextListener
                </listener-class>
        </listener>

         <!-- The Log4jConfigListener loads the log4j configuration and sets it up. -->
        <listener>
            <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
        </listener>  


        <!-- The dispatcher servlet maps the action URL to the controller and processes the request. -->
        <servlet>
        <servlet-name>checkAppStatusServlet</servlet-name>
        <servlet-class>com.tdk.devices.support.CheckAppStatusServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
        </servlet>

         <servlet-mapping>
        <servlet-name>checkAppStatusServlet</servlet-name>
        <url-pattern>/CheckAppStatus</url-pattern>
        </servlet-mapping>


        <!-- The dispatcher servlet maps the action URL to the controller and processes the request. -->
        <servlet>
                <servlet-name>devicesWeb</servlet-name>
                <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        
                <load-on-startup>0</load-on-startup>
        </servlet>

        <servlet-mapping>
                <servlet-name>devicesWeb</servlet-name>
                <url-pattern>*.do</url-pattern>

                <url-pattern>/newdesign/welcome</url-pattern>
                <url-pattern>/newdesign/welcome/</url-pattern>

                <url-pattern>/newdesign/manage/manageapplications</url-pattern>    
                <url-pattern>/newdesign/manage/manageapplications/</url-pattern>
                <url-pattern>/newdesign/manage/manageapplications/*</url-pattern>
                <url-pattern>/newdesign/manage/manageapplications/*/</url-pattern>
                <url-pattern>/newdesign/manage/manageapplications/*/*</url-pattern>     
                <url-pattern>/newdesign/manage/manageapplications/*/*/</url-pattern>

                <url-pattern>/newdesign/manage/manageserviceapps</url-pattern>      
                <url-pattern>/newdesign/manage/manageserviceapps/</url-pattern>
                <url-pattern>/newdesign/manage/manageserviceapps/*</url-pattern>        
                <url-pattern>/newdesign/manage/manageserviceapps/*/</url-pattern>
                <url-pattern>/newdesign/manage/manageserviceapps/*/*</url-pattern>      
                <url-pattern>/newdesign/manage/manageserviceapps/*/*/</url-pattern>

                <url-pattern>/newdesign/manage/manageproductapps</url-pattern>
                <url-pattern>/newdesign/manage/manageproductapps/</url-pattern>
                <url-pattern>/newdesign/manage/manageproductapps/*</url-pattern>        
                <url-pattern>/newdesign/manage/manageproductapps/*/</url-pattern>
                <url-pattern>/newdesign/manage/manageproductapps/*/*</url-pattern>      
                <url-pattern>/newdesign/manage/manageproductapps/*/*/</url-pattern>

                <!-- <url-pattern>/newdesign/viewapplications/</url-pattern>                            
                <url-pattern>/newdesign/viewapplications/*</url-pattern>
                <url-pattern>/newdesign/viewapplications/*/</url-pattern>
                <url-pattern>/newdesign/viewapplications/*/*</url-pattern>
                <url-pattern>/newdesign/viewapplications/*/*/</url-pattern -->          

                <url-pattern>/newdesign/manage/home</url-pattern>    
                <url-pattern>/newdesign/manage/home/</url-pattern>
                <url-pattern>/newdesign/manage/home/*</url-pattern> 


                <!-- Explicitly mention /welcome.do for usage as welcome page -->
                <url-pattern>/welcome/welcome.do</url-pattern>
        </servlet-mapping>

        <servlet-mapping>
                <servlet-name>devicesWeb</servlet-name>
                <url-pattern>/newdesign/viewapplications/mt-gov/awarded</url-pattern>
                <url-pattern>/devices/newdesign/viewapplications/mt-gov/awarded</url-pattern>
                <url-pattern>newdesign/viewapplications/mt-gov/awarded</url-pattern>                                
        </servlet-mapping>

        <servlet>
                <servlet-name>displayImage</servlet-name>
                <servlet-class>com.tdk.devices.utils.DisplayImageServlet</servlet-class>
        </servlet>
        <servlet-mapping>
                <servlet-name>displayImage</servlet-name>
                <url-pattern>/displayImage</url-pattern>
        </servlet-mapping>
        <!-- The welcome file is the first file to call when the root URL is used. -->
        <!-- This index.jsp file will forward to the welcome action URL. -->
        <welcome-file-list>
                <welcome-file>welcome/welcome.do</welcome-file>
        </welcome-file-list>
        <!-- The error page maps a HTTP error caught in the webserver to an action URL. -->
        <!-- We forward a '404: page not found' error to an error page that explains the error to the user. -->
        <error-page>
                <error-code>404</error-code>
                <location>/errors/error404.do</location>
        </error-page>
        <!-- We forward a '500: internal server error' to an error page that explains the error to the user. -->
        <error-page>
                <exception-type>java.lang.Throwable</exception-type>
                <location>/errors/error500.do</location>
        </error-page>




        <session-config>
            <session-timeout>60</session-timeout>
        </session-config>

        <jsp-config>
        <taglib>
            <taglib-uri>http://www.owasp.org/index.php/Category:OWASP_Enterprise_Security_API</taglib-uri>
            <taglib-location>/WEB-INF/tld/esapi.tld</taglib-location>
        </taglib>
        </jsp-config>

</web-app>

这是我的SessionInterceptor

public class SessionInterceptor extends HandlerInterceptorAdapter {

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

        // sometimes no session produces an exception:
        if (!response.isCommitted() && ex != null) {
             if(sessionNeeded (request, handler)) {
                request.getRequestDispatcher(
                        "/WEB-INF/jsp/errors/noSession.jsp").forward(request,
                        response);
            }
        }
        super.afterCompletion(request, response, handler, ex);
    }


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

         if(sessionNeeded (request, handler)) {
             throw new SessionExpiredException();
         }
         super.postHandle(request, response, handler, modelAndView);
    }



    private boolean sessionNeeded (HttpServletRequest request, Object handler) {

        System.out.println  ("***************************");
        System.out.println  ("Object handler ---> " + handler);
        System.out.println  ("***************************");

        HttpSession session = request.getSession(false);
        return (session == null || session.getAttribute(Const.SESSION_USER) == null) && 
                    !(handler instanceof LogoutController) &&
                        !(handler instanceof Error404Controller) &&
                            !(handler instanceof WelcomeController) &&
                                !(handler instanceof CBHomeController) &&                                                       
                                    !(handler instanceof Error500Controller);
    }

}

这是1个控制器

@Controller
public class CBHomeController extends CBController  {

    public static final Logger LOGGER = Logger.getLogger    (CBHomeController.class);


    /** 
     * 
     */
    @RequestMapping(value = { "/newdesign/manage/home", 
                              "/newdesign/manage/home/"}, method = {RequestMethod.GET})
    public String cbHome    (HttpServletRequest request, Model model ) throws ExecutionException {

        System.out.println  ("*** CBHomeController ***");


        return "cbHomeView";
    }

}

当我把它放在网址

http://127.0.0.1:7001/devices/newdesign/manage/home

JSP中的一切都很好,因为我看到了主页,但这是我在控制台中看到的:

*** CBHomeController ***
***************************
Object handler ---> com.tdk.devices.controller.errors.Error404Controller@633c91
***************************

1 个答案:

答案 0 :(得分:0)

您确定不仅仅是请求fav.ico的浏览器。

端口7001表示weblogic,系统out表示您不能设置断点并检查请求URL。也许我的假设是错误的;如果我不是,那么设置一个可以设置断点的环境应该是您的首要任务,从长远来看,它将为您节省数小时 - 并保存您忘记删除的所有系统中的文件空间: - )

此外,我对你的拦截器处理程序非常感兴趣,我从来没有像这样需要,通常会在需要时自动创建会话,例如Spring安全性。你能详细说明这个特殊的拦截器吗,对我来说看起来很奇怪。