Reputation: 21
Is there any way to identify that two or more frequent requests are coming from exactly same browser/user-agent? I am using spring version 3.0 framework.
Upvotes: 0
Views: 1277
Reputation: 1008
You can add a cookie with a unique id to the browser which sent the requests.
Following requests will be sent with that cookie, and you can use its value to check if this browser made requests to your service.
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
@RestController
public class ExampleController {
Map<String, String> agents = new HashMap<>();
@GetMapping("/foo")
public void foo(HttpServletResponse response, @CookieValue("agent-id") String agentId) {
// If the cookie is not present in the browser, the value of agentId is null
if (agentId == null) {
String newAgentId = UUID.randomUUID().toString();
agents.put(newAgentId, "request specific information");
Cookie newAgentIdCookie = new Cookie("agent-id", newAgentId);
response.addCookie(newAgentIdCookie);
System.out.println("Now I know you");
} else if (agents.containsKey(agentId)) {
System.out.println(agents.get(agentId));
}
}
}
Upvotes: 1