How to intercept custom HTTP header value and store it in Wicket's WebSession?

泄露秘密 提交于 2019-12-30 07:53:10

问题


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.


回答1:


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:

  • Request cycle and request cycle processor


来源:https://stackoverflow.com/questions/3914528/how-to-intercept-custom-http-header-value-and-store-it-in-wickets-websession

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!