如何拦截自定义 HTTP 标头值并将其存储在 Wicket 的 WebSession 中?

发布于 2024-09-27 10:28:30 字数 595 浏览 4 评论 0原文

我需要从每个请求中获取特定的自定义 HTTP 标头值并将其放入 WebSession 中,以便以后可以在任何网页上使用它。 (我相信 Wicket 的方法是使用一个扩展 WebSession 的自定义类,该类具有适当的访问器。)

我的问题是,我需要哪种过滤器(或其他机制)能够拦截标头和访问 WebSession 来存储值?

我尝试使用普通的 Java EE Filter 来执行此操作,使用

CustomSession session = (CustomSession) AuthenticatedWebSession.get();

But(也许并不奇怪),结果是:

java.lang.IllegalStateException: 
    you can only locate or create sessions in the context of a request cycle

我是否应该扩展 WicketFilter 并在那里执行它(我可以在那时访问会话吗?),或者是更复杂的事情必需的?

当然,如果我做的完全错误,请指出;我是威克特的新手。

I need to grab a certain custom HTTP header value from every request and put it in WebSession so that it will be available on any WebPage later on. (I believe the Wicket way to do this is to have a custom class extending WebSession that has appropriate accessors.)

My question is, what kind of Filter (or other mechanism) I need to be able to both intercept the header and access the WebSession for storing the value?

I tried to do this with a normal Java EE Filter, using

CustomSession session = (CustomSession) AuthenticatedWebSession.get();

But (perhaps not surprisingly), that yields:

java.lang.IllegalStateException: 
    you can only locate or create sessions in the context of a request cycle

Should I perhaps extend WicketFilter and do it there (can I access the session at that point?), or is something even more complicated required?

Of course, please point it out if I'm doing something completely wrong; I'm new to Wicket.

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

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

发布评论

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

评论(1

无风消散 2024-10-04 10:28:30

我猜你需要实现一个自定义的 WebRequestCycle:

public class CustomRequestCycle extends WebRequestCycle{

    public CustomRequestCycle(WebApplication application,
        WebRequest request,
        Response response){
        super(application, request, response);
        String headerValue = request.getHttpServletRequest().getHeader("foo");
        ((MyCustomSession)Session.get()).setFoo(headerValue);
    }

}

在你的 WebApplication 类中,你可以像这样注册自定义的 RequestCycle:

public class MyApp extends WebApplication{

    @Override
    public RequestCycle newRequestCycle(Request request, Response response){
        return new CustomRequestCycle(this, (WebRequest) request, response);
    }

}

参考:

I'd guess you need to implement a custom WebRequestCycle:

public class CustomRequestCycle extends WebRequestCycle{

    public CustomRequestCycle(WebApplication application,
        WebRequest request,
        Response response){
        super(application, request, response);
        String headerValue = request.getHttpServletRequest().getHeader("foo");
        ((MyCustomSession)Session.get()).setFoo(headerValue);
    }

}

And in your WebApplication class you register the custom RequestCycle like this:

public class MyApp extends WebApplication{

    @Override
    public RequestCycle newRequestCycle(Request request, Response response){
        return new CustomRequestCycle(this, (WebRequest) request, response);
    }

}

Reference:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文