【问题标题】:How to add Basic authentication in Apache Axis Web Service (SOAP)?如何在 Apache Axis Web 服务 (SOAP) 中添加基本身份验证?
【发布时间】:2018-02-20 16:03:36
【问题描述】:

我使用一个 Maven 插件 (org.codehaus.mojo > axistools-maven-plugin) + 一个 WSDL 文件来生成一个 Soap Web 服务。

target/generated-source/wsdl2java/com.comp.proj 中的生成文件是:

  • Foo.java(java接口)
  • FooServiceLocator.java
  • FooSoapBindingImpl.java(java 空实现)
  • FooSoapBindingSkeleton.java
  • FooSoapBindingStub.java

在我的项目中,我在同名的包中创建FooSoapBindingImpl.java + 在这个 java 实现中添加我的自定义代码。

此 Web 服务已准备好在生产中使用。

所以,今天我在我的客户端上添加了基本身份验证(header => Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==)

如何在我的 Axis Web 服务中添加对此基本身份验证的检查?

【问题讨论】:

  • 我知道这是个老问题,但您可以在此处使用 RPC 和 AXIS 1.x 提供指南。 ibm.com/docs/en/was/8.5.5?topic=SSEQTP_8.5.5/… ``` StockQuote sq = (StockQuote)service.getPort(portQname, StockQuote.class); ((javax.xml.rpc.Stub) sq)._setProperty(javax.xml.rpc.Stub.USERNAME_PROPERTY, "myUser"); ((javax.xml.rpc.Stub) sq)._setProperty(javax.xml.rpc.Stub.PASSWORD_PROPERTY, "myPwd"); ```

标签: java maven soap axis basic-authentication


【解决方案1】:

Axis security section 'Authenticating the caller'”提到:

客户端可以使用客户端证书或 HTTP 基本身份验证来验证自己。
后者在非加密通道上太弱而无法信任,但可以通过 HTTPS 工作。

当 SOAP 消息发布到端点时,MessageContext 类将配置发送者的用户名和密码;*

an example here

使用适当的 getter 来查看这些值。请注意,Axis 尚未与 servlet API 身份验证内容集成。

查看getter example in this answer

【讨论】:

    【解决方案2】:

    如何在我的 Axis Web 服务中添加对此基本身份验证的检查?

    Anwser:可以从WebServiceContext检索基本凭据

    @WebService
    public class Service{
    //Injected by whatever container you are using
    @Resource
    WebServiceContext wctx;
    
    @WebMethod
    public Integer fooServiceLocator(int x, int y){
        //Get The Message Context
        MessageContext mctx = wctx.getMessageContext();
    
        //Grab the HTTP Request Headers
        Map<Object, Object> object = (Map<Object, Object>) mctx.get(MessageContext.HTTP_REQUEST_HEADERS);
    
        //Grab the Authorization Values
        List<String> basicCredentials = (List<String>) object.get("Authorization");
    
        //Print out credentials
        basicCredentials.forEach(System.out::println);
    
        //Do a meaningful check of the credentials
    

    上面的代码解释了如何从内部网络服务检索基本凭据。我的建议是让 Servlet 或 EJB 容器来处理您的应用程序需要的任何安全性。通过让容器处理您的安全性,您的代码在环境之间变得更加可移植。

    【讨论】:

      【解决方案3】:

      最简单的方法是在 WSDL 中添加 SOAP 标头以进行身份​​验证。例如,用户名和密码可以作为新元素添加到 WSDL 文件中的新 SOAP 标头下并重新生成源文件。

      `<xs:element name="UsernameToken"> 
          <xs:complexType> 
              <xs:sequence> 
                  <xs:element ref="Username"/> 
                  <xs:element ref="Password" minOccurs="0"/> 
              </xs:sequence> 
          <xs:attribute name="Id" type="xs:ID"/>
      </xs:complexType></xs:element>`
      

      在WSDL中使用上面的header,如果我们生成源文件,我们可以很容易的在impl中添加认证逻辑。

      然后可以在*Impl.java类中验证用户名和密码。

      【讨论】:

      • 你能复制/粘贴一个样本吗?
      • 用示例标题编辑了我的回复
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-26
      相关资源
      最近更新 更多