Spring boot uses interceptors for user login interception

Source: Internet
Author: User

Login interception and permission interception implementations are similar to first customizing a "Defineadapter "class, this class I was used to put a custom configuration (such as custom request parameters, custom interceptors, etc.), integrated Webmvcconfigureradapter rewrite his addinterceptors method, Put your interceptor's implementation class You can join,The Defineadapter code is as follows:
@Configurationpublic class Defineadapter extends webmvcconfigureradapter { @Autowiredpublic roleinterceptor roleinterceptor;//user role blocker @Autowiredpublic logininterceptor logininterceptor;//user login blocker @Autowiredmeidarequestdatacustomargumentresolver meidarequestdatacustomargumentresolver;//Custom parameter binding //Custom parameter binding@Overridepublic void addargumentresolvers (listargumentresolvers) { Super. Addargumentresolvers (argumentresolvers); argumentresolvers. Add (meidarequestdatacustomargumentresolver); }//Custom Interceptor@Overridepublic void addinterceptors (interceptorregistry registry) { //user role blockerregistry. Addinterceptor (roleinterceptor). addpathpatterns ("/**"); //user login blockerregistry. Addinterceptor (logininterceptor). addpathpatterns ("/**"). addpathpatterns ("/*/login"). addpathpatterns ("/*/h5/login"). excludepathpatterns ("/switch/*"); Super. Addinterceptors (registry); }
( custom parameter bindings can be used without a tube, you just don't have that piece of code!!) ) Addinterceptors: Add the implementation logic class for your custom interceptoraddpathpatterns: Add the request path you want to intercept, and if there are multiple paths, continue addpathpatterns excludepathpatterns: Add a request path that you do not need to intercept My interceptor specific implementation logic is Logininterceptor This class, the code is as follows:
@Servicepublic class Logininterceptor extends Handlerinterceptoradapter {{    protected static final Log logger = LOGFAC Tory.getlog (logininterceptor.class);        public boolean prehandle (HttpServletRequest request, httpservletresponse response, Object handler) {                            String userId = Request.getparameter ("UserId");                if (Stringutils.isnotblank (userId)) {                return false;               }}

inherit the Handlerinterceptoradapter abstract class, implement the Prehandle method, and write your interception logic inside. Finish it!

Spring boot uses interceptors for user login interception

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.