唠叨两句
讲真,SOAP跟现在流行的RESTful WebService比起来显得很难用。冗余的XML文本信息太多,可读性差,它的请求信息有时很难手动构造,不太好调试。不过说归说,对某些企业用户来说SOAP的使用率仍然是很高的。
需求背景
接手维护的一个项目,最近客户想开放项目中的功能给第三方调用,而且接入方指定必须是SOAP接口。这项目原来的代码我看着头疼,也不想再改了,除非推倒重写。客户的需求虽然不难但要的很急,为了尽快交付就使用SpringBoot快速搭一个微服务。
开始动手
1.新建一个Spring Starter Project 2.加入cxf的maven配置 <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.1.15</version> </dependency>
编写服务代码(示例代码)
@WebService(name = "UserService", targetNamespace="http://demo.example.com/") public interface IUserService { @WebMethod User getUserById(@WebParam(name = "id") 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 class UserServiceImpl implements IUserService { private Logger logger = LoggerFactory.getLogger(UserServiceImpl.class); @Autowired private IUserDAO userDAO; @Override public User getUserById(int id) { return userDAO.getUserById(id); } @Override public int addUser(User user) { logger.info("save user [" + user.getId() + "]"); userDAO.addUser(user); return 0; } }
鉴权拦截器
@Component public class AuthInterceptor extends AbstractSoapInterceptor { private static final String BASIC_PREFIX = "Basic "; private static final String USERNAME = "lichmama"; private static final String PASSWORD = "123456"; public AuthInterceptor() { super(Phase.PRE_INVOKE); } @Override public void handleMessage(SoapMessage message) throws Fault { HttpServletRequest request = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST); String auth = request.getHeader("Authorization"); if (auth == null) { throw new IllegalArgumentException("auth failed, header [Authorization] not exists"); } if (!auth.startsWith(BASIC_PREFIX)) { throw new IllegalArgumentException("auth failed, header [Authorization] is illegal"); } String plaintext = new String(Base64.getDecoder().decode(auth.substring(BASIC_PREFIX.length()))); if (StringUtils.isEmpty(plaintext) || !plaintext.contains(":")) { throw new IllegalArgumentException("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 new IllegalArgumentException("auth failed, username or password is incorrect"); } } }
编写配置类
@Configuration public class SoapConfig { @Autowired private IUserService userService; @Autowired @Qualifier(Bus.DEFAULT_BUS_ID) private SpringBus bus; @Bean public Endpoint endpoint() { EndpointImpl endpoint = new EndpointImpl(bus, userService); endpoint.publish("/userService"); return endpoint; } }
修改CXF默认发布路径(application.properties)
server.port=8000 cxf.path=/soap
启动项目后访问http://localhost:8000/soap/userService?wsdl
使用SoapUI测试一下,看上去没什么问题
最后交代一下开发环境
STS 3.7.3 + SpringBoot 2.0.1 + JDK1.8
收工下班了。
原文地址:https://www.cnblogs.com/lichmama/p/8728262.html
时间: 2024-10-11 22:53:19