春季3.0整合拦截Web请求(Integrating interceptor to web requ

2019-11-04 20:43发布

目前,我工作的一个Web项目和使用弹簧3.0注释基地控制器。

  1. 我试图用拦截登录。
  2. 没有网址可以直接打到但从登录使用的拦截器。

我可以写一个拦截器,不会放过任何人进入网站而不给予必要的参数进行登录。 但问题是,剩余的网页也可以直接访问。

我与你我共享servlet.xml中

我也想知道为什么我要defien URL映射得到拦截工作,否则它会进入一个无限循环。

请帮我解决这个问题,如果您有任何工作示例对于这样的要求,那么也请分享。

先感谢您。

用SpringMVC-servlet.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan
        base-package="com.waqas.app.controller" />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>



        <bean id="urlMapping"
        class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="urlMap">
            <map>
        <!--  -->   
            <entry key="contacts.htm"><ref bean="contactController" /></entry>
                    <entry key="users.htm"><ref bean="loginController" /></entry>

                            <entry key="hello.htm"><ref bean="helloController" /></entry>


            </map>
        </property>
    </bean>


    <bean name="contactController" class="com.waqas.app.controller.ContactController" />

        <bean name="helloController" class="com.waqas.app.controller.HelloWorldController" />


   <bean name="loginController" class="com.waqas.app.controller.LoginController" />



        <bean id="handlerMapping"
          class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="interceptors">
            <list>
                <ref bean="loginInterceptor"/>
            </list>
        </property>
          <property name="mappings">
            <value>

            *.htm=contactController

            </value>
        </property>

    </bean>



    <bean id="loginInterceptor"
          class="com.waqas.app.interceptor.LoginInterceptor">

    </bean>






</beans>

的index.html

<%@ include file="/WEB-INF/jsp/include.jsp" %>
<jsp:forward page="contacts.htm"></jsp:forward>

我也想对sepcific网址/ URL模式这个拦截器应用。

期待你的回复。

亲切的问候,qasibeat

Answer 1:

这是一个相当普遍的问题。 斯科特·墨菲创造了一个很好的弹簧插件,允许基于URL的控制器上指定的拦截器。 你可以从这里下载springplugins 。

另请参阅基于注解的Spring框架控制器拦截器配置

编辑:对于3.1+版本,请参阅我的回答这个问题控制访问控制器配置基于URL模式拦截。



Answer 2:

性能监视器拦截器实例。 它记录每次的请求接管2秒。

public class PerformanceMonitorHandlerInterceptor implements HandlerInterceptor {

        private static Logger logger = Logger.getLogger( PerformanceMonitorHandlerInterceptor.class );

        /** Default to not active and 2 seconds. */
        private boolean active = true;

        /** The threshold. */
        private long threshold = 2000;

        /**
         * Starts the StopWatch to time request.
         * @param handler the handler
         * @param request the request
         * @param response the response
         * @return true, if pre handle
         * @throws Exception the exception
         * @see org.springframework.web.servlet.HandlerInterceptor#preHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse,
         *      java.lang.Object)
         */
        @SuppressWarnings("unused")
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
                // Start Watch
                if ( this.active ) {
                        String name = this.createRequestTraceName( request );
                        StopWatch stopWatch = new StopWatch( name );
                        stopWatch.start( name );
                        StopWatchHolder.setStopWatch( stopWatch );
                }

                // Continue with request
                return true;
        }

        /**
         * Warn if method takes longer than threshold milliseconds.
         * @param handler the handler
         * @param request the request
         * @param modelAndView the model and view
         * @param response the response
         * @throws Exception the exception
         * @see org.springframework.web.servlet.HandlerInterceptor#postHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse,
         *      java.lang.Object, org.springframework.web.servlet.ModelAndView)
         */
        @SuppressWarnings("unused")
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
                if ( this.active ) {
                        // Stop Watch
                        StopWatch stopWatch = StopWatchHolder.getStopWatch();
                        stopWatch.stop();

                        // Check threshold and log if over
                        if ( stopWatch.getLastTaskTimeMillis() >= this.threshold ) {
                                logger.warn( stopWatch.shortSummary() );
                        }

                        // Set ThreadLocal to null
                        StopWatchHolder.clear();
                }
        }

        /**
         * Not implemented.
         * @param handler the handler
         * @param exception the exception
         * @param request the request
         * @param response the response
         * @throws Exception the exception
         * @see org.springframework.web.servlet.HandlerInterceptor#afterCompletion(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse,
         *      java.lang.Object, java.lang.Exception)
         */
        @SuppressWarnings("unused")
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) throws Exception {
        // not implemented
        }

        /**
         * Creates the request trace name.
         * @param request the request
         * @return trace name
         */
        protected String createRequestTraceName(HttpServletRequest request) {
                StringBuilder sb = new StringBuilder();
                sb.append( "url: [" );
                sb.append( request.getRequestURL() );
                sb.append( "]" );
                sb.append( "  query: [" );
                sb.append( request.getQueryString() );
                sb.append( "]" );
                return sb.toString();
        }

        /**
         * Sets the threshold.
         * @param threshold The threshold to set.
         */
        public void setThreshold(long threshold) {
                this.threshold = threshold;
        }

        /**
         * Sets the active.
         * @param active The active to set.
         */
        public void setActive(boolean active) {
                this.active = active;
        }
}

那么这个bean插入到你的bean名URL映射:

    <bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
            <property name="interceptors">
            <list>
                    <ref bean="performanceMonitorHandlerInterceptor" />
                    </list>
            </property>
    </bean>


文章来源: Integrating interceptor to web requests in spring 3.0