CXF获取客户端IP(解决WebServiceContext为null问题)

原文链接:https://blog.csdn.net/wwwyuanliang10000/article/details/21290027

CXF获取客户端IP(解决WebServiceContext为null问题)

原文地址: https://blog.csdn.net/wwwyuanliang10000/article/details/21290027

 

 

网络上很多文章都是这样配的:
 
@Resource
private WebServiceContext wscontext;
 
public String getIP(){
        MessageContext ctx = wscontext.getMessageContext();
        HttpServletRequest request = (HttpServletRequest)ctx.ge(AbstractHTTPDestination.HTTP_REQUEST);
        return request.getRemoteAddr();
}
 
但是在我测试的过程中,发现如果把这段代码写在aop切点中,wscontext就是null,如果写在普通的实现类,就可以正常获取。其实很多帖子也说到null的问题,但最后都没解决。
现在在这里发现另一种方法,经测试完全有效:
 
Message message = PhaseInterceptorChain.getCurrentMessage();
HttpServletRequest httprequest = (HttpServletRequest)message.get(AbstractHTTPDestination.HTTP_REQUEST);
return httprequest.getRemoteAddr();
————————————————
版权声明:本文为CSDN博主「AndyYuan10000」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/wwwyuanliang10000/article/details/21290027

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章