使用Spring Boot使用客户端(HTML)提供的凭据制作基于SOAP的Web服务

时间:2018-12-05 08:34:04

标签: java spring-boot soap

我正在使用Spring Boot创建Web应用程序。该应用程序将从客户端(Angular)获取凭据,然后使用这些凭据通过以下代码进行请求。但是,它不起作用,因为似乎所有与配置相关的工作都在应用程序启动后立即执行。那时,soap服务凭据为null,这就是为什么spring在HttpComponentsMessageSender类上将凭据设置为null的原因。当用户通过凭据登录时,它不使用这些凭据,而是使用旧的凭据。

我正在使用以下代码拨打肥皂服务电话:

这是我的rest控制器的代码,它调用soap客户端(在我的情况下为CustomerRequestClient):

public class CustomerRequestController {

private final CustomerRequestClient CRClient;

CustomerRequestController(CustomerRequestClient CRClient) {
    this.CRClient = CRClient;
}


@RequestMapping(method = RequestMethod.POST, value="/getAllCustomerRequests")
@ResponseBody
List<Object> registerStudent(@RequestBody Login user) {
    return CRClient.getAllCustomerRequests(user.getUserName(), user.getPassword());
}

正在拨打电话的客户

public class CustomerRequestClient extends WebServiceGatewaySupport {

private static final Logger log = LoggerFactory.getLogger(CustomerRequestClient.class);

public List<Object> getAllCustomerRequests(String userName, String password) {
    //Instantiate Request object
    FindAePReqEByDocument aePReq=  createAllRequest();

    //Soap Service call
    WebServiceTemplate template = getWebServiceTemplate();
    JAXBElement<AePReqEListType> response = (JAXBElement<AePReqEListType>) template.
            marshalSendAndReceive("http://localhost:8086/fmax/ws", aePReq);

    //Get AePReqEListType object from jaxb element 
    AePReqEListType r = (AePReqEListType)response.getValue();

    //Business logic come afterwards,,,,

}

private FindAePReqEByDocument createAllRequest() {
    FindAePReqEByDocument aePReq = new FindAePReqEByDocument();
    AePReqEType aePReqEType = new AePReqEType();
    aePReq.setAePReqE(aePReqEType);
    SortObjectListType s = new SortObjectListType();
    aePReq.setList(s);

    return aePReq;
}
}  

配置类如下:

public class CustomerRequestConfig {
@Bean
public Jaxb2Marshaller marshaller() {
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    marshaller.setContextPath("my.wsdl");
    return marshaller;
}

@Bean
public CustomerRequestClient customerRequestClient(Jaxb2Marshaller marshaller) {
    CustomerRequestClient client = new CustomerRequestClient();
    client.setDefaultUri(URI);
    client.setMessageSender(httpComponentsMessageSender(client));
    client.setMarshaller(marshaller);
    client.setUnmarshaller(marshaller);


    return client;
}

@Bean
public HttpComponentsMessageSender httpComponentsMessageSender(CustomerRequestClient client) {
    HttpComponentsMessageSender httpComponentsMessageSender = new HttpComponentsMessageSender();

    httpComponentsMessageSender.setCredentials(usernamePasswordCredentials(client));

    return httpComponentsMessageSender;
}

@Bean
public UsernamePasswordCredentials usernamePasswordCredentials(CustomerRequestClient client) {
    System.out.println("Password - " + client.getPassword());
    return new UsernamePasswordCredentials(client.getUserName(), client.getPassword());
}
}

0 个答案:

没有答案