Business logic: Write a public intercept class that filters the parameters passed into the controller
In order to invoke the interface security, each interface that needs to be called has a parameter Accesstoken for security validation
Note: First into the filter filters, then into the AOP, finally into the controller, we do things in the AOP filter controller parameters
PackageCom.xgt.config;ImportCom.xgt.common.BaseController;ImportCom.xgt.common.PcsResult;ImportCom.xgt.util.IpUtil;Importorg.apache.commons.lang.StringUtils;ImportOrg.aspectj.lang.ProceedingJoinPoint;ImportOrg.aspectj.lang.annotation.Around;ImportOrg.aspectj.lang.annotation.Aspect;ImportOrg.aspectj.lang.annotation.Pointcut;Importorg.aspectj.lang.reflect.MethodSignature;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;ImportOrg.springframework.beans.factory.annotation.Value;Importorg.springframework.stereotype.Component;ImportOrg.springframework.web.context.request.RequestContextHolder;Importorg.springframework.web.context.request.ServletRequestAttributes;Importjavax.servlet.http.HttpServletRequest;ImportJava.lang.reflect.Method;/*** Interceptor: Record the number of user interface operations *@authorcc*/@Aspect @component Public classControllerinterceptorextendsBasecontroller {Private Static FinalLogger Logger = Loggerfactory.getlogger (controllerinterceptor.class); @Value ("${spring.profiles.active}") PrivateString env;/*** Define interception rules: Block all classes below the Com.xgt.controller.bs package*/@Pointcut ("Execution (* com.xgt.controller.bs). *.*(..))") Public voidcontrollermethodpointcut () {}/*** Interceptor Specific implementation *@paramPJP *@returnJsonresult (The execution result of the intercepted method, or the error prompt that requires a login.) )*/@Around ("Controllermethodpointcut ()")//Specifies the interceptor rule, or you can simply "execution (* Com.xjj ...)" Write it in here . PublicPcsresult Interceptor (Proceedingjoinpoint PJP) {object[] paramvalues=Pjp.getargs (); String Accesstoken= ""; for(inti=0;i<paramvalues.length;i++){ if(accesstoken!=NULL) {Accesstoken= Paramvalues[0].tostring (); }} Object result=NULL; Try{String Accesstokenkey= "XXXX"; if(!accesstoken.equals (Accesstokenkey)) { returnNewresult (false). Setmessage ("Parameter Error"); } intRequestcount=0; if(Stringutils.isnotempty (Accesstokenkey)) {RequestCount++; if(requestcount>100){ returnNewresult (false). Setmessage ("The request is too fast, take a break and try again.")); } } if(Result = =NULL){ //under all normal circumstances, continue to execute the intercepted methodresult =pjp.proceed (); RequestCount++; } } Catch(Throwable e) {logger.error ("Exception:", E); returnNewresult (false). Setmessage ("" +e.getmessage ()); } return(pcsresult) result; }}
Add: Interception naming rules brief
1) means matching all methods
Execution (* * (..))
2) means matching all public methods in the Com.savage.server.UserService
Execution (Public* com. savage.service.userservice.* (..))
3) represents all methods that match the Com.savage.server package and its sub-packages
Execution (* com.savage.server). *.*(..))
Springboot Facets Control business logic