有什么方法可以识别两个或两个以上的频繁请求来自完全相同的浏览器用户代理?我使用的是spring 3.0版本的框架。
你可以给发送请求的浏览器添加一个带有唯一ID的cookie。
随后的请求将通过该cookie发送,您可以使用它的值来检查该浏览器是否向您的服务发出请求。
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));
}
}
}