Springboot + CXF Fast implementation of SOAP WebService (supports basic Auth)

Source: Internet
Author: User
Tags soap

Two words of nagging

True, soap is harder to use than the now-popular restful webservice. Redundant XML text information too much, poor readability, its request information is sometimes difficult to manually construct, not very good debugging. But to be honest, the use of soap is still high for some business users.

Requirements background

A project that takes over maintenance, a recent customer wants to open a feature in the project to a third-party invocation, and the provider specifies that the SOAP interface must be the. The original code of the project I looked at the headache, also do not want to change, unless the rewrite. The customer's needs are not difficult but urgent, in order to quickly deliver on the use of Springboot fast to take a micro-service.

Get started.

1 . Create a new spring Starter Project 2 . Join CXF's maven configuration     <dependency>        <groupId>org.apache.cxf</groupId>        <artifactId> Cxf-spring-boot-starter-jaxws</artifactid>        <version>3.1.  </version>    </dependency>

Writing Service Code (sample code)

@WebService (name = "UserService", targetnamespace= "http://demo.example.com/") Public Interface  iuserservice {    @WebMethod    int  ID);     int addUser (@WebParam (name = "User"), user user);
@InInterceptors (interceptors={"Com.example.demo.auth.AuthInterceptor"}) @WebService (ServiceName= "UserService", targetnamespace = "http://demo.example.com/", Endpointinterface = " Com.example.demo.soap.IUserService ") @Component Public classUserserviceimplImplementsIuserservice {PrivateLogger Logger = Loggerfactory.getlogger (Userserviceimpl.class); @AutowiredPrivateIuserdao Userdao; @Override PublicUser Getuserbyid (intID) {returnUserdao.getuserbyid (ID); } @Override Public intaddUser (user user) {logger.info ("Save User [" + user.getid () + "]");        Userdao.adduser (user); return0; }}

Authentication Interceptor

@Component Public classAuthinterceptorextendsAbstractsoapinterceptor {Private Static FinalString basic_prefix = "BASIC"; Private Static FinalString USERNAME = "Lichmama"; Private Static FinalString PASSWORD = "123456";  PublicAuthinterceptor () {Super(Phase.pre_invoke); } @Override Public voidHandlemessage (SoapMessage message)throwsFault {httpservletrequest request=(HttpServletRequest) message.get (abstracthttpdestination.http_request); String Auth= Request.getheader ("Authorization"); if(Auth = =NULL) {            Throw NewIllegalArgumentException ("Auth failed, header [Authorization] NOT EXISTS"); }        if(!Auth.startswith (Basic_prefix)) {            Throw NewIllegalArgumentException ("Auth failed, header [Authorization] is illegal"); } String PlainText=NewString (Base64.getdecoder (). Decode (Auth.substring (Basic_prefix.length ()))); if(Stringutils.isempty (plaintext) | |!plaintext.contains (":")) {            Throw NewIllegalArgumentException ("Auth failed, header [Authorization] is illegal"); } string[] Userandpass= Plaintext.split (":"); String username= Userandpass[0]; String Password= Userandpass[1]; if(! Username.equals (USERNAME) | | !password.equals (PASSWORD)) {            Throw NewIllegalArgumentException ("Auth failed, username or password is incorrect"); }    }}

Writing configuration Classes

@Configuration  Public class soapconfig {    @Autowired    private  iuserservice userservice;    @Autowired    @Qualifier (bus.default_bus_id)    private  springbus Bus;    @Bean    public  Endpoint Endpoint () {        new  Endpointimpl (bus, UserService);        Endpoint.publish ("/userservice");         return endpoint;}    }

Modify CXF Default publishing path (application.properties)

server.port=8000Cxf.path=/soap

Access HTTP://LOCALHOST:8000/SOAP/USERSERVICE?WSDL after starting project

Test it with SOAPUI, it looks fine.

Finally, explain the development environment

STS 3.7.3 + springboot 2.0.1 + JDK1.8

Work is over.

Springboot + CXF Fast implementation of SOAP WebService (supports basic Auth)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.