【问题标题】:How to interact with HTTP Session in a Spring JAX-WS web service如何在 Spring JAX-WS Web 服务中与 HTTP Session 交互
【发布时间】:2013-01-03 14:08:46
【问题描述】:

我正在将旧版 Web 服务从在 JBoss 中运行移植到使用 Spring 在 Jetty 中运行。

这是我的网络服务类:

@WebService( serviceName = "Authentication" )
public class AuthenticationWebService
{
    @Resource
    private WebServiceContext context;

    public boolean authenticate( @WebParam( name = "clientRef" ) final String clientRef, @WebParam( name = "username" ) final String username, @WebParam( name = "password" ) final String password )
    {
         ... DO THE ACTUAL LOGIN - DB CALLS ETC

         // finally, store the username in the httpsession
         final HttpServletRequest request = (HttpServletRequest) context.getMessageContext().get( MessageContext.SERVLET_REQUEST );
         final HttpSession session = request.getSession();
         session.setAttribute( "FIELD_USERNAME", username );
    }
}

为了公开服务,我的 Spring xml 如下所示:

    <bean id="wsExporter" class="org.springframework.remoting.jaxws.SimpleHttpServerJaxWsServiceExporter">
        <property name="basePath" value="/webServices/"/>
        <property name="port" value="9494"/>
    </bean>

    <bean class="com.example.AuthenticationWebService" />

Web 服务在我的 Jetty 服务器中导出,我可以调用它(通过使用 JaxWsPortProxyFactoryBean 的 Junit 类),但是我在 request.getSession() 中得到一个空指针异常。

看起来“javax.xml.ws.servlet.request”不是 context.getMessageContext() 映射上的条目。

用谷歌搜索了一下,所有的建议都指向这是获取 servlet 请求的正确方法。是不是我用 Spring 导出服务的方式有问题?

注意 - 我不能也不想改变这个遗留服务在设置/读取 http 会话值时的行为,但我会对网络服务可以做到这一点的其他方式感兴趣。

【问题讨论】:

    标签: java web-services spring jax-ws


    【解决方案1】:

    默认情况下,jaxws 不维护会话。在获取会话之前添加此行。

    context.getMessageContext().put(javax.xml.ws.BindingProvider.SESSION_MAINTAIN_PROPERTY, true)
    

    【讨论】:

    • 为什么需要这个?提供了 WebServiceContext,但 context.getMessageContext().get(MessageContext.SERVLET_REQUEST) 返回 null。
    • 我已经尝试过了,但没有任何区别: context.getMessageContext().get("javax.xml.ws.servlet.request") 仍然返回 null 所以我无法从中获取 http 会话它。
    猜你喜欢
    • 1970-01-01
    • 2013-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多