How spring projects count return codes for all requests-project safety Brush Statistics

Source: Internet
Author: User

Recently encountered a problem, a simple spring MVC project, but often by brush, malicious brute force crack communication protocol, constantly try to log on the system, in order to facilitate the real-time statistics of requests and the distribution of request return codes. The simplest logic: people are constantly wrong landing, when the number of times, indicating that there is a problem, someone in the malicious brush our system.

Then an important part of this problem is to count all the request return code, convenient offline big data classmate analysis, how to record the return code it? It is not always possible to define a log every time, log.info in each return.

Returnmap.put ("Result", "$"), Returnmap.put ("MSG", "Database query Error"), Errorlogger.error ("500, database query Error", e); return Returnmap;

This way is disgusting, redundant code a lump of a lump.

The way I think about it now is to use custom annotations and AOP to count return codes for return. For example: One day 20% of the request is successful, 80% is a failure, so it is convenient for us to analyze.

The procedure is as follows:

Custom annotations: Add this statistical annotation to the method that requires statistics

Package Com.annotation;import Java.lang.annotation.documented;import Java.lang.annotation.elementtype;import Java.lang.annotation.retention;import Java.lang.annotation.retentionpolicy;import java.lang.annotation.Target;/* * * @author */@Target ({elementtype.parameter, elementtype.method}) @Retention (retentionpolicy.runtime) @ Documentedpublic @interface controllermetric {String description () default ""; Aspectreturntypeenum type () default aspectreturntypeenum.string; String ReturnCode () default "result";}

The type of the annotation return value enum:

Package com.annotation;/** * @author * </br> * The return type defined when the controller layer is intercepted * The different return types are handled differently in Controlleraspect. */public enum Aspectreturntypeenum {Map, String, JSON, XML}

Defined AOP Facets:

package com.aop;import java.util.map;import javax.servlet.http.httpservletrequest;import  org.aspectj.lang.proceedingjoinpoint;import org.aspectj.lang.annotation.around;import  org.aspectj.lang.annotation.aspect;import org.aspectj.lang.annotation.pointcut;import  org.slf4j.logger;import org.slf4j.loggerfactory;import org.springframework.stereotype.component; import com.annotation.controllermetric;/** *  @author   */@Aspect @componentpublic  class controlleraspect {private static final logger logger =  Loggerfactory.getlogger (Controlleraspect.class); @Pointcut ("@annotation ( com.annotation.controllermetric)" ) Public void controlleraspect ()  {} @Around ("Controlleraspect () && @annotation (log)") public  object docountmetric (Proceedingjoinpoint pjp, controllermetric log)  throws  throwable {logger.debug ("Enter docountmetric proceedingjoinpoint  " + log.type ()  + ", " + log.returncode ()); O Bject[] args = pjp.getargs (); httpservletrequest req =  (HttpServletRequest)  args[0]; System.out.println ("the request uri for this proceedingjoinpoint is :  " + req.getrequesturi ()); Object object = pjp.proceed ();switch  (Log.type ())   {case map:map ret =  (JAVA.UTIL.MAP)  object; SYSTEM.OUT.PRINTLN (ret); Break;     default:}logger.debug ("Leave docountmetric  proceedingjoinpoint "); return object;}}

Usage in controller

@Controller @requestmapping ("/sdk/mobservice/device")/** * trusted Device related Controller * @author liuxiaoming * */public class Equipmentcontroller {private static Logger Moblog = Loggerfactory.getlogger ("Moblog");p rivate static Logger Errorlogger = Loggerfactory.getlogger (equipmentcontroller.class); @AutowiredEquipmentServiceImpl equipmentserviceimpl;@ Autowiredmobutil mobutil; @RequestMapping ("/bd") @ResponseBody @controllermetricpublic map<string, string> Bddevice (httpservletrequest request,httpservletresponse response) {map<string, string> returnMap = new HashMap <string, string> (); return Returnmap}

In this form, all the return codes will be intercepted, and then a log can be used to record it, and I am going to output it directly.

How the Spring project counts return codes for all requests-project security anti-brush statistics

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.