spingboot整合cxf生成webservice接口

一、pom文件设置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.RELEASE</version>
    </parent>
    <groupId>cn.com.dreamboat</groupId>
    <artifactId>springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 数据库******start******-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- 数据库******end******-->
       <!-- swagger配置******start****** -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.9.6</version>
        </dependency>
        <!-- swagger配置******end****** -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.58</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.5</version>
        </dependency>
        <!-- cxf-webservice******start****** -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.3.6</version>
        </dependency>
        <!-- cxf-webservice******end****** -->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 

二、application.yml设置
#webservice鉴权
webservice:
  auth:
    name: test
    pwd: 123456
    uri: http://service.springboot.dreamboat.com.cn/auth

三、发布文件设置

@Configuration
public class WebServiceConfig {

    @Autowired
    private Bus bus;

    @Autowired
    private TestWebService testWebService;

    @Autowired
    private AuthHandler authHandler;

    @Bean
    public ServletRegistrationBean disServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/webservice/*");
    }

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus, testWebService);
        // 鉴权校验
        endpoint.getHandlers().add(authHandler);
        endpoint.publish("/test");
        return endpoint;
    }
}

四、service类

@WebService(targetNamespace = "http://service.springboot.dreamboat.com.cn")
public interface HelloWebService {

    @WebMethod
    @WSDLDocumentation( "测试webservice方法")
    RestResult<String> testWebServiceMethod(@WebParam(name = "methodParam") String methodParam);
}
@Slf4j
@WebService(serviceName = "TestWebService",
        targetNamespace = "http://service.springboot.dreamboat.com.cn",
        endpointInterface = "cn.com.dreamboat.springboot.service.HelloWebService"
)
@Component
public class HelloWebServiceImpl implements HelloWebService {

    @Override
    public RestResult<String> testWebServiceMethod(String methodParam) {
        // 业务...
        return RestResult.success(methodParam);
    }
}

五、鉴权


@Slf4j
@Component
public class AuthHandler implements SOAPHandler<SOAPMessageContext> {

@Value("${webservice.auth.pwd}")
private String AUTH_PWD;
@Value("${webservice.auth.name}")
private String AUTH_NAME;
@Value("${webservice.auth.uri}")
private String AUTH_URI;

@Override
public void close(MessageContext context) {
}

@Override
public boolean handleFault(SOAPMessageContext context) {
return true;
}

@Override
public boolean handleMessage(SOAPMessageContext context) {
// 判断消息是请求还是响应
Boolean output = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
boolean result = false;
SOAPMessage message = context.getMessage();

//如果是请求,则执行校验
try {
if (!output) {
result = validate(message);

if (!result) {
validateFail(message);
}
}

message.writeTo(System.out);
} catch (SOAPException | IOException e) {
log.error("鉴权验证失败:{}", e.getMessage(), e);
e.printStackTrace();
}

return result;
}

/**
* 授权校验失败,在SOAPBody中添加SOAPFault
* @param message
*/
private void validateFail(SOAPMessage message) throws SOAPException {
SOAPEnvelope envelop = message.getSOAPPart().getEnvelope();

envelop.getHeader().detachNode();
envelop.addHeader();

envelop.getBody().detachNode();
SOAPBody body = envelop.addBody();

SOAPFault fault = body.getFault();

if (fault == null) {
fault = body.addFault();
}

fault.setFaultString("授权校验失败!");

message.saveChanges();
}

/**
* 授权校验
* @param message
* @return 校验成功返回true,校验失败返回false
*/
private boolean validate(SOAPMessage message) throws SOAPException {
boolean result = false;

SOAPEnvelope envelop = message.getSOAPPart().getEnvelope();
SOAPHeader header = envelop.getHeader();

if (header != null) {
Iterator iterator = header.getChildElements(new QName(AUTH_URI, "auth"));
SOAPElement auth;

if (iterator.hasNext()) {
//获取auth
auth = (SOAPElement) iterator.next();

//获取name
Iterator it = auth.getChildElements(new QName(AUTH_URI, "name"));
SOAPElement name = null;
if (it.hasNext()) {
name = (SOAPElement) it.next();
}

//获取password
it = auth.getChildElements(new QName(AUTH_URI, "password"));
SOAPElement password = null;
if (it.hasNext()) {
password = (SOAPElement) it.next();
}

//判断name和password是否符合要求
if (name != null && password != null && AUTH_NAME.equals(name.getValue().trim()) && AUTH_PWD.equals(password.getValue().trim())) {
result = true;
}
}
}

return result;
}

@Override
public Set<QName> getHeaders() {
return null;
}

}
 

六、启动服务查询wsdl

浏览器输入: http://localhost:9527/webservice/test?wsdl

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://service.springboot.dreamboat.com.cn" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="TestWebService" targetNamespace="http://service.springboot.dreamboat.com.cn">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://service.springboot.dreamboat.com.cn" elementFormDefault="unqualified" targetNamespace="http://service.springboot.dreamboat.com.cn" version="1.0">
<xs:element name="testWebServiceMethod" type="tns:testWebServiceMethod"/>
<xs:element name="testWebServiceMethodResponse" type="tns:testWebServiceMethodResponse"/>
<xs:complexType name="testWebServiceMethod">
<xs:sequence>
<xs:element minOccurs="0" name="methodParam" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="testWebServiceMethodResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="tns:restResult"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="restResult">
<xs:sequence>
<xs:element minOccurs="0" name="code" type="xs:int"/>
<xs:element minOccurs="0" name="data" type="xs:anyType"/>
<xs:element minOccurs="0" name="error" type="xs:boolean"/>
<xs:element minOccurs="0" name="msg" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="testWebServiceMethodResponse">
<wsdl:part element="tns:testWebServiceMethodResponse" name="parameters"> </wsdl:part>
</wsdl:message>
<wsdl:message name="testWebServiceMethod">
<wsdl:part element="tns:testWebServiceMethod" name="parameters"> </wsdl:part>
</wsdl:message>
<wsdl:portType name="HelloWebService">
<wsdl:operation name="testWebServiceMethod">
<wsdl:documentation>测试webservice方法</wsdl:documentation>
<wsdl:input message="tns:testWebServiceMethod" name="testWebServiceMethod"> </wsdl:input>
<wsdl:output message="tns:testWebServiceMethodResponse" name="testWebServiceMethodResponse"> </wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="TestWebServiceSoapBinding" type="tns:HelloWebService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="testWebServiceMethod">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="testWebServiceMethod">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="testWebServiceMethodResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="TestWebService">
<wsdl:port binding="tns:TestWebServiceSoapBinding" name="HelloWebServiceImplPort">
<soap:address location="http://localhost:9527/webservice/test"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>六

七、soapui测试

鉴权失败:

 

 鉴权成功:

 

 八、打完收工~

 

posted @ 2020-05-15 22:13  时の封印  阅读(496)  评论(2编辑  收藏  举报