Spring Boot Configuration Filter Filters

Source: Internet
Author: User
Tags prepare

The filter filter is an important component in web development, and below is a session login example of how to use the filter in spring boot

The first thing to do is to prepare a class sessionfilter that implements the filter's interface:

Import Org.slf4j.LoggerFactory;
Import javax.servlet.*;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import javax.servlet.http.HttpSession;
Import java.io.IOException;
Import java.util.ArrayList;
Import java.util.List;
Import Java.util.regex.Matcher;

Import Java.util.regex.Pattern;
 /** * Created by Mazhenhua on 2016/12/27. * * Filter * */public class Sessionfilter implements filter {private static final Org.slf4j.Logger Logger = Loggerfacto


    Ry.getlogger (Sessionfilter.class);

    /** * Package, no filtered list of lists */protected static list<pattern> patterns = new arraylist<pattern> (); @Override public void init (Filterconfig filterconfig) throws servletexception {} @Override public Vo ID doFilter (servletrequest servletrequest, Servletresponse servletresponse, Filterchain chain) throws IOException,
     servletexception {HttpServletRequest HttpRequest = (httpservletrequest) servletrequest;   HttpServletResponse HttpResponse = (httpservletresponse) servletresponse;
        Logger.info ("aaaaaaaaaa");
        String URL = Httprequest.getrequesturi (). substring (Httprequest.getcontextpath (). Length ());
        if (Url.startswith ("/") && url.length () > 1) {url = url.substring (1);
            } if (Isinclude (URL)) {Chain.dofilter (HttpRequest, HttpResponse);
        Return
            } else {HttpSession session = Httprequest.getsession (); if (Session.getattribute ("") = null) {//session exists Chain.dofilter (HttpRequest, Httprespon
                SE);
            Return } else {//session does not exist prepare for jump failure/* RequestDispatcher dispatcher = Request.getrequestdispatch
                    ER (path);
                Dispatcher.forward (request, response); */Chain.dofilter (HttpRequest, HttpResponse);
            Return }}} @OverriDe public void Destroy () {}/** * do I need to filter * @param URL * @return */Private Boolean
            Isinclude (String URL) {for (Pattern pattern:patterns) {Matcher Matcher = pattern.matcher (URL);
            if (Matcher.matches ()) {return true;
    }} return false; }

}

Actual development often have a lot of requests to directly request in, do not need authentication login, so the code to filter out the code of this request, loaded into the list is good.

    /**
     * Configure Filter
     * @return *
    /@Bean public
    Filterregistrationbean somefilterregistration () {
        Filterregistrationbean registration = new Filterregistrationbean ();
        Registration.setfilter (Sessionfilter ());
        Registration.addurlpatterns ("/*");
        Registration.addinitparameter ("ParamName", "paramvalue");
        Registration.setname ("Sessionfilter");
        return registration;
    }

    /**
     * Create a Bean
     * @return
     *
    /@Bean (name = "Sessionfilter") public
    Filter Sessionfilter () {
        return new Sessionfilter ();
    }

After the configuration of these two steps above, the filter is basically available.

To configure multiple filter and specify the order, refer to: When the Spring boot filter is configured for multiple execution order

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.