zoukankan      html  css  js  c++  java
  • 使用 SpringBoot 集成 WebService [需要身份验证]

    1. 使用 JDK 自带的 wsimport 工具生成实体类

      1.1 创建身份验证文件(用于 Webservice 身份验证—auth.txt

      # 格式
      http://账号:密码@wsdl地址
      # 案例
      http://userName:password@192.168.1.3/ReportServer/reportservice2010.asmx?wsdl

      1.2 创建文件夹(用于保存生成的WebService实体类—wsdl

       

      1.3 在 JDK 安装目录下的 bin 目录下,输入 cmd 进入黑窗口

       

      1.4 输入命令,生成客户端实体类

      wsimport -s 实体类保存文件夹路径 -p 包路径 -encoding utf-8 -keep -verbose -Xauthfile 验权文件路径 wsdl地址
      # 案例
      wsimport -s d:wsdl -p cn.lqdev.webservice -encoding utf-8 -keep -verbose -Xauthfile d:auth.txt http://192.168.x.x/ReportServer/reportservice2010.asmx?wsdl

       

    2. 将实体类导入到项目中

      SOPA方式

      需要依赖

      <!--WebService 所需依赖-->
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web-services</artifactId>
      </dependency>

      创建身份验证类

      import org.springframework.beans.factory.annotation.Value;
      import org.springframework.stereotype.Component;
      ​
      import java.net.Authenticator;
      import java.net.PasswordAuthentication;
      ​
      /**
       * <p>描述: [返回鉴权信息] </p>
       * <p>创建时间: 2020/4/7 </p>
       *
       * @author <a href="mailto:chenys@highzap.com" rel="nofollow">Chenys</a>
       * @version v1.0
       */
      @Component
      public class SsrsAuthenticator extends Authenticator {
          @Value("${report.ssrs.confirm.userName}")
          private String ssrsUserName;
          @Value("${report.ssrs.confirm.password}")
          private String ssrsPassword;
      ​
          /**
           * 描述: [重写父类方法 返回鉴权信息]
           *
           * @param
           * @return
           * @throws
           * @date 2020/4/7 16:29
           */
          @Override
          public PasswordAuthentication getPasswordAuthentication() {
              return new PasswordAuthentication(ssrsUserName, ssrsPassword.toCharArray());
          }
      }
       

      创建配置类(返回SOAP接口的实现类

      import com.highzap.report.webservice.reportingservice.ReportingService2010;
      import com.highzap.report.webservice.reportingservice.ReportingService2010Soap;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.beans.factory.annotation.Value;
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;
      ​
      import java.net.Authenticator;
      import java.net.MalformedURLException;
      import java.net.URL;
      ​
      /**
       * <p>描述: [ReportingService 配置类] </p>
       * <p>创建时间: 2020/4/2 </p>
       *
       * @author <a href="mailto:chenys@highzap.com" rel="nofollow">Chenys</a>
       * @version v1.0
       */
      @Configuration
      public class SsrsConfig {
      ​
          @Autowired
          private SsrsAuthenticator ssrsAuthenticator;
      ​
          @Value("${report.ssrs.wsdl}")
          private String wsdl;
      ​
          /**
           * 描述: [获取一个实例对象]
           *
           * @param
           * @return reportingService2010Soap
           * @date 2020/4/3 14:55
           */
          @Bean
          public ReportingService2010Soap getReportingService2010Soap() throws MalformedURLException {
              // SOAP鉴权
              Authenticator.setDefault(ssrsAuthenticator);
              // 返回一个SOAP接口实现类
              return new ReportingService2010(new URL(wsdl)).getReportingService2010Soap();
          }
      }

      CXF 方式(两种方式效果一样

      需要依赖

      <!--WebService 所需依赖-->
      <dependency>
          
      </dependency>

       

    创建配置类(返回SOAP接口的实现类

      
     /**
        * <p>描述: [ReportingService 配置类] </p>
        * <p>创建时间: 2020/4/2 </p>
        *
        * @author <a href="mailto:chenys@highzap.com" rel="nofollow">Chenys</a>
        * @version v1.0
        */
       @Configuration
       public class SsrsConfig {
       
           @Value("${report.ssrs.wsdl}")
           private String wsdl;
       
           /**
            * 描述: [获取一个实例对象]
            *
            * @param
            * @return reportingService2010Soap
            * @date 2020/4/3 14:55
            */
           @Bean
           public ReportingService2010Soap getReportingService2010Soap() throws MalformedURLException {
               // 代理工厂
               JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
               // 认证用户、密码
               jaxWsProxyFactoryBean.setUsername("userName");
               jaxWsProxyFactoryBean.setPassword("password");
               // 设置代理地址
               jaxWsProxyFactoryBean.setAddress(wsdl);
               // 设置接口类型
               jaxWsProxyFactoryBean.setServiceClass(ReportingService2010Soap.class);
               // 创建一个代理接口实现
               ReportingService2010Soap us = (ReportingService2010Soap) jaxWsProxyFactoryBean.create();
               return us;
           }
       }

     

    1. 调用

      package com.highzap.report.proxy.ssrs;
      ​
      import com.highzap.report.dto.ssrs.GetReportDTO;
      import com.highzap.report.dto.ssrs.UploadReportDTO;
      import com.highzap.report.exception.ReportBusinessException;
      import com.highzap.report.webservice.reportingservice.ArrayOfCatalogItem;
      import com.highzap.report.webservice.reportingservice.ArrayOfProperty;
      import com.highzap.report.webservice.reportingservice.CatalogItem;
      import com.highzap.report.webservice.reportingservice.ReportingService2010Soap;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Component;
      ​
      import java.util.List;
      ​
      /**
       * <p>描述: [封装 ReportingService 官方代理类] </p>
       * <p>创建时间: 2020/4/2 </p>
       *
       * @author <a href="mailto:chenys@highzap.com" rel="nofollow">Chenys</a>
       * @version v1.0
       */
      @Component
      public class SsrsProxy {
          @Autowired
          private ReportingService2010Soap reportingService2010Soap;
      ​
          private String notExist = "Unknown";
      ​
          /**
           * 描述: [报表上传]
           *
           * @param uploadReportDTO [上传报表方法 参数封装]
           * @return
           * @date 2020/4/2 15:02
           */
          public void uploadReport(UploadReportDTO uploadReportDTO) {
              reportingService2010Soap
                      .createCatalogItem(
                              uploadReportDTO.getItemType(),
                              uploadReportDTO.getName(),
                              uploadReportDTO.getParent(),
                              uploadReportDTO.getOverwrite(),
                              uploadReportDTO.getDefinition(),
                              uploadReportDTO.getProperties(),
                              uploadReportDTO.getItemInfo(),
                              uploadReportDTO.getWarning());
          }
      ​
      }
      
      

    常见问题

    1. 报错:401-鉴权失败(Authentication failure)—代码没有问题,部分环境运行报错;

      原因:JDK放置在了 C盘,导致JDK的安全策略,会禁止proxy使用用户名密码这种鉴权方式;

      解决办法:修改JDK路径下的文件(jdk1.8.0_111/jre/lib/net.properties)

       

    1. 报错信息:缺少xxxxImpl类

      报错原因:JDK版本问题

      解决办法:切换JDK版本为 JDK1.8

  • 相关阅读:
    利用dockerfile定制镜像
    发布Docker 镜像到dockerhub
    Docker 停止容器
    133. Clone Graph
    132. Palindrome Partitioning II
    131. Palindrome Partitioning
    130. Surrounded Regions
    129. Sum Root to Leaf Numbers
    128. Longest Consecutive Sequence
    127. Word Ladder
  • 原文地址:https://www.cnblogs.com/damaoa/p/12763400.html
Copyright © 2011-2022 走看看