【问题标题】:spring security - identify browser/user-agent uniquelyspring security - 唯一识别浏览器/用户代理
【发布时间】:2020-08-12 04:40:52
【问题描述】:

有什么方法可以识别两个或更多频繁的请求来自完全相同的浏览器/用户代理?我正在使用 Spring 3.0 版框架。

【问题讨论】:

    标签: spring spring-mvc servlets spring-security user-agent


    【解决方案1】:

    您可以向发送请求的浏览器添加具有唯一 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));
            }
        }
    
    }
    

    【讨论】:

    • 如果用户请求来自两台机器。这些机器具有相同版本的相同浏览器。那么呢?
    • @bytegroup,现在回答你的问题了吗?
    • 感谢您的回答。你的回答肯定是有帮助的。但我不应该使用 cookie。
    • 您应该将其添加到问题中。无论如何,你应该阅读浏览器指纹识别(例如pixelprivacy.com/resources/browser-fingerprinting)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-15
    • 2015-02-28
    • 2014-11-28
    • 2012-09-25
    • 2010-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多