[27] springboot realizes the function of saving the current login information like the session through the threadLocal+ parameter parser

    The overall column of the springboot chapter: 


[1] springboot integrates swagger (super detailed

[2] springboot integrates swagger (custom) (super detailed)

[3] springboot integration token (super detailed)

[4] springboot integrates mybatis-plus (super detailed) (on)

[5] springboot integrates mybatis-plus (super detailed) (below)

[6] springboot integrates custom global exception handling

[7] springboot integrates redis (super detailed)

[8] springboot integrates AOP to realize log operation (super detailed)

[Nine] springboot integrated timing tasks (super detailed)

[10] springboot integrates redis to realize the startup service, that is, save the hotspot data in the global and redis (super detailed)

[Eleven] springboot integrates quartz to realize timing task optimization (super detailed)

[Twelve] springboot integrates thread pool to solve high concurrency (super detailed, keep you understanding)

[Thirteen] springboot integrates asynchronous calls and obtains return values ​​(super detailed)

[14] springboot integrates WebService (super detailed)

[Fifteen] springboot integrates WebService (about passing parameters) (super detailed)

[16] springboot integrates WebSocket (super detailed)

[Seventeen] springboot integrates WebSocket to realize chat room (super detailed)

[Eighteen] springboot implements custom global exception handling

[Nineteen] springboot integrates ElasticSearch actual combat (ten thousand characters)

[Twenty] springboot integration filter combat

[21] springboot integrates interceptors in actual combat and compares filters

[22] springboot integration activiti7 (1) practical demonstration

【23】springboot integrated spring business detailed explanation and actual combat

[24] springboot uses EasyExcel and thread pool to realize multi-threaded import of Excel data

[25] springboot integrates jedis and redisson Bloom filters to handle cache penetration

        In development, the function of obtaining the currently logged-in user is a necessary function, such as saving the creator and modifyer, etc. In the past, the set and get methods of the session (the parameter list of each method needs to be added with the HttpServletRequest request parameter) or frameworks such as spring security to obtain the current login information. Below I share a method currently used on the project.

        Qq exchange group navigation ——> 231378628

The general process is as follows:

Table of contents

1. ThreadLocal

二、HandlerMethodArgumentResolver

3. Practical Demo


1. ThreadLocal

        Thread local variables, after creating a thread variable, each thread can have its own copy of the variable for this variable, and each thread is its own copy of access, independent of other threads.

二、HandlerMethodArgumentResolver

        This interface is used to pre-process request parameters. It has the following two methods:

  • supportsParameter:  Determine whether the request parameter supports the parser (return true to indicate support, and the resolveArgument method will continue to be called, return false to indicate no support, and the resolveArgument method will not continue to be called after the parameter analysis is completed).
  • resolveArgument:  The specific implementation logic of parameter parsing, and finally returns the encapsulated object after the parameter parsing.

3. Practical Demo

        1. Interceptor

        The simulated front-end request carries a token. If 1 is passed, it is assumed that the user information obtained from the cache is Zhang San, otherwise it is Li Si. And stuff the user information into the thread variable.

       2. Global user class

        Adopt Hungry Chinese Singleton Pattern

        3. Custom annotations

        It is used in the parameter list to determine whether an interface supports a custom parameter parser.

        4. Custom parameter analysis

        Customize the parameter parsing class, integrate the HandlerMethodArgumentResolver interface, and rewrite its two methods, here to judge whether the method parameter contains our custom annotation, if it contains, it means that the parser is supported, return true, enter the following method execution . 

        The rewritten resolveArgument method returns the value of the thread variable to the parameter corresponding to the annotation.

        5. Configuration

        Write a configuration class, inherit the WebMvcConfigurer interface, rewrite the method of adding interceptor and adding parameter parser, and add a custom interceptor and parser.

        6. Test

        Test two methods to get the current login user information. 

Guess you like

Origin blog.csdn.net/weixin_56995925/article/details/126532539