Spring Boot Web 服务客户端身份验证

Spring Boot Web 服务客户端身份验证

问题描述:

我的目标是调用需要身份验证的网络服务(当我在浏览器中打开 wsdl 时,浏览器要求我登录+密码).

My goal is to call web service, which is require authentification (when I opne it's wsdl in my browser, browser asks me login+password).

作为基础,我使用了教程中的示例.

As a base, I use the sample from this tutorial.

现在我必须添加身份验证配置.

And now I have to add authentification configurations.

根据 文档 类似配置 WebServiceTemplatebean 可能会有所帮助.

Accoding to the documentation something like configuring WebServiceTemplate bean may help.

但是对于 Spring Boot,项目中没有 applicationContext.xml 或任何其他配置 xml.

But with Spring Boot there are no applicationContext.xml or any other configuration xml's in a project.

那么,如何使用 Spring Boot 配置 WebServiceTemplate,或者还有什么可以解决这样的任务?

So, how to configure WebServiceTemplate using Spring Boot, or what else can solve such task?

在 Spring Boot 中,您可以使用 @Bean 注释配置 bean.您可以为不同的 bean 使用配置类.在这些类中,您需要 @Configuaration 注释.

In Spring Boot you are able to configure your beans with the @Bean annotation. You can use configuration classes for different beans. In those classes you need the @Configuaration annotation.

这个教程 描述了 Spring 教程的第二部分".提供的教程主要内容是:(基于Spring教程)

This tutorial describes the "second part" of the Spring tutorial. The main things of provided tutorial is: (based on the Spring tutorial)

问题

我使用的 SOAP 网络服务需要基本的 http 身份验证,所以我需要向请求添加身份验证标头.

The SOAP webservice I consume requires basic http authentication, so I need to add authentication header to the request.

无需身份验证

首先,您需要在没有身份验证就像在 spring.io 上的教程中一样.接着我会使用身份验证标头修改 http 请求.

First of all you need to have implemented a request without the authentication like in the tutorial on the spring.io. Then I will modify the http request with the authentication header.

在自定义 WebServiceMessageSender 中获取 http 请求

原始 http 连接可在 WeatherConfiguration 中访问班级.在weatherClient中,您可以设置消息发送者网络服务模板.消息发送者可以访问原始 http联系.所以现在是时候延长HttpUrlConnectionMessageSender 并编写它的自定义实现这会将身份验证标头添加到请求中.我的习惯发件人如下:

The raw http connection is accessible in the WeatherConfiguration class. There in the weatherClient you can set the message sender in the WebServiceTemplate. The message sender has access to the raw http connection. So now it’s time to extend the HttpUrlConnectionMessageSender and write custom implementation of it that will add the authentication header to the request. My custom sender is as follows:

public class WebServiceMessageSenderWithAuth extends HttpUrlConnectionMessageSender{

@Override
protected void prepareConnection(HttpURLConnection connection)
        throws IOException {

    BASE64Encoder enc = new sun.misc.BASE64Encoder();
    String userpassword = "yourLogin:yourPassword";
    String encodedAuthorization = enc.encode( userpassword.getBytes() );
    connection.setRequestProperty("Authorization", "Basic " + encodedAuthorization);

    super.prepareConnection(connection);
}

@Bean
public WeatherClient weatherClient(Jaxb2Marshaller marshaller){

WebServiceTemplate template = client.getWebServiceTemplate();
template.setMessageSender(new WebServiceMessageSenderWithAuth());

return client;
}