Three ways to implement interface verification in Java
This paper introduces three ways to implement interface verification in Java, mainly including AOP and MVC interceptor. The details are as follows:
Method 1: AOP
The code below defines a permission annotation
Get ID token in page request
The token authentication class stores redis
Spring AOP configuration
The permission verification method can be directly annotated with accesstoken
for example
Method 2: AOP method 2
1. Define a query parent class, which contains the authToken and usedid attributes. All query parameters that need to verify the user's request inherit this query parent class. The reason for this userid is that after we verify the user, we need to obtain some user data according to the user ID, so we backfill this parameter in the AOP layer, This will not affect the previous code logic (this may be related to my business requirements)
2. Define a method level annotation. Add this annotation to all requests requiring verification for AOP interception (of course, you can also intercept all controller requests)
3. In AOP processing, the annotation is passed in as a parameter because the type attribute of the annotation can be used to distinguish the verification of multiple apps
4. Finally, configure the AOP in the configuration file (because our spring version is a little different from the aspect version, we can't use annotation based method)
Finally, the test code is given, so the code is much more elegant
Method 3: MVC interceptor
The server:
Splice all parameters except the token, and finally splice the token_ Key, do MD5 and compare it with the token parameter
If the token comparison fails, the status code 500 is returned
spring-config. XML configuration
client:
Splice all parameters of the request interface, and finally splice the token_ Key, do MD5 as the token parameter
Request example: http://127.0.0.1:8080/interface/api?key0=param0&key1=param1&token=md5 (concat(param0,param1))
The API test page uses bootstrap and angularjs, as well as a hex of JS_ MD5 function
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.