Implementing an authentication supplier

Implementing an authentication supplier

Authentication suppliers are used by clients to supply username and passwords to servers for HTTP Basic authentication. They are user-supplied custom objects that can pre-emptively supply authentication credentials and also handle HTTP 401 challenges (see Handling HTTP basic authentication challenges).

Authentication suppliers are implemented by extending the org.apache.cxf.transport.http.HTTPAuthSupplier class. HTTPAuthSupplier is an abstract class with two operations that need to be overridden:

  • public abstract String getAuthorizationForRealm(HTTPConduit conduit,
                                                    URL currentURL,
                                                    Message message,
                                                    String realm,
                                                    String fullHeader);

    getAuthorizationForRealm() is called when an HTTP server issues a 401 authentication challenge. The realm information is taken from the WWW-Authenticate: ???? realm=????? header. The method determines if there is a valid authentication for the URL, realm, message combination. If there are valid authentication credentials it should return the authentication credentials. If not, it should return null.

    If getAuthorizationForRealm() returns a value other than null, the request is retransmitted. If it returns null the call that initiated the original message fails.

  • public abstract String getPreemptiveAuthorization(HTTPConduit conduit,
                                                      URL currentURL,
                                                      Message message);

    getPreemptiveAuthorization() is called before an HTTP request is made. If there is a valid set of credentials for the URL, the method should return it. If not, it should return null.

    If getPreemptiveAuthorization() returns null, the request is transmitted without authentication credentials.

The following is an example of an authentication supplier:

package com.somecompany;
import org.apache.cxf.transport.http.HttpAuthSupplier;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.message.Message;

public class MyBasicAuthSupplier extends HttpAuthSupplier
{
  MyBasicAuthSupplier()
  {
  }
        
  @Override
  public String getPreemptiveAuthorization(HTTPConduit conduit, 
                                           URL currentURL,
                                           Message message)
  {
    String preemptiveUsername = "examplePreemptiveUsername";
    String preemptiveUsername = "examplePreemptivePassword";
    return createUserPass(preemptiveUsername, preemptivePreemptivePassword);
  }

  @Override
  public String getAuthorizationForRealm(HTTPConduit conduit,
                                         URL currentURL,
                                         Message message,
                                         String reqestedRealm,
                                         String fullHeader)
  {
    String onDemandUsername = "exampleUsername";
    String onDemandUsername = "examplePassword";
    return createUserPass(onDemandUsername, onDemandPassword);
  }

  /* This is a helper method to build the security header */
  private String createUserPass(String usr, String pwd)
  {
    String userpass = usr + ":" + pwd;
    String token = Base64Utility.encode(userpass.getBytes());
    return "Basic " + token;
  }
}

 

url: http://communities.progress.com/infocenter/index.jsp?topic=/com.sonicsw.tools.sonicconnect.doc/auth_supplier_impl.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值