This article is a mirror article of machine translation, please click here to jump to the original article.

View: 28258|Reply: 2

[Source] Practical Operation: AOP in Spring Boot dynamically modifies the request parameter value

[Copy link]
Posted on 11/6/2021 12:15:55 PM | | | |
Requirements: The project needs to dynamically modify the request parameters, to assign values to the request parameters according to the current user's permissions, before the parameters enter the controller method, the modification of the request parameters must be completed, and when the controller calls the service layer, the request parameters can be used as conditions to query and modify the data. There are two cases of requesting parameters:

  • For users with high privileges, you can use the actual requested parameter values
  • For people with only partial permissions, the request parameter values are dynamically modified based on the information bound to the current user


At first, I wanted to implement it through a filter, but it was too cumbersome to serialize and deserialize the request content, and it also required a lot of logical judgment and performance loss. Fortunately, Spring provides AOP face-oriented functionality, which can be implemented with simple code.

First of all, the maven project pom.xml needs to introduce the spring-boot-starter-aop package, as follows:

Once the package is introduced, we can use the AOP feature, through@Aspectannotation.

Aspect supports 5 types of notification annotations:

@Before: Pre-notification, which is executed before the method is executed

@After: Post-notification, executed after the method is executed

@AfterRunning: Returns a notification, which is executed after the method returns a result

@AfterThrowing: Exception notification, after the method throws an exception

@Around: Wraparound notifications, around method execution

Create a Request.java generic request wrapper class:

Create a new UserInfo.java request specific data class:

If the current request header has an appid, the value of the request parameter appId will be modified, otherwise, the request parameter will not be modified, the code is as follows:

Create a new HomeController controller and add an interface with a request parameter containing Request<UserInfo>, the code is as follows:

In the case of testing without the appId request header, the request parameter appId is the actual value and has not been modified, as shown in the following figure:



If you try to add the appId request header, you can see that the appId request parameter has been successfully modified, as shown in the following figure:



Finally, attach the source code:The hyperlink login is visible.





Previous:Use iPerf3 to test device network communication speeds
Next:Install iDRAC Service Module software on VMware ESXi
 Landlord| Posted on 11/6/2021 12:21:32 PM |
In Spring AOP, business logic only focuses on the business itself, dividing logging, performance statistics, security control, transaction processing, exception handling, etc. from the business logic code, and by separating these behaviors, we hope to be able to separate them into methods that do not guide business logic, and then change these behaviors without affecting the code of business logic.
Posted on 3/15/2023 11:11:04 AM |
zsdfghjkvbjnm
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com