spring mvc custom method interceptor

Custom method interceptor @interface belongs to annotation annotation

import java.lang.annotation.ElementType;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.Retention;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AccessRequired {
	
}




Design: Inherit org.springframework.web.servlet.handler.HandlerInterceptorAdapter and override the preHandle method.

accomplish:

xxx-servlet.xml add:

 

 

<mvc:interceptors>
	<mvc:interceptor>
		<mvc:mapping path="/**"/>
		<bean class="com.xxx.FrontInterceptor"/>
	</mvc:interceptor>
</mvc:interceptors>



front-end interceptor
public class FrontInterceptor implrmrnts HandlerInterceptor{

private static  Logger logger=Logger.getLogger(FrontInterceptor.class )

@override
public boolean preHandle(HttpServletRequest request,HttpServletresponse,Object handler){


HandlerMethod handlerMethod = (HandlerMethod) handler;
		Method method = handlerMethod.getMethod();
		AccessRequired annotation = method.getAnnotation(AccessRequired.class);
		if (annotation != null) {
		   System.out.println("你遇到了:@AccessRequired");
		   String accessToken = request.getParameter("access_token");
			/**
			 * Do something
			 */
		    response.getWriter().write("没有通过拦截,accessToken的值为:" + accessToken);
		}
		// 没有注解通过拦截
		return true;
}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326175107&siteId=291194637