Spring MVC login Blocker

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.