This article is participating in the Java Theme Month – Java Debug Notes EventActive link
preface
Note: The situation described in this example is the problem encountered in the development process of simple account (open source bookkeeping software), and the solution
Some API calls need to be recorded in the simple account and recorded to the database in the form of logs
The requirements are roughly as follows:
Graph TD User Request --> Get expense category User request --> wechat login Authorization --> Log
1. Obtain the cost category interface: this interface is invoked frequently and has no actual meaning. Therefore, call log 2 is not required. Wechat login and authorization interface: Because this interface interacts with wechat, related information must be recorded in the log table to facilitate the production environment to troubleshoot authorization failures
First, find the problem
Once the requirements are identified, the coding begins. When the project is started, the following error message appears:
Second, solve the problem
The following two points can be known from the error message in the figure above:
1. The error occurred in the process of GlobalMethodSecurityConfiguration this Bean creation
2. Create Bean error due to invalid parameter, ProceedingJoinPoint pointcut only supports surround notification
In accordance with point 2 above, replace the ProceedingJoinPoint with JoinPoint
Modify before:
Revised:
Then you can start the project normally
Third, the question is rechecked
Have you ever wondered why the stack error message doesn’t include a custom aspect class? Clearly is the section class write wrong ah, why there is no relevant information? With this doubt, let’s look down!
The following uses Spring source code as an entry point to analyze the problem from two aspects
1. Understand the GlobalMethodSecurityConfiguration
GlobalMethodSecurityConfiguration are defined as follows
@Configuration(proxyBeanMethods = false)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public class GlobalMethodSecurityConfiguration
implements ImportAware.SmartInitializingSingleton.BeanFactoryAware {
// AOP dynamic proxy
@Bean
public MethodInterceptor methodSecurityInterceptor(MethodSecurityMetadataSource methodSecurityMetadataSource) {
this.methodSecurityInterceptor = isAspectJ()
? new AspectJMethodSecurityInterceptor()
: new MethodSecurityInterceptor();
methodSecurityInterceptor.setAccessDecisionManager(accessDecisionManager());
methodSecurityInterceptor.setAfterInvocationManager(afterInvocationManager());
methodSecurityInterceptor
.setSecurityMetadataSource(methodSecurityMetadataSource);
RunAsManager runAsManager = runAsManager();
if(runAsManager ! =null) {
methodSecurityInterceptor.setRunAsManager(runAsManager);
}
return this.methodSecurityInterceptor;
}
// Execute the singleton after completion
@Override
public void afterSingletonsInstantiated(a) {
try {
initializeMethodSecurityInterceptor();
}
catch (Exception e) {
throw new RuntimeException(e);
}
PermissionEvaluator permissionEvaluator = getSingleBeanOrNull(
PermissionEvaluator.class);
if(permissionEvaluator ! =null) {
this.defaultMethodExpressionHandler
.setPermissionEvaluator(permissionEvaluator);
}
RoleHierarchy roleHierarchy = getSingleBeanOrNull(RoleHierarchy.class);
if(roleHierarchy ! =null) {
this.defaultMethodExpressionHandler.setRoleHierarchy(roleHierarchy);
}
AuthenticationTrustResolver trustResolver = getSingleBeanOrNull(
AuthenticationTrustResolver.class);
if(trustResolver ! =null) {
this.defaultMethodExpressionHandler.setTrustResolver(trustResolver);
}
GrantedAuthorityDefaults grantedAuthorityDefaults = getSingleBeanOrNull(
GrantedAuthorityDefaults.class);
if(grantedAuthorityDefaults ! =null) {
this.defaultMethodExpressionHandler.setDefaultRolePrefix( grantedAuthorityDefaults.getRolePrefix()); }}}Copy the code
2.Debug Finds the root cause of an error
Debug shows that the method’s parameters are validated after the aspect class is instantiated
The specific verification logic is as follows:
So why is GlobalMethodSecurityConfiguration error here?
If you are in the plane in the constructor of a class a breakpoint will find that the plane class to instantiate a before the BeanFactory GlobalMethodSecurityConfiguration to carry out the corresponding calibration
The specific flow chart is as follows:
Graph of TD A start [projects] -- > C (whether to cut class) C - > | | D F [instantiated to the IOC container] C - > | T | E [instantiation GlobalMethodSecurityConfiguration] E - > F [check] F Through -- - > | | D F - > not | | through start abnormal D - > start to normal
3. Solve problems
I believe that with steps 1 and 2, you should be able to solve this problem easily 😊
Spring is the only framework for JavaEE, and it is well thought out
Four,
If you share my passion for technology and root causes, please give my blog a thumbs up 8👍
Finally, I want to promote my open source project [simple account] (back end, front end, small program three open source), if you are interested, you can click Star~
Making address:
- Jane zhang backend
- Jane zhang front-end
- Simple account small procedures
Past the link
-
Brief account main function introduction
-
Brief introduction and deployment of back-end environment