由于历史原因,几个项目都选用hessian作为web service的实现方式,hessian的确是非常轻量级,基于http协议进行传输,通过自定义的串行化机制将请求信息进行序列化,以二进制传输节省了不少的开销,速度跟socket差不多.客户端和服务器发起和接收请求都是通过spring提供的hessian api进行请求和接收,但是在服务端中并没有记录和控制远端ip地址和主机的信息,所以需要对源码进行一些重写
对org.springframework.remoting.caucho.HessianServiceExporter进行重写
/**
* 重写HessianServiceExporter.handleRequest(),拦截获取远端调用信息
* @author chenyz
*
*/
public class HouseHessianServiceExporter extends HessianServiceExporter {
private static String[] entryIP = {"192.168.0.1","192.168.0.3","192.168.0.3"};
private static Log log = LogFactory.getLog("Myremote");
@Override
public void handleRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
log.info("try ==>remote 's IP:"+IpUtil.getIpAddr(request)+"remote 's host: "+request.getRemoteHost());
int call = 0;
for(String ip:entryIP){
if(ip.equals(IpUtil.getIpAddr(request)))
call++;
}
if(call>0){
log.info("call ==>remote 's IP:"+IpUtil.getIpAddr(request)+"remote 's host: "+request.getRemoteHost());
super.handleRequest(request, response);
}
}
}
<bean id="shineLibWSImpl" class="com.***.shine.remote.ShineLibWSImpl"/>
<bean name="/remote/shineinfo" class="com.***.shine.hessian.service.HouseHessianServiceExporter">
<property name="service">
<ref bean="shineLibWSImpl"/>
</property>
<property name="serviceInterface">
<value>com.***.shine.remote.ShineLibWebService</value>
</property>
</bean>
重写HessianServiceExporter.handleRequest(),拦截获取远端调用信息,提取出调用端的ip信息与服务端制定的ip列表进行对比,并将所有调用信息记录日志
如果直接使用hessian的api的HessianServlet,直接对HessianServlet的service()重写,拦截并提取远端调用信息
/**
* 重写HouseHessianServlet.service(),拦截获取远端调用信息
* @author chenyz
*
*/
public class HouseHessianServlet extends HessianServlet{
private static String[] entryIP = {"192.168.0.1","192.168.0.3","192.168.0.3"};
private static Log log = LogFactory.getLog("Myremote");
@Override
public void service(ServletRequest request, ServletResponse response)
throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
log.info("try ==>remote 's IP:"+IpUtil.getIpAddr(req)+"remote 's host: "+request.getRemoteHost());
int call = 0;
for(String ip:entryIP){
if(ip.equals(IpUtil.getIpAddr(request)))
call++;
}
if(call>0){
log.info("call ==>remote 's IP:"+IpUtil.getIpAddr(req)+"remote 's host: "+request.getRemoteHost());
super.service(request, response);
}
}
}
web.xml
<servlet>
<servlet-name>HessianServlet</servlet-name>
<servlet-class>
com.***.product.remote.Htest
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HessianServlet</servlet-name>
<url-pattern>/service/hession</url-pattern>
</servlet-mapping>
com.***.product.remote.Htest
public class Htest extends HouseHessianServlet implements IHtest{
public hello(){
System.out.println("hello");
}
}