コード例 #1
0
  @Override
  public User getUser(Renderer renderer, String username, String password) throws IOException {
    if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password))
      throw new AuthException("Username or password is empty");
    if (StringUtils.isEmpty(renderer.getAuthServer()))
      throw new AuthException("No auth server given, check the parameters of the renderer");

    ActiveDirectory activeDirectory = null;
    try {
      String domain = renderer.getAuthDomain();
      String authServer = renderer.getAuthServer();

      User user = AuthUserCache.INSTANCE.get(username, domain);
      if (user != null) return user;

      NtlmPasswordAuthentication ntlmAuth = getNtlmAuth(renderer, username, password);
      UniAddress dc = UniAddress.getByName(authServer, true);
      SmbSession.logon(dc, ntlmAuth);

      activeDirectory =
          new ActiveDirectory(authServer, ntlmAuth.getUsername(), ntlmAuth.getPassword(), domain);

      NamingEnumeration<SearchResult> result = activeDirectory.findUser(username);
      Attributes attrs = ActiveDirectory.getAttributes(result);
      if (attrs == null) throw new AuthException("No user found: " + username);

      String userId = ActiveDirectory.getObjectSID(attrs);
      List<ADGroup> groups = new ArrayList<ADGroup>();
      activeDirectory.findUserGroups(attrs, groups);
      String dnUser = ActiveDirectory.getStringAttribute(attrs, "DistinguishedName");
      if (!StringUtils.isEmpty(dnUser)) activeDirectory.findUserGroup(dnUser, groups);

      Logging.info("USER authenticated: " + user);

      user =
          new User(
              userId.toLowerCase(),
              username.toLowerCase(),
              password,
              ActiveDirectory.toArray(groups, "everyone"),
              ActiveDirectory.getDisplayString(domain, username));
      AuthUserCache.INSTANCE.add(username, domain, user);
      return user;

    } catch (SmbAuthException e) {
      Logging.warn(e);
      throw new AuthException("Authentication error (SmbAuthException) : " + e.getMessage());
    } catch (UnknownHostException e) {
      Logging.warn(e);
      throw new AuthException("Authentication error (UnknownHostException) : " + e.getMessage());
    } catch (NamingException e) {
      Logging.warn(e);
      throw new AuthException("LDAP error (NamingException) : " + e.getMessage());
    } finally {
      IOUtils.close(activeDirectory);
    }
  }
コード例 #2
0
  public static void main(String[] args)
      throws NamingException, UnknownHostException, SmbException {

    ActiveDirectory activeDirectory = null;
    try {
      String server = args[0];
      String domain = args[1];
      String username = args[2];
      String password = args[3];

      NtlmPasswordAuthentication ntlmAuth =
          new NtlmPasswordAuthentication(domain, username, password);
      UniAddress dc = UniAddress.getByName(server, true);
      SmbSession.logon(dc, ntlmAuth);

      activeDirectory = new ActiveDirectory(server, username, password, domain);

      NamingEnumeration<SearchResult> result = activeDirectory.findUser(username);

      Attributes attrs = ActiveDirectory.getAttributes(result);
      if (attrs == null) {
        System.out.println("no user found");
        return;
      }
      String userId = ActiveDirectory.getObjectSID(attrs);
      List<ADGroup> groups = new ArrayList<ADGroup>();
      activeDirectory.findUserGroups(attrs, groups);

      String dnUser = ActiveDirectory.getStringAttribute(attrs, "DistinguishedName");
      System.out.println(dnUser);
      if (!StringUtils.isEmpty(dnUser)) activeDirectory.findUserGroup(dnUser, groups);

      String[] groupArray = ActiveDirectory.toArray(groups, "everyone");
      System.out.println(
          new User(
              userId,
              username,
              password,
              groupArray,
              ActiveDirectory.getDisplayString(domain, username)));
      for (String group : groupArray) System.out.println(group);

    } finally {
      if (activeDirectory != null) activeDirectory.close();
    }
  }