Spring WS / boot:如何在客户端拦截器中设置WebServiceTemplate属性?

时间:2017-03-18 00:53:55

标签: java spring soap spring-boot spring-ws

我的应用程序是SOAP服务器和客户端,我在客户端和Web服务提供者之间充当代理服务器。所以我得到不同的客户端请求,我将通过设置适当的SSL / TS配置进行验证并将它们路由到目标soap服务。我正在使用Spring WS进行SOAP Web服务调用。下面的代码工作正常:

     try {
            webServiceTemplate.setMessageSender(certManager.setKeystores());
            // perform the transaction
            SOAPResponse =  (JAXBElement<Response>) webServiceTemplate
                    .marshalSendAndReceive(SoapRequest);
        } catch(Exception e) {
            // catching a broad exception here because many things can go wrong,
            //but any exception means total failure
            throw new ServiceException(e);
        }

但我正在尝试一种不同的方法,我在客户端拦截器中设置SSL配置:

public class AppClientInterceptor implements ClientInterceptor{

    private static final Logger LOGGER = LoggerFactory
            .getLogger(AppClientInterceptor.class);
    @Autowired
    WebServiceTemplate webServiceTemplate;
    @Autowired
    CertManager certManager;

    @Override
    public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException {
        // pull out the request from the current thread

        try {
            LOGGER.info("before setting message sender");
            if(webServiceTemplate!=null)
            webServiceTemplate.setMessageSender(certManager.setKeystores());
            else
            System.out.println("web service tempate is null");  
            return false;
        } catch (GeneralSecurityException | IOException e) {
            // TODO Auto-generated catch block
            LOGGER.info("In catch block unable to set keystores");
            e.printStackTrace();
        }

        return false;
    }

当我进行webservice调用时,会调用客户端拦截器,但是当webservicetemplate为null时,它会失败。我连接了Web服务模板,但无法理解为什么它的null。我使用的webservice模板如下:

@Bean
    public WebServiceTemplate webServiceTemplate() {

        WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
        webServiceTemplate.setMarshaller(jaxb2Marshaller());
        webServiceTemplate.setUnmarshaller(jaxb2Marshaller());
        ClientInterceptor[] interceptors = {new AppClientInterceptor()};
        webServiceTemplate.setInterceptors(interceptors);
        webServiceTemplate.setDefaultUri(
                "https://destinationserver.com:10072");

        return webServiceTemplate;
    }

我能否以上述方式在拦截器中设置SSL配置?这是一种正确的方法吗?

提前致谢

1 个答案:

答案 0 :(得分:1)

您是否在课程定义之前尝试过@Configurable注释?

@Configurable
public class AppClientInterceptor implements ClientInterceptor{
    ...
}