Your own implementation of the first spring MVC login Interceptor
Title: Deny login to the system, as long as the user is not logged in, then forward the user request to/login.do require users to log in
Implementation steps:
1. Add the login interception in the spring configuration file as follows:
Spring-web.xml
<Mvc:interceptors>
<!--Configuring the Login Blocker-
<Mvc:interceptor>
<Mvc: MappingPath="/**"/>//Intercept all requests
<Mvc: exclude-mappingPath= "/tologin"/>//Do not intercept requests related to login
<Mvc: exclude-mappingPath= "/login"/>
<Mvc: exclude-mappingPath= "/static/**"/>< Span style= "COLOR: #9876aa" > < Span style= "color: #808080; font-family: "Arial" "> < Span style= "COLOR: #9876aa" > //do not intercept static
<bean Class= "Com.hand.filter.LoginHandlerIntercepter"/>
</MVC:interceptor>
</mvc:interceptors>
Note: To use mvc:exclude-mapping, you must change the http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd of the XML file to Www.springframework.org/schema/mvc/spring-mvc-3.2.xsd "
2. Implement interceptors
< Span style= "COLOR: #6a8759" > < Span style= "COLOR: #e8bf6a" > < Span style= "COLOR: #e8bf6a" > < Span style= "COLOR: #bababa" >loginhandlerintercepter . java
public class LoginhandlerintercepterImplements Handlerinterceptor {
@Override
public BooleanPrehandle (HttpServletRequest HttpServletRequest, HttpServletResponse HttpServletResponse, Object O)Throws Exception {
if (Httpservletrequest.getsession (). getattribute ("Customer")! =NULL) {
return true;
}else {
//No landing, turn to landing screen
Httpservletrequest.getrequestdispatcher ("/tologin"). Forward (HttpServletRequest, HttpServletResponse);
return false;
"
}
@Override
public void posthandle (httpservletrequest Httpservletrequestthrows Exception {
}
@Override
public void Aftercompletion (httpservletrequest httpservletrequestthrows Exception {
}
}
Summary: There are many imperfections in this interceptor, but that's probably the way it's thought.
Spring MVC login Blocker