Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to track unique Web request in Spring Boot?

I have a Spring Boot (MVC) java server. How can I track each unique Web request? For example, I'd like to know the following.

  1. How to get user agent of Web request (browser type, device such as mobile or desktop)
  2. How to get the IP address or location of the Web request?
  3. How to count number of Web request? For examppe, I'd like to count the request count in a synchronized fashion.
like image 529
code Avatar asked Jan 07 '23 11:01

code


1 Answers

Most of the things will be given by HttpServletRequest, using headers.

1. How to get user agent of Web request

User agent ? Exactly !

String userAgentInfo = request.getHeader("User-Agent");

also there is an API as utils to check the information.

2. How to get the IP address or location of the Web request?

Though I cannot point to the location information, we can get IP address information using varied headers.

  • X-Forwarded-For
  • Proxy-Client-IP
  • WL-Proxy-Client-IP
  • HTTP_X_FORWARDED_FOR
  • HTTP_X_FORWARDED
  • HTTP_X_CLUSTER_CLIENT_IP
  • HTTP_CLIENT_IP
  • HTTP_FORWARDED_FOR
  • HTTP_FORWARDED
  • HTTP_VIA
  • REMOTE_ADDR

Depending on the request type these can give you IP information.

3. How to count number of Web request?

Old fashioned Filter creation will help you in tracking the hit counts.

This can help you better.

If Spring-boot is the catch, the implementation is bit change in getting the values,

In the controller, using @RequestHeader(value="User-Agent") can help. Similarly for the others too.

like image 82
Vinay Veluri Avatar answered Jan 20 '23 00:01

Vinay Veluri