HttpServletResponse response = XFireServletController.getResponse();
此條路不通只好去查XFire的文檔關于authentication部分,大概有以下幾種方案:
-
HTTP Authentication
-
SOAP Header authentication with JSR181
-
SOAP authentication with handlers
-
WS-Security
// Create your client
Client client = ....;
// Or get it from your proxy
Client client = ((XFireProxy) Proxy.getInvocationHandler(myClientProxy)).getClient();
client.setProperty(Channel.USERNAME, "username");
client.setProperty(Channel.PASSWORD, "pass");
去忘了寫服務端應該怎么做,這個username和password該在那里驗證呢??我翻遍了文檔也沒找著
來看第二條SOAP header authentication with JSR181,看起來倒是很簡單
但是在Service的代碼中每個方法里都要寫一個UserToken驗證的參數,雖然似乎權限粒度能控制得很細,但是這嚴重污染了業務邏輯的代碼,非常的不優雅,放棄!
public void someOperation(String data, @WebParam(header=true) UserToken token) {
authenticate(token)
// do your normal request here
}
再看WS-Security,這是webservice的安全標準,但實在太復雜了,并且需要配置Service.xml,我們項目是Java1.5,Service.xml根本就沒有寫,是自動生成的,我是實在找不到Service.xml該在那配置?只好作罷
文檔里遺漏了很重要的一點,就是關于如何在xfire-servlet里配置,導致很多人看了文檔也進行不下去,我查了很久才在老外的一篇blog里找到一點tips,下面是配置:
<!-- WebService base, do not modify it -->
<bean id="webService" class="org.codehaus.xfire.spring.remoting.XFireExporter" abstract="true">
<property name="serviceFactory">
<ref bean="xfire.serviceFactory" />
</property>
<property name="xfire">
<ref bean="xfire" />
</property>
</bean>
<bean id="yourWebService" parent="webService">
<property name="serviceBean">
<ref bean="yourService" />
</property>
<property name="serviceClass">
<value>your.package.YourServiceInterface</value>
</property>
<property name="inHandlers">
<ref bean="authenticationHandler"/>
</property>
</bean>
AuthenticationHandler需要修改一下,其他不用變:
AuthenticationHandler.java
import org.apache.log4j.Logger;
import org.codehaus.xfire.MessageContext;
import org.codehaus.xfire.handler.AbstractHandler;
import org.codehaus.xfire.fault.*;
import org.jdom.*;
public class AuthenticationHandler extends AbstractHandler {
private static final Logger log = Logger.getLogger(AuthenticationHandler.class);
public void invoke(MessageContext context) throws Exception {
log.info("authentication handler is invoked");
if (context.getInMessage().getHeader() == null)
{
throw new XFireFault("Request must include company authentication token.",
XFireFault.SENDER);
}
Element header = context.getInMessage().getHeader();
Element token = header.getChild("AuthenticationToken");
if (token == null)
{
throw new XFireFault("Request must include authentication token.",
XFireFault.SENDER);
}
String username = token.getChild("Username").getText();
String password = token.getChild("Password").getText();
try {
// 現在你已經得到了客戶端傳來的username和password,那就驗證它吧(可以交給acegi來驗證)
}
}catch(Exception e) {
log.warn(e);
throw new XFireFault("Authentication Failed.",
XFireFault.SENDER);
}
}
}
客戶端代碼:
YourService service = (YourService) new XFireProxyFactory().create(serviceModel,
"http://localhost:8080/YourProject/service/YourService");
Client client = proxy.getClient();
client.addOutHandler(new ClientAuthHandler("jeffrey", "killjava"));
// 執行下面代碼會進行驗證
service.someOperation();