本文介绍了如何拦截自定义HTTP标头值并将其存储在Wicket的WebSession中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.)

我的问题是,什么样的Filter(或者其他机制)我需要能够截取标题并访问WebSession 来存储值吗?

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?

我试着用正常的方法做到这一点Java EE Filter,使用

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

我是否应该扩展WicketFilter并在那里进行(我可以在那时访问会话吗?),还是需要更复杂的东西?

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

当然,如果我做错了,请指出来;我是Wicket的新手。

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

推荐答案

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

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);
    }

}

在您的WebApplication类中注册像这样的自定义RequestCycle:

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);
    }

}

参考:



这篇关于如何拦截自定义HTTP标头值并将其存储在Wicket的WebSession中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 21:19