Я решил это с помощью аннотации ininterceptor. Я добавил аннотацию org.apache.cxf.interceptor.InInterceptors и предоставил настраиваемый класс для установки значений в обязательные поля WSS4JInInterceptor и добавил WSS4JInInterceptor в цепочку перехватчиков.
@InInterceptors(interceptors = {"com.xxx.xx.ws.wsse.WSSecurityInterceptor"})
@WebService
@Stateless
public class OrganizationImportServiceImpl{...enter code here
Вот класс com.xxx.xx.ws.wsse.WSSecurityInterceptor
package com.xxx.xx.ws.wsse;
import java.util.HashMap;
import java.util.Map;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.interceptor.Interceptor;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor;
public class WSSecurityInterceptor extends AbstractPhaseInterceptor<Message>{
public WSSecurityInterceptor() {
super(Phase.PRE_PROTOCOL);
}
public WSSecurityInterceptor(String phase) {
super(Phase.PRE_PROTOCOL);
}
@Override
public void handleMessage(Message message) throws Fault {
Map<String, Object> props = new HashMap<String, Object>();
props.put("action", "UsernameToken");
props.put("passwordCallbackClass", "com.xxx.xx.ws.wsse.ServerPasswordCallback");
props.put("passwordType", "PasswordText");
WSS4JInInterceptor wss4jInHandler = new WSS4JInInterceptor(props);
message.getInterceptorChain().add((Interceptor<? extends Message>) wss4jInHandler);
}
}
Затем установите действительный пароль в классе обработчика обратного вызова. вот класс обработчика обратного вызова.
package com.xxx.xx.ws.wsse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;
import org.apache.wss4j.common.ext.WSPasswordCallback;
public class ServerPasswordCallback implements CallbackHandler {
private Map<String, String> passwords = new HashMap<String, String>();
public ServerPasswordCallback() {
super();
passwords.put("testuser", "testpwd");
}
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
if (pc.getIdentifier() == null) {
throw new IOException("authentication failure. required username password to proceed ..");
}
if (passwords.containsKey(pc.getIdentifier())) {
// set the password on the callback. This will be compared to the
// password which was sent from the client.
pc.setPassword(passwords.get(pc.getIdentifier()));
} else {
throw new IOException("authentication failure. invalid user name or password ");
}
}
}
Затем проверка пароля выполняется в модуле cxf-rt-ws-security.