一般应用中有ajax进行跨子域代理调用时,通常需要设置HTTP代理,解决域的权限问题。
BSD爱好者乐园e,z+jmC_举例,blog.chinajavaworld.com中用ajax调search子系统的服务,Resin的HttpProxyServlet设置如下:
BSD爱好者乐园6uL7CHNl!o4s([
http-proxy
com.caucho.servlets.HttpProxyServlet
'search.chinajavaworld.com:80'/>
"/ajax/search/*"servlet-name="http-proxy"/>
BSD爱好者乐园&X$Be8fF3sz.B m9KBSD爱好者乐园v&MOUt.C在search.chinajavaworld.com/ajax/search/test.jsp中
,Mm w5Z,M;vs6u;O1f<%out.print(request.getHeader("Host"));%>
c YI&MNZy现在访问:
BSD爱好者乐园.af }Vd0g;Dhttp://blog.chinajavaworld.com/ajax/search/test.jsp,输出:
?,s qIDW!pc:isearch.chinajavaworld.com
$P6vz#{]T2c.Ig4b"s#j$QL*CO(j当应用要支持访问时,问题来了:BSD爱好者乐园4f0Nlb w0v7Z.i$s'uBSD爱好者乐园t@6iK q2r | Fj6N"I5B4hRHh*O1o如用 http://blog.chinajavaworld-bbb.com/ajax/search/test.jsp 进行访问,输出也是:
BSD爱好者乐园y;O)K0aj)ZWBSD爱好者乐园Z wVh)sX`hsearch.chinajavaworld.com
r&l;o&ak如果设置两个host
(]?Y-pb,K
http-proxy
com.caucho.servlets.HttpProxyServlet
'search.chinajavaworld.com:80'/>
'search.chinajavaworld-bbb.com:80'/>
aE-@|\#U则用两个域名访问的话,都将输出:
BSD爱好者乐园O.yo8K GJqsearch.chinajavaworld-bbb.com
8U R7y#M+t@ z1X8JtLFr k qU现在的需求是,用哪个主域名访问的,就必须返回与该主域名一致的子域BSD爱好者乐园"k7A;N K)Z;q(@BSD爱好者乐园x)ch,l}m [I0^3Nm解决方法:修改 HttpProxyServlet 源码:
h;E];Q ik"y-DTcpPool.java加入以下方法:
Z-][-O1Kz)]opublicServer nextServer(String mainHost)
{BSD爱好者乐园WfE&p+b Ox)pServer bestServer =
null;
BSD爱好者乐园0P+u+K^ y(m,T'b:l?synchronized(
this)
{BSD爱好者乐园 R.cz'k_)u@String _URL =
null;
IGG/|*y\e |for(
inti = 0; i < _servers.length; i++)
{BSD爱好者乐园:S` d FGdqPNServer server = _servers[i];
BSD爱好者乐园/II)I)d,K/H5B4Nif(server.getURL().indexOf(mainHost) != -1)
{)Lk D;h$fyK]//System.out.println("ok,found host.return server.");BSD爱好者乐园Gj9nO.edm zbestServer = server;
BSD爱好者乐园*CF ~ `/yBbreak;
pstL|1Q(~}6_+h@@Q9`#Y$V0kif(i == _servers.length - 1) _URL = server.getURL();
}BSD爱好者乐园i*u8l A%eUqjmif(bestServer ==
null)
{B4v&CH6\N'Xr Sw@//System.out.println(mainHost+" is not in server list. add now.");.M2Lju)\Ts-n5TString oldMainHost = getMainDomain(_URL);
\d|,lU R_URL = _URL.replace(oldMainHost, mainHost);
S.}(i8bf&e,ch YO'I'RString _host = getDomain(_URL);
BSD爱好者乐园+x#rQ| JS9[ qbestServer =
Server(_host);
_ Wa*l5c yA_hosts.add(_host);
2GL;h/{ \ yt0SServer[] servers =
newServer[_servers.length + 1];
a`+~A#cd r3vSystem.arraycopy(_servers, 0, servers, 0, _servers.length);
BSD爱好者乐园z[p#Jw d.B1Kk_1vservers[_servers.length] = bestServer;
9[A:NB(L^ u_servers = servers;
2n1S8B*P1H5F}BSD爱好者乐园%Za0FCq9Yo;}returnbestServer;
BSD爱好者乐园u Ejg(`w:j;W3R6N[}BSD爱好者乐园ig6T YD5J+Y FZ}BSD爱好者乐园@5lpZ da eE5Z1_D3e7[[BSD爱好者乐园4t}"s:YE:O3k u'G"iHttpProxyServlet中加入:
BSD爱好者乐园RCv1_Q9P9kur*w*BT+KString host = req.getHeader("Host");
;bc$w4f:dnN'["L#rString mainHost = TcpPool.getMainDomain(host);
3U!}De+hje#o[4z ?-~3v,X"W&fBSD爱好者乐园fH3c/~&fmg7z然后将
t4jfC%EBSD爱好者乐园r5U6e~2fTcpPool.Server server = _tcpPool.nextServer();
aPQLqi:G:L8S rA ^替换为
BSD爱好者乐园kc._!Z? X1~0FI+\2EbT W.c/Q{TcpPool.Server server = _tcpPool.nextServer(mainHost);
BSD爱好者乐园%j [f\8l&}*T N8f/\
阅读(1338) | 评论(0) | 转发(0) |