当前位置: 首页>>代码示例>>Java>>正文


Java ResultCode.INVALID_CREDENTIALS属性代码示例

本文整理汇总了Java中com.unboundid.ldap.sdk.ResultCode.INVALID_CREDENTIALS属性的典型用法代码示例。如果您正苦于以下问题:Java ResultCode.INVALID_CREDENTIALS属性的具体用法?Java ResultCode.INVALID_CREDENTIALS怎么用?Java ResultCode.INVALID_CREDENTIALS使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在com.unboundid.ldap.sdk.ResultCode的用法示例。


在下文中一共展示了ResultCode.INVALID_CREDENTIALS属性的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: dnFromUsername

private String dnFromUsername(String username) throws LDAPException, GeneralSecurityException {
  String baseDN = config.getUserBaseDN();
  String lookup = String.format("(%s=%s)", config.getUserAttribute(), username);
  SearchRequest searchRequest = new SearchRequest(baseDN, SearchScope.SUB, lookup);

  LDAPConnection connection = connectionFactory.getLDAPConnection();
  try {
    SearchResult sr = connection.search(searchRequest);

    if (sr.getEntryCount() == 0) {
      throw new LDAPException(ResultCode.INVALID_CREDENTIALS);
    }

    return sr.getSearchEntries().get(0).getDN();
  } finally {
    connection.close();
  }
}
 
开发者ID:square,项目名称:keywhiz,代码行数:18,代码来源:LdapAuthenticator.java

示例2: authenticate

@Override
public Optional<User> authenticate(BasicCredentials credentials) {
  User user = null;

  try {
    String username = credentials.getUsername();
    if (!User.isSanitizedUsername(username)) {
      logger.info("Username: {} must match pattern: {}", username, User.USERNAME_PATTERN);
      return Optional.empty();
    }

    String userDN = dnFromUsername(username);
    String password = credentials.getPassword();

    // Must have password for current config
    if (Strings.isNullOrEmpty(password)) {
      logger.info("No password for user provided");
      return Optional.empty();
    }

    LDAPConnection authenticatedConnection = connectionFactory.getLDAPConnection(userDN, password);
    authenticatedConnection.close();

    Set<String> requiredRoles = config.getRequiredRoles();
    if (!requiredRoles.isEmpty()) {
      Set<String> roles = rolesFromDN(userDN);

      boolean accessAllowed = false;
      for (String requiredRole : requiredRoles) {
        if (roles.contains(requiredRole)) {
          accessAllowed = true;
        }
      }

      if (!accessAllowed) {
        logger.warn("User {} not in one of required LDAP roles: [{}].", username, requiredRoles);
        throw new ForbiddenException();
      }
    }

    user = User.named(username);
  } catch (LDAPException le) {
    // The INVALID_CREDENTIALS case is handled by returning an absent optional from this function
    if (le.getResultCode() != ResultCode.INVALID_CREDENTIALS) {
      logger.error("Error connecting to LDAP", le);
      throw Throwables.propagate(le);
    }
  } catch (GeneralSecurityException gse) {
      logger.error("TLS error connecting to LDAP", gse);
      throw Throwables.propagate(gse);
  }

  return Optional.ofNullable(user);
}
 
开发者ID:square,项目名称:keywhiz,代码行数:54,代码来源:LdapAuthenticator.java


注:本文中的com.unboundid.ldap.sdk.ResultCode.INVALID_CREDENTIALS属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。