有没有办法从Java中的响应对象读取cookie?

发布于 2024-09-14 22:57:55 字数 331 浏览 5 评论 0原文

HttpServletResponse 似乎没有公开任何方法来执行此操作。

现在,我正在将一堆日志记录代码添加到一个粗糙且难以理解的 servlet 中,试图弄清楚它到底做了什么。我知道它会设置一堆cookie,但我不知道何时、为什么或什么。如果在 servlet 执行结束时将所有 cookie 记录在 HttpServletResponse 对象中,那就太好了。

我知道 cookie 通常是浏览器的责任,而且我记得在 .NET 中没有办法做到这一点。只是希望 Java 可能有所不同......

但如果这是不可能的 - 关于如何完成我想做的事情还有其他想法吗?

谢谢,一如既往。

It doesn't seem that HttpServletResponse exposes any methods to do this.

Right now, I'm adding a bunch of logging code to a crufty and ill-understood servlet, in an attempt to figure out what exactly it does. I know that it sets a bunch of cookies, but I don't know when, why, or what. It would be nice to just log all the cookies in the HttpServletResponse object at the end of the servlet's execution.

I know that cookies are typically the browser's responsibility, and I remember that there was no way to do this in .NET. Just hoping that Java may be different...

But if this isn't possible -- any other ideas for how to accomplish what I'm trying to do?

Thanks, as always.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

闻呓 2024-09-21 22:57:55

如果您只需要记录日志,那么我建议编写一个 javax.servlet.Filter 的实现,它将提供的 HttpServletResponse 包装在一个包装器中,该包装器允许您公开 cookie过滤器执行后。像这样的事情:

public class CookieLoggingFilter implements Filter {

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException ,ServletException {
        ResponseWrapper wrappedResponse = new ResponseWrapper((HttpServletResponse) response);

        filterChain.doFilter(request, wrappedResponse);

        // use a real logger here, naturally :)
        System.out.println("Cookies: " + wrappedResponse.cookies); 
    }

    private class ResponseWrapper extends HttpServletResponseWrapper {

        private Collection<Cookie> cookies = new ArrayList<Cookie>();

        public ResponseWrapper(HttpServletResponse response) {
            super(response);
        }

        @Override
        public void addCookie(Cookie cookie) {
            super.addCookie(cookie);
            cookies.add(cookie);
        }
    }

       // other methods here
}

一个重要的警告:这不会向您显示哪些 cookie 被发送回浏览器,它只会向您显示应用程序代码添加到响应中的 cookie。如果容器选择更改、添加或忽略这些 cookie(例如,会话 cookie 由容器而不是应用程序处理),您将不知道使用此方法。但这对您的情况可能并不重要。

唯一确定的方法是使用浏览器插件,例如 Live Http Headers for Firefox 或 man-in -中间 HTTP 日志记录代理。

If logging is all you're after, then I suggest writing an implemention of javax.servlet.Filter which wraps the supplied HttpServletResponse in a wrapper which allows you to expose the cookies after the filter executes. Something like this:

public class CookieLoggingFilter implements Filter {

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException ,ServletException {
        ResponseWrapper wrappedResponse = new ResponseWrapper((HttpServletResponse) response);

        filterChain.doFilter(request, wrappedResponse);

        // use a real logger here, naturally :)
        System.out.println("Cookies: " + wrappedResponse.cookies); 
    }

    private class ResponseWrapper extends HttpServletResponseWrapper {

        private Collection<Cookie> cookies = new ArrayList<Cookie>();

        public ResponseWrapper(HttpServletResponse response) {
            super(response);
        }

        @Override
        public void addCookie(Cookie cookie) {
            super.addCookie(cookie);
            cookies.add(cookie);
        }
    }

       // other methods here
}

One big caveat: This will not show you what cookies are sent back to the browser, it will only show you which cookies the application code added to the response. If the container chooses to change, add to, or ignore those cookies (e.g. session cookies are handled by the container, not the application), you won't know using this approach. But that may not matter for your situation.

The only way to be sure is to use a browser plugin like Live Http Headers for Firefox, or a man-in-the-middle HTTP logging proxy.

め七分饶幸 2024-09-21 22:57:55

我遇到了同样的问题,我使用接受 HttpServletResponse 的第三方库,并且我需要读回它在我的响应对象上设置的 cookie。为了解决这个问题,我创建了一个 HttpServletResponseWrapper 扩展,它在我拨打电话后为我公开这些 cookie:

public class CookieAwareHttpServletResponse extends HttpServletResponseWrapper {

    private List<Cookie> cookies = new ArrayList<Cookie>();

    public CookieAwareHttpServletResponse (HttpServletResponse aResponse) {
        super (aResponse);
    }

    @Override
    public void addCookie (Cookie aCookie) {
        cookies.add (aCookie);
        super.addCookie(aCookie);
    }

    public List<Cookie> getCookies () {
        return Collections.unmodifiableList (cookies);
    }

} 

以及我使用它的方式:

// wrap the response object
CookieAwareHttpServletResponse response = new CookieAwareHttpServletResponse(aResponse);

// make the call to the 3rd party library 
String order = orderService.getOrder (aRequest, response, String.class);

// get the list of cookies set
List<Cookie> cookies = response.getCookies();

I had the same problem where I was using a 3rd party library which accepts an HttpServletResponse and I needed to read back the cookies that it set on my response object. To solve that I created an HttpServletResponseWrapper extension which exposes these cookies for me after I make the call:

public class CookieAwareHttpServletResponse extends HttpServletResponseWrapper {

    private List<Cookie> cookies = new ArrayList<Cookie>();

    public CookieAwareHttpServletResponse (HttpServletResponse aResponse) {
        super (aResponse);
    }

    @Override
    public void addCookie (Cookie aCookie) {
        cookies.add (aCookie);
        super.addCookie(aCookie);
    }

    public List<Cookie> getCookies () {
        return Collections.unmodifiableList (cookies);
    }

} 

And the way I use it:

// wrap the response object
CookieAwareHttpServletResponse response = new CookieAwareHttpServletResponse(aResponse);

// make the call to the 3rd party library 
String order = orderService.getOrder (aRequest, response, String.class);

// get the list of cookies set
List<Cookie> cookies = response.getCookies();
温馨耳语 2024-09-21 22:57:55

您唯一的方法是包装 HttpServletResponse 对象,以便 addCookie 方法可以在设置 cookie 时拦截并记录日志。您可以通过添加 ServletFilter 来完成此操作,该过滤器在将现有 HttpServletResponse 传递到 Servlet 之前对其进行包装。

Your only approach is to wrap the HttpServletResponse object so that the addCookie methods can intercept and log when cookies are set. You can do this by adding a ServletFilter which wraps the existing HttpServletResponse before it is passed into your Servlet.

汐鸠 2024-09-21 22:57:55

Cookie 通过“Set-Cookie”响应标头发送到客户端。试试这个:

private static void logResponseHeaders(HttpServletResponse httpServletResponse) {

    Collection<String> headerNames = httpServletResponse.getHeaderNames();

    for (String headerName : headerNames) {
        if (headerName.equals("Set-Cookie")) {
            log.info("Response header name={}, header value={}", headerName, httpServletResponse.getHeader(headerName));
        }
    }
}

Cookies are sent to the client in the "Set-Cookie" response header. Try this:

private static void logResponseHeaders(HttpServletResponse httpServletResponse) {

    Collection<String> headerNames = httpServletResponse.getHeaderNames();

    for (String headerName : headerNames) {
        if (headerName.equals("Set-Cookie")) {
            log.info("Response header name={}, header value={}", headerName, httpServletResponse.getHeader(headerName));
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文