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

📄 ntlmauthentication.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
字号:
package com.maverick.http;

import java.io.IOException;

public class NTLMAuthentication
    extends HttpAuthenticator {

  NTLM ntlm = new NTLM();
  String host = "";
  String domain = "";

  String challenge = null;

  private static final int INITIATED           = 1;
  private static final int TYPE1_MSG_GENERATED = 2;
  private static final int TYPE2_MSG_RECEIVED  = 3;
  private static final int TYPE3_MSG_GENERATED = 4;
  private static final int FAILED              = Integer.MAX_VALUE;

  int state;

  boolean isAuthenticated = false;

  public NTLMAuthentication(String uri) {
    super("NTLM", uri);
    this.state = INITIATED;
  }

  public boolean isStateless() {
      return false;
  }

  public void setChallenge(String challenge) {

  }

  public void setDomain(String domain) {
    this.domain = domain;
  }
  
  public void setCredentials(PasswordCredentials credentials) {
      
      if(credentials!=null && credentials.getUsername() !=null
                      && credentials.getUsername().indexOf('\\') > -1) {
          int idx = credentials.getUsername().indexOf('\\');
          domain = credentials.getUsername().substring(0, idx);
          
          this.credentials = new PasswordCredentials(credentials.getUsername().substring(idx+1),
              credentials.getPassword());
          
      } else
          this.credentials = credentials;
  }
  
  /**
   * authenticate
   *
   * @param request HttpRequest
   * @return boolean
   * @throws ProxyException
   * @todo Implement this com.maverick.proxy.http.HttpAuthenticator method
   */
  public void authenticate(HttpRequest request, HttpMethod method) throws IOException {


     switch(state) {
        case INITIATED:
          request.setHeaderField(authorizationHeader,
                                 "NTLM " +
                                 ntlm.getResponseFor(challenge,
                                                     credentials.getUsername(),
                                                     credentials.getPassword(),
                                                     connection.getHost(),
                                                     domain));
          this.state = TYPE1_MSG_GENERATED;
          break;
        case TYPE2_MSG_RECEIVED:
          request.setHeaderField(authorizationHeader,
                                 "NTLM " +
                                 ntlm.getResponseFor(challenge,
                                                     credentials.getUsername(),
                                                     credentials.getPassword(),
                                                     connection.getHost(),
                                                     domain));
          this.state = TYPE3_MSG_GENERATED;
          break;
        case TYPE3_MSG_GENERATED:
        case TYPE1_MSG_GENERATED:
        default:
          throw new IOException("Invalid authentication state in NTLM authentication");

     }
  }

  private void reset() {
    state = INITIATED;
    challenge = null;
    ntlm = new NTLM();
    domain = "";
  }

  public boolean wantsPrompt() {
    return state==INITIATED && super.wantsPrompt();
  }

  public boolean canAuthenticate() {
    return state==INITIATED || state==TYPE2_MSG_RECEIVED;
  }


  public int processResponse(HttpResponse response) {

    if(response.getStatus()>= 200 && response.getStatus() < 400) {
      reset();
      return AUTHENTICATION_COMPLETED;
    }
    String[] challenges = response.getHeaderFields(authenticationHeader);

    challenge = null;

    for(int i=0;i<challenges.length;i++) {
      if(challenges[i].startsWith("NTLM")) {
        challenge = challenges[i];
        break;
      }
    }

    if(challenge==null || challenge.equals("NTLM")) {
      reset();
      return AUTHENTICATION_FAILED;
    } else {
      challenge = challenge.substring(5).trim();
      this.state = TYPE2_MSG_RECEIVED;
      return AUTHENTICATION_IN_PROGRESS;
    }
  }

  public boolean isAuthenticated() {
    return isAuthenticated;
  }

  void complete() {
    reset();
    super.complete();
  }

}

⌨️ 快捷键说明

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