Spring3 MVC interceptor

Source: Internet
Author: User
Tags date now

Here I define a global Interceptor: [html] view plaincopy <! -- Interceptor --> <mvc: interceptors> <mvc: interceptor> <mvc: mapping path = "/*. do "/> <bean class =" com. log. report. interceptor. accessStatisticsIntceptor "/> </mvc: interceptor> </mvc: interceptors> is defined in the servlet-config.xml file. The following is an interceptor used to record access ip addresses and traffic. The interceptor must implement the HandlerInterceptor interface and implement corresponding methods. In the PreHandler method, if the returned value is false, the request is blocked. [Java] import java. text. simpleDateFormat; import java. util. date; import java. util. hashMap; import java. util. map; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import org. springframework. web. servlet. handlerInterceptor; import org. springframework. web. servlet. modelAndView; public class AccessStatisticsIntceptor implements HandlerInterceptor {public stat Ic long access_num = 0; public static Map <String, Long> IPMap = new HashMap <String, Long> (); public static Map <String, long> urlMap = new HashMap <String, Long> (); @ Override public void afterCompletion (HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception {// TODO Auto-generated method stub} @ Override public void postHandle (HttpServletRequest arg0, HttpServl EtResponse arg1, Object arg2, ModelAndView arg3) throws Exception {// TODO Auto-generated method stub} @ Override public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {access_num + = 1; SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMdd"); Date now = new Date (); String ip = request. getHeader ("x-forwarded-for"); if (ip = null | ip. len Unknown () = 0 | "unknown ". equalsIgnoreCase (ip) {ip = request. getHeader ("Proxy-Client-IP");} if (ip = null | ip. length () = 0 | "unknown ". equalsIgnoreCase (ip) {ip = request. getHeader ("WL-Proxy-Client-IP");} if (ip = null | ip. length () = 0 | "unknown ". equalsIgnoreCase (ip) {ip = request. getRemoteAddr ();} String url = request. getRequestURI ()! = Null? Request. getRequestURI (). toString (): null; url = sdf. format (now) + "-" + url; if (url! = Null & urlMap. containsKey (url) {urlMap. put (url, urlMap. get (url) + 1);} else if (url! = Null) {urlMap. put (url, 1l);} String key = sdf. format (now) + "-" + ip; if (IPMap. containsKey (key) {IPMap. put (key, IPMap. get (key) + 1);} else {IPMap. put (key, 1l) ;}return true ;}}

Related Article

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.