【问题标题】:How to intercept custom HTTP header value and store it in Wicket's WebSession?如何拦截自定义 HTTP 标头值并将其存储在 Wicket 的 WebSession 中?
【发布时间】:2010-10-12 12:15:36
【问题描述】:

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

我的问题是,我需要什么样的过滤器(或其他机制)才能拦截标头并访问 WebSession 来存储值?

我尝试使用普通的 Java EE 过滤器来做到这一点,使用

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

但是(也许并不奇怪),这会产生:

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

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

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

【问题讨论】:

    标签: java jakarta-ee wicket servlet-filters


    【解决方案1】:

    我猜你需要实现一个自定义的 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);
        }
    
    }
    

    参考:

    【讨论】:

    • 谢谢,我用这种方法搞定了! (我花了一些时间来验证 Tamper Data...)使用自定义 WebRequestCycle 实际上非常简单和干净,没有我想象的那么复杂。
    • wicket 对于大多数问题都有一个简单的解决方案,这就是它的好处
    猜你喜欢
    • 2018-11-21
    • 2019-12-03
    • 1970-01-01
    • 2020-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-31
    • 1970-01-01
    相关资源
    最近更新 更多