【问题标题】:Get Client IP in Stateless EJB在无状态 EJB 中获取客户端 IP
【发布时间】:2016-02-07 10:48:33
【问题描述】:

我有无状态 EJB WebService。

WS接口:

@Remote  
@WebService
public interface WSInterface{  
    @WebMethod  
    public String[] WSMethod(@WebParam(name="arg0") String arg0)
}

WS 实现:

@WebService
@Stateless
public class WSImpl extends GenericSessionBean implements WSInterface {
    @WebMethod
    public String[] WSMethod( String arg0)
    {
        return ...;
    }
}

我需要在 WSMethod 中获取客户端 IP。 我试图通过这种方式得到它(它在“通用”网络服务中工作):

@Resource  
private SessionContext ctx;  
public String[] getProperties() {  
    List propList = new ArrayList();    
    MessageContext mc = ctx.getMessageContext();  
    Iterator props = mc.getPropertyNames();  
    for (String prop = (String)props.next(); props.hasNext(); prop = (String)props.next())  
        { propList.add(prop); }  
    return propList.toArray(new String[propList.size()]);
}

但没有成功:MessageContext 中没有名称为 REMOTE_ADDR 的属性。

有没有办法在@Stateless EJB 中获取REMOTE_ADDR

【问题讨论】:

    标签: java web-services ejb stateless


    【解决方案1】:

    您可以尝试使用@Resource 获取 WebServiceContext 而不是 SessionContext 吗?我现在没有合适的环境来检查它

    【讨论】:

    • @Resource private WebServiceContext ctx; ... MessageContext mc = ctx.getMessageContext(); HttpServletRequest 请求 = (javax.servlet.http.HttpServletRequest)mc.get(MessageContext.SERVLET_REQUEST)); String clientIP = request.getRemoteAddr();
    • 这很奇怪,但现在您的解决方案有效!我记得早些时候我已经尝试过 WebServiceContext,但是编译器给了我一个错误。现在它可以根据需要工作。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-26
    • 1970-01-01
    • 2016-10-07
    • 2015-06-06
    • 2020-11-10
    • 1970-01-01
    相关资源
    最近更新 更多