Two-step configuration Spring MVC Interceptor Interceptor One interceptor first step to implement Handlerinterceptor interface The second step registers into spring
Two-step configuration Spring MVC Interceptor Interceptor A. Interceptor.
Unlike filters, interceptors are java-based reflection mechanisms, and filters are Java EE standards, in the form of function callbacks. Detailed differences See also: relationship and difference between Java filter and SPRINGMVC interceptor
In Springmvc, the Interceptor (Handlerinterceptor) is an interface (interface) that must be overridden in three ways:
Prehandler (): Before entering the handler, the use of identity authentication, identity authorization, login verification, such as identity authentication, the user did not login, interception is no longer down, the return value is False, you can intercept; otherwise, when true, the interception is not performed;
Posthandler (): After entering the handler method, to return to Modelandview before the execution, using the scene from the Modelandview parameters, for example, the common model data passed here to the view, you can also specify the view of the display;
Afterhandler (): Similar to the finally statement block or C + + destructor, after the execution of handler, generally used for exceptions, log processing, etc.;
Quote: http://blog.csdn.net/lablenet/article/details/50483674 First step: Implement Handlerinterceptor interface
example, all requests except/login are blocked:
Package site.gaoyisheng.filter;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Org.springframework.web.servlet.HandlerInterceptor;
Import Org.springframework.web.servlet.ModelAndView;
Import Site.gaoyisheng.pojo.User; public class Loginhandlerinterceptor implements Handlerinterceptor {@Override public boolean prehandle (HTTPSERVL Etrequest request, HttpServletResponse response, Object handler) throws Exception {String RequestUri
= Request.getrequesturi (); if (!requesturi.equalsignorecase ("/trans/login")) {//Description in edit page User currentUser = (user) Request
. GetSession (). getattribute ("CurrentUser");
if (CurrentUser!= null) {//Login successful user return true; else {//No login, turn to login interface Request.getrequestdispatcher ("/login"). Forward (request, response)
;
return false;
} else return true;
@Override public void Posthandle (HttpServletRequest request, httpservletresponse response, Object handler, Modelandview Modelandview) throws Exception {} @Override public void Aftercompletion (httpservletreq
Uest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {}}
Step two: Register in spring
In the Springmvc.xml configuration, register the interceptor.
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path= "/**"/>
<bean class= " Site.gaoyisheng.filter.LoginHandlerInterceptor "></bean>
</mvc:interceptor>
</MVC: Interceptors>
Source: Welcome to Exchange.
Https://github.com/timo1160139211/trans
Resources:
http://blog.csdn.net/lablenet/article/details/50483674
http://www.imooc.com/learn/498