使用SOAP标头进行身份验证

时间:2018-09-02 09:55:53

标签: soap soap-client spring-ws wss4j webservicetemplate

wsdl使用ObjectFactory和工作接口生成了一些类。通过使用用户名令牌将标头附加到正文来形成请求。我这样尝试: 配置:

@PostConstruct
public void initialization() {
    JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
    factory.setServiceClass(EMyInterface.class);
    factory.setAddress(moneyTransferServiceWsdlUrl);

    factory.getOutInterceptors().add(new LoggingOutInterceptor());
    factory.getInInterceptors().add(new LoggingInInterceptor());
    factory.getInFaultInterceptors().add(new LoggingInInterceptor());

    service = (EMyInterface) factory.create();
    Client client = ClientProxy.getClient(service);

    HTTPConduit httpConduit = (HTTPConduit) client.getConduit();
    HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
    httpClientPolicy.setConnectionTimeout(connectionTimeout);
    httpClientPolicy.setReceiveTimeout(receiveTimeout);
    httpConduit.setClient(httpClientPolicy);
}

@Bean
public EMyInterface subscribeInfo() {
    return service;
}

@Bean
public WebServiceTemplate getTemplate() {
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    try {
        marshaller.setContextPath(String.valueOf(JAXBContext.newInstance(ObjectFactory.class)));
    } catch (JAXBException e) {
        System.out.println(e.getErrorCode());
    }
    WebServiceTemplate template = new WebServiceTemplate();
    template.setDefaultUri(myServiceWsdlUrl);
    template.setMarshaller(marshaller);
    template.setUnmarshaller(marshaller);

    ClientInterceptor[] interceptors = new ClientInterceptor[]{
            securityInterceptor()
    };
    template.setInterceptors(interceptors);
    return template;
}

@Bean
public Wss4jSecurityInterceptor securityInterceptor() {
    Wss4jSecurityInterceptor wss4jSecurityInterceptor = new Wss4jSecurityInterceptor();
    wss4jSecurityInterceptor.setSecurementActions("UsernameToken");
    wss4jSecurityInterceptor.setSecurementUsername(myAuthName);
    wss4jSecurityInterceptor.setSecurementPassword(myPassword);
    wss4jSecurityInterceptor.setSecurementPasswordType(WSConstants.PW_TEXT);
    return wss4jSecurityInterceptor;
}

所以我从界面调用了我需要的方法:

@Service
public class EMyIntegrationImpl implements EMyIntegration {
    private final EMyInterface service;
    private final WebServiceTemplate webServiceTemplate;

    public EMyIntegrationImpl(EMyInterface service, WebServiceTemplate webServiceTemplate) {
        this.service = service;
        this.webServiceTemplate = webServiceTemplate;
    }

    @Override
    public MakeExtendedResult getMakeExtended(String param1, String param2, String param3) {
        MakeExtendedResult result;
        result = (MakeExtendedResult) webServiceTemplate.marshalSendAndReceive(
               service.makeExtended(param1, param2, param3)
        );
   }
}

返回错误:org.apache.cxf.interceptor.Fault:无法接收消息。在这种情况下,主体形成正常形式,并且标题没有userNameToken,其中仅包含soap-service的地址

0 个答案:

没有答案