春天未经授权[401]

时间:2018-04-19 19:13:12

标签: spring-mvc jaxb spring-ws jaxb2

我正在尝试使用肥皂服务。该服务没有证书,但作为请求用户名和密码的属性接收。 但我收到以下错误:

org.springframework.ws.client.WebServiceTransportException: Unauthorized [401]

我试过几种方法来传递用户名和密码。 使用soapui我能够进行申请。在soapui中,我通过请求属性传递用户名和密码。

 @Bean
    Jaxb2Marshaller jaxb2Marshaller() {
        Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
        jaxb2Marshaller.setPackagesToScan("ce.gov.s2gpr.compras.licita.business.model.service.bean");
        return jaxb2Marshaller;
    }


    @Bean
    public WebServiceTemplate webServiceTemplate() {
        WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
        webServiceTemplate.setMarshaller(jaxb2Marshaller());
        webServiceTemplate.setUnmarshaller(jaxb2Marshaller());
        webServiceTemplate.setDefaultUri(defaultUri);

        HttpsUrlConnectionMessageSender sender = new HttpsUrlConnectionMessageSender();
        sender.setTrustManagers(new TrustManager[] {
                new X509TrustManager() {
                    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                        return null;
                    }
                    public void checkClientTrusted(
                            java.security.cert.X509Certificate[] certs, String authType) {
                    }
                    public void checkServerTrusted(
                            java.security.cert.X509Certificate[] certs, String authType) {
                    }
                }
        });

        webServiceTemplate.setMessageSenders(new WebServiceMessageSender[]{sender, httpComponentsMessageSender()});

        return webServiceTemplate;
    }


    @Bean
    public HttpComponentsMessageSender httpComponentsMessageSender() {
        HttpComponentsMessageSender httpComponentsMessageSender = new HttpComponentsMessageSender();
        httpComponentsMessageSender.setConnectionTimeout(timeout);
        httpComponentsMessageSender.setReadTimeout(DEFAULT_READ_TIMEOUT_MILLISECONDS);
        httpComponentsMessageSender.setReadTimeout(timeout);
        httpComponentsMessageSender.setCredentials(new UsernamePasswordCredentials(userName, userPassword));

        return httpComponentsMessageSender;
    }

我尝试了几种方法来传递用户名和密码,但我收到了同样的错误。

@SuppressWarnings("unchecked")
    public AtualizacaoItensGruposResponse testeSoap(PgeTO dto) {

    ObjectFactory factory = new ObjectFactory();

    AtualizacaoItensGrupos itens = factory.createAtualizacaoItensGrupos();

    itens.setArg0(dto);
    JAXBElement<AtualizacaoItensGrupos> request =  factory.createAtualizacaoItensGrupos(itens);

    try {

        WebServiceMessageCallback wsCallback = message -> {
            TransportContext context = TransportContextHolder.getTransportContext();
            WebServiceConnection connection = context.getConnection();
            HttpUrlConnection conn = (HttpUrlConnection) connection;

            conn.getConnection().setRequestProperty("Username", "");
            conn.getConnection().setRequestProperty("Password", "");
            conn.getConnection().setRequestProperty("username", "");
            conn.getConnection().setRequestProperty("password", "");

            conn.getConnection().addRequestProperty("Username", "");
            conn.getConnection().addRequestProperty("Password", "");
            conn.getConnection().addRequestProperty("username", "");
            conn.getConnection().addRequestProperty("password", "");
            conn.getConnection().addRequestProperty("s2gpr.integracao.cotacao.item_licitacao.webservice.user", "");
            conn.getConnection().addRequestProperty("s2gpr.integracao.cotacao.item_licitacao.webservice.password", "");
        };

        webServiceTemplate.marshalSendAndReceive(request, wsCallback);
        return null;
    } catch (Exception e) {
        log.error(e.getMessage());
        throw new ClientLicitaWebException("Ocorreu um erro ao enviar as informações para o sistema LicitaWeb");
    }

}

0 个答案:

没有答案