Spring MVC interceptor uses annotations to judge and control invalid bugs

Keywords: Spring

In the beginning, the interceptor was configured as follows:

 

<bean id="urlMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> 
	
		<property name="interceptors">
			<list>
				<bean class="com.moneyP2P.base.spring.interceptor.CheckLoginInterceptor"/>
				<bean class="com.moneyP2P.p2p.VSFP.app.spring.interceptor.TokenInterceptor">
					<property name="notAllowList">
					    <list>
							<value>/app/check/**</value>					        
					    </list>
					</property>
				</bean>
				<bean class="com.moneyP2P.p2p.VSFP.app.spring.controller.mfa.interceptor.AppAdminTokenInterceptor">
					<property name="notAllowList">
					    <list>
							<value>/appAdmin/check/**</value>					        
					    </list>
					</property>
				</bean>
			</list>
		</property>
	</bean>


Code:

 @SuppressWarnings("static-access")
    @Override
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
        // Defining return value variables
       
        Class<?> clazz = handler.getClass();
        // In this way, annotations can be obtained for judgment control
        CheckLogin checkLogin = clazz.getAnnotation(CheckLogin.class);

       . . . . . 





 

There is no problem with this sub configuration.

After that, the configuration changed

<!-- Discard the old version and use the new version below
	<bean id="urlMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> 
	-->

<bean id="urlMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
		<property name="interceptors">
			<list>
				<bean class="com.moneyP2P.base.spring.interceptor.CheckLoginInterceptor"/>
				<bean class="com.moneyP2P.p2p.VSFP.app.spring.interceptor.TokenInterceptor">
					<property name="notAllowList">
					    <list>
							<value>/app/check/**</value>					        
					    </list>
					</property>
				</bean>
				<bean class="com.moneyP2P.p2p.VSFP.app.spring.controller.mfa.interceptor.AppAdminTokenInterceptor">
					<property name="notAllowList">
					    <list>
							<value>/appAdmin/check/**</value>					        
					    </list>
					</property>
				</bean>
			</list>
		</property>
	</bean>

 

After using it for a long time, I found that the interceptor didn't work at all,

Results exclusion findings

CheckLogin checkLogin = clazz.getAnnotation(CheckLogin.class);

Obtained content, empty!!!

Test with colleagues and find that sometimes it works, sometimes it doesn't!!!

 

Baidu referred to

Annotation interceptor

And then the code changes

 @Override
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
       
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Class<?> clazz= handlerMethod.getBeanType();
      
        CheckLogin checkLogin = clazz.getAnnotation(CheckLogin.class);

        // Use getBeanType. If you use getBean, you may not

Posted by DarkerAngel on Wed, 01 Jan 2020 17:26:09 -0800