由于歷史原因,幾個(gè)項(xiàng)目都選用hessian作為web service的實(shí)現(xiàn)方式,hessian的確是非常輕量級(jí),基于http協(xié)議進(jìn)行傳輸,通過(guò)自定義的串行化機(jī)制將請(qǐng)求信息進(jìn)行序列化,以二進(jìn)制傳輸節(jié)省了不少的開銷,速度跟socket差不多.客戶端和服務(wù)器發(fā)起和接收請(qǐng)求都是通過(guò)spring提供的hessian api進(jìn)行請(qǐng)求和接收,但是在服務(wù)端中并沒(méi)有記錄和控制遠(yuǎn)端ip地址和主機(jī)的信息,所以需要對(duì)源碼進(jìn)行一些重寫
對(duì)org.springframework.remoting.caucho.HessianServiceExporter進(jìn)行重寫
/**
* 重寫HessianServiceExporter.handleRequest(),攔截獲取遠(yuǎn)端調(diào)用信息
* @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(),攔截獲取遠(yuǎn)端調(diào)用信息,提取出調(diào)用端的ip信息與服務(wù)端制定的ip列表進(jìn)行對(duì)比,并將所有調(diào)用信息記錄日志
如果直接使用hessian的api的HessianServlet,直接對(duì)HessianServlet的service()重寫,攔截并提取遠(yuǎn)端調(diào)用信息
/**
* 重寫HouseHessianServlet.service(),攔截獲取遠(yuǎn)端調(diào)用信息
* @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");
}
}