在Spring继承CXF中要用到request来获取IP
这里使用到切点,
<jaxws:endpoint id="myService" implementor="#myServiceImpl" address="/MyService">
<jaxws:inInterceptors>
<ref bean="clientIpInInterceptor"/>
</jaxws:inInterceptors>
</jaxws:endpoint>
<bean id="myServiceImpl" class="com.service.MyService" />
public class ClientIpInInterceptor extends AbstractPhaseInterceptor<Message> {
public ClientIpInInterceptor() {
super(Phase.RECEIVE);
}
@Override
public void handleMessage(Message message) throws Fault {
String result = null;
try {
HttpServletRequest request = (HttpServletRequest) message.get(
AbstractHTTPDestination.HTTP_REQUEST);//这句可以获取到request
String ip = StringUtils.getIpFromRequest(request);//自己写校验IP规则
message.setContent(String.class, ip);
} catch (Exception e) {
e.printStackTrace();
}
}
}
如果想要在接口中获取到request,可以这样做
@WebService(endpointInterface = "com.service.IService")
public class MyService implements IService {
@Resource
private WebServiceContext context;
// MessageContext ctx = context.getMessageContext();
// HttpServletRequest request = (HttpServletRequest) ctx
// .get(AbstractHTTPDestination.HTTP_REQUEST);
// HttpServletResponse response = (HttpServletResponse) ctx
// .get(AbstractHTTPDestination.HTTP_RESPONSE);
在你的接口方法中写就能获取了,主要是
@Resource
private WebServiceContext context;
这个是官方文档提供的
还有一个重要的就是切点中,切点有进入方法时截获在上面讲了,但是接口调用完毕后返回出去就不知道怎么获取request,网上没有说明
<bean id="OutInterceptor" class="xxxx.OutInterceptor" />
<jaxws:outFaultInterceptors>
<ref bean="OutInterceptor"/>
</jaxws:outFaultInterceptors>
这里就不知道怎么获取了.
public class OutInterceptor extends AbstractSoapInterceptor {
private JiBXConverter wmsConverter;
public OutInterceptor() {
super(Phase.PRE_STREAM);//Phase这个类就知道怎么用.
}
@Override
public void handleMessage(SoapMessage message) throws Fault {xxx}
SoapMessage 这个类就不知道怎么获取了.安装上面的方式试了一下,为null