⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpauthenticatorfactory.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
字号:
/*
 * Created on Feb 25, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.maverick.http;

import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
 * @author lee
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class HttpAuthenticatorFactory {

  public static final String NTLM = "NTLM";
  public static final String BASIC = "Basic";
  public static final String DIGEST = "Digest";
  public static final String NONE = "None";

  /* DEBUG */static Log log = LogFactory.getLog(HttpAuthenticatorFactory.class);

  public static HttpAuthenticator createAuthenticator(HttpConnection con,
      String[] challenges,
      String authenticationHeader,
      String authorizationHeader,
      String pref,
      String uri) throws UnsupportedAuthenticationException {

    HttpAuthenticator authenticator = null;


    String actualChallenge = "";



    if(pref!=null) {

      boolean prefAvailable = false;
      for (int x = 0; pref!=null && x < challenges.length; x++) {
          if (challenges[x].toLowerCase().startsWith(pref.toLowerCase())) {
              prefAvailable = true;
              actualChallenge = challenges[x];
              break;
          }
        }

        if(prefAvailable) {
          if (pref.equalsIgnoreCase(BASIC)) {
            authenticator = new BasicAuthentication(uri);
          }
          else if (pref.equalsIgnoreCase(NTLM)) {
            authenticator = new NTLMAuthentication(uri);
          }
          else if (pref.equalsIgnoreCase(DIGEST)) {
            authenticator = new DigestAuthentication(uri);
          }
        }
    } else{
    for(int i=0;i<challenges.length;i++) {
        String method = getAuthenticationMethod(challenges[i]);

        if (method.equalsIgnoreCase(BASIC)) {
            authenticator = new BasicAuthentication(uri);
        } else if (method.equalsIgnoreCase(NTLM)) {
            authenticator = new NTLMAuthentication(uri);
        } else if (method.equalsIgnoreCase(DIGEST)) {
            authenticator = new DigestAuthentication(uri);
        }

        if(authenticator!=null) {
            actualChallenge = challenges[i];
            break;
        }
      }
    }
    if (authenticator != null) {

        /* DEBUG */log.info("Created " + authenticator.getScheme() + " authenticator");
        
        authenticator.setConnection(con);
        authenticator.setChallenge(actualChallenge);
        authenticator.setAuthenicationHeader(authenticationHeader);
        authenticator.setAuthorizationHeader(authorizationHeader);

        return authenticator;
    }

    if(pref==null)
      throw new UnsupportedAuthenticationException(challenges);
    else
      throw new UnsupportedAuthenticationException(challenges,
         "Preferred authentication scheme " + pref + " is not supported by host/proxy");
  }

  public static String getAuthenticationMethod(String challenge) {
    String method = challenge;

    if (method != null) {
      int n = method.indexOf(' ');
      if (n > -1)
        method = method.substring(0, n);
    }

    return method;
  }

  public String getAuthenticationRealm(String challenge) {
    String auth = challenge;
    String realm = "";

    if (auth != null) {
      int l;
      int r = auth.indexOf('=');

      while (r >= 0) {
        l = auth.lastIndexOf(' ', r);
        realm = auth.substring(l + 1, r);

        if (realm.equalsIgnoreCase("realm")) {
          l = r + 2;
          r = auth.indexOf('"', l);
          realm = auth.substring(l, r);

          break;
        }

        r = auth.indexOf('=', r + 1);
      }
    }

    return realm;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -