spring+xfire +wss4j实现
web.xml 配置
?
<context-param>
??<param-name>contextConfigLocation</param-name>
??<param-value>
???? ? /WEB-INF/applicationContext.xml,
?? /WEB-INF/xfire-servlet.xml,
?????? ? classpath:org/codehaus/xfire/spring/xfire.xml
??????? </param-value>
?</context-param>
?
<servlet>
??<servlet-name>xfire</servlet-name>
??<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
??<load-on-startup>11</load-on-startup>
?</servlet>
?<servlet-mapping>
??<servlet-name>xfire</servlet-name>
??<url-pattern>/services/*</url-pattern>
?</servlet-mapping>
?
xfire-servlet.xml 配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
?<bean />
????</entry>
???</map>
??</property>
?</bean>
?<bean id="domInHandler" />
?<bean id="wss4jInHandlerUsernameToken" lazy-init="false" abstract="true">??
??????? <property name="serviceFactory">??
??????????? <ref bean="xfire.serviceFactory"/>??
??????? </property>??
??????? <property name="xfire">??
??????????? <ref bean="xfire"/>??
??????? </property>??
??? </bean>??
???
??? <bean id="userServiceWSS4JUsernameToken" parent="baseWebService">??
??????? <property name="serviceBean" ref="userWebService"/>??
??????? <property name="serviceClass" value="com.wine.service.IUserWebService"/>??
??????? <property name="name" value="userServiceWSS4JUsernameToken"></property>
??????? <property name="inHandlers">??
??????????? <list>??
??????????????? <ref bean="domInHandler"/>??
??????????????? <ref bean="wss4jInHandlerUsernameToken"/>??
??????????? </list>??
??????? </property>??
??? </bean>??
</beans>
?
?
验证
public class PassWordHandler implements CallbackHandler {
?private static Map<String,String> users = new HashMap<String,String>();
?
?static {
??users.put(WSPropertiesReader.getUserName(), WSPropertiesReader.getPassword());
?}
?public void handle(Callback[] callbacks) throws IOException,
???UnsupportedCallbackException {
??WSPasswordCallback callback = (WSPasswordCallback) callbacks[0];
??String id = callback.getIdentifer();
??if(WSConstants.PASSWORD_TEXT.equals(callback.getPasswordType())){
???String validPW = users.get(id);
???if(validPW == null || !validPW.equalsIgnoreCase(callback.getPassword())){
????throw new WSSecurityException("password not match ......");
???}
??} else {
???callback.setPassword(users.get(id));
??}
?}
}
?
客户端实现
public class Test {
?private static XFireProxyFactory xFireProxyFactory=new XFireProxyFactory();
?public static void main(String[] args) throws Exception {
??Service service=new ObjectServiceFactory().create(IUserWebService.class);
??String url="http://localhost:8080/dc/services/userServiceWSS4JUsernameToken";
??IUserWebService userWebService=(IUserWebService) xFireProxyFactory.create(service,url);
??Client client=((XFireProxy) Proxy.getInvocationHandler(userWebService)).getClient();
??client.addOutHandler(new DOMOutHandler());
??Properties properties=new Properties();
??properties.setProperty(WSHandlerConstants.ACTION,WSHandlerConstants.USERNAME_TOKEN);
??properties.setProperty(WSHandlerConstants.PASSWORD_TYPE,WSConstants.PW_DIGEST);
??properties.setProperty(WSHandlerConstants.USER,"feng");
??properties.setProperty(WSHandlerConstants.PW_CALLBACK_CLASS,PassWordHandler.class.getName());
??client.addOutHandler(new WSS4JOutHandler(properties));
??System.out.println("xxxxxxxxxxxx"+userWebService.isValidSession("a"));
?}
}
?