Springboot Login Blocker

Source: Internet
Author: User

1, preferred to create an handlerinterceptor to inherit the interceptor

Import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.springframework.web.servlet.handlerinterceptor;import org.springframework.web.servlet.modelandview;/** * Interceptor * /public class Myinterceptor implements handlerinterceptor{//call before request processing (@Override public Boolea before the Controller method call) N Prehandle (httpservletrequest httpservletrequest, HttpServletResponse httpservletresponse, Object o) throws Exception        {HttpSession session = Httpservletrequest.getsession (); String user = (string) session.getattribute ("User");        Gets the session information of the login if (user!=null) {return true;  } else{httpservletresponse.sendredirect (Httpservletrequest.getcontextpath () + "/login/index");        Not logged in auto jump interface return false; }}//Call after request processing, but before the view is rendered (after the Controller method call) @Override public void Posthandle (HttpServletRequest httpservletr Equest, HttpServletResponse httpservletresponse, Object o, Modelandview modelandview) Throws Exception {System.out.println ("Posthandle is called \ n"); }//is called after the end of the request, that is, after Dispatcherservlet renders the corresponding view (primarily for resource cleanup) @Override public void Aftercompletion (Httpserv Letrequest HttpServletRequest, HttpServletResponse httpservletresponse, Object o, Exception e) throws Exception {S    Ystem.out.println ("aftercompletion is called \ n"); }}

2, inherit the Webmvcconfigureadapter class, overwrite its addinterceptors interface, register the custom interceptor:

Import Org.springframework.context.annotation.configuration;import Org.springframework.web.servlet.config.annotation.interceptorregistry;import Org.springframework.web.servlet.config.annotation.WebMvcConfigurer, @Configurationpublic class Webmvcconfig Implements Webmvcconfigurer {    /**     * Register Interceptor *    /@Override public    void Addinterceptors ( Interceptorregistry registry) {//addpathpattern followed by intercept address, Excludepathpatterns followed by exclude intercept address registry.addinterceptor (new MyIn Terceptor ()). Addpathpatterns ("/**"). Excludepathpatterns ("/login/index"). Excludepathpatterns ("/login/login");     } }

  

In this way, we can implement login interception at the user request to the controller layer, all user requests will be intercepted, login judgment in the Prehandle method, return True to verify, otherwise fail

Springboot 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.