if (notFound.getActualSize() == 0) { throw new UsernameNotFoundException("User " + username + " not found in directory.");
private DirContextOperations searchForUser(DirContext context, String username) throws NamingException { SearchControls searchControls = new SearchControls(); searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE); String bindPrincipal = createBindPrincipal(username); String searchRoot = rootDn != null ? rootDn : searchRootFromPrincipal(bindPrincipal); try { return SpringSecurityLdapTemplate.searchForSingleEntryInternal(context, searchControls, searchRoot, searchFilter, new Object[] { bindPrincipal, username }); } catch (IncorrectResultSizeDataAccessException incorrectResults) { // Search should never return multiple results if properly configured - just // rethrow if (incorrectResults.getActualSize() != 0) { throw incorrectResults; } // If we found no results, then the username/password did not match UsernameNotFoundException userNameNotFoundException = new UsernameNotFoundException( "User " + username + " not found in directory.", incorrectResults); throw badCredentials(userNameNotFoundException); } }
@Test @SuppressWarnings("deprecation") // on JDK 9 public void withEquivalentIntegerInstanceTwice() { Collection<Integer> col = new ArrayList<>(2); col.add(new Integer(5)); col.add(new Integer(5)); try { DataAccessUtils.uniqueResult(col); fail("Should have thrown IncorrectResultSizeDataAccessException"); } catch (IncorrectResultSizeDataAccessException ex) { // expected assertEquals(1, ex.getExpectedSize()); assertEquals(2, ex.getActualSize()); } }
assertEquals(2, ex.getActualSize()); assertEquals(2, ex.getActualSize()); assertEquals(2, ex.getActualSize()); assertEquals(2, ex.getActualSize()); assertEquals(2, ex.getActualSize());
assertEquals(0, ex.getActualSize()); assertEquals(0, ex.getActualSize()); assertEquals(0, ex.getActualSize()); assertEquals(0, ex.getActualSize());
/** {@inheritDoc} */ @Override public synchronized int getServiceId(String serviceName) throws DataAccessException { Assert.notNull(serviceName, "The serviceName argument must not be null"); if (m_serviceMap.containsKey(serviceName)) { return m_serviceMap.get(serviceName).intValue(); } else { LOG.debug("Could not find entry for '{}' in service name cache. Looking up in database.", serviceName); int serviceId; try { serviceId = new JdbcTemplate(m_dataSource).queryForObject("SELECT serviceID FROM service WHERE serviceName = ?", new Object[] { serviceName }, Integer.class); } catch (IncorrectResultSizeDataAccessException e) { if (e.getActualSize() == 0) { LOG.debug("Did not find entry for '{}' in database.", serviceName); return -1; // not found } else { throw e; // more than one found... WTF?!?! } } m_serviceMap.put(serviceName, serviceId); LOG.debug("Found entry for '{}' (ID {}) in database. Adding to service name cache.", serviceName, serviceId); return serviceId; } }
/** {@inheritDoc} */ @Override public synchronized int getServiceId(String serviceName) throws DataAccessException { Assert.notNull(serviceName, "The serviceName argument must not be null"); if (m_serviceMap.containsKey(serviceName)) { return m_serviceMap.get(serviceName).intValue(); } else { LOG.debug("Could not find entry for '{}' in service name cache. Looking up in database.", serviceName); int serviceId; try { serviceId = new JdbcTemplate(m_dataSource).queryForObject("SELECT serviceID FROM service WHERE serviceName = ?", new Object[] { serviceName }, Integer.class); } catch (IncorrectResultSizeDataAccessException e) { if (e.getActualSize() == 0) { LOG.debug("Did not find entry for '{}' in database.", serviceName); return -1; // not found } else { throw e; // more than one found... WTF?!?! } } m_serviceMap.put(serviceName, serviceId); LOG.debug("Found entry for '{}' (ID {}) in database. Adding to service name cache.", serviceName, serviceId); return serviceId; } }
/** * Return the LdapUserDetails containing the user's information * * @param username the username to search for. * * @return An LdapUserDetails object containing the details of the located user's directory entry * * @throws UsernameNotFoundException if no matching entry is found. */ public DirContextOperations searchForUser(String username) { if (logger.isDebugEnabled()) { logger.debug("Searching for user '" + username + "', with user search " + this); } SpringSecurityLdapTemplate template = new SpringSecurityLdapTemplate(contextSource); template.setSearchControls(searchControls); try { return template.searchForSingleEntry(searchBase, searchFilter, new String[] {username}); } catch (IncorrectResultSizeDataAccessException notFound) { if (notFound.getActualSize() == 0) { throw new UsernameNotFoundException("User " + username + " not found in directory.", username); } // Search should never return multiple results if properly configured, so just rethrow throw notFound; } }
if (notFound.getActualSize() == 0) { throw new UsernameNotFoundException("User " + username + " not found in directory.");
private DirContextOperations searchForUser(DirContext context, String username) throws NamingException { SearchControls searchControls = new SearchControls(); searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE); String bindPrincipal = createBindPrincipal(username); String searchRoot = rootDn != null ? rootDn : searchRootFromPrincipal(bindPrincipal); try { return SpringSecurityLdapTemplate.searchForSingleEntryInternal(context, searchControls, searchRoot, searchFilter, new Object[] { bindPrincipal, username }); } catch (IncorrectResultSizeDataAccessException incorrectResults) { // Search should never return multiple results if properly configured - just // rethrow if (incorrectResults.getActualSize() != 0) { throw incorrectResults; } // If we found no results, then the username/password did not match UsernameNotFoundException userNameNotFoundException = new UsernameNotFoundException( "User " + username + " not found in directory.", incorrectResults); throw badCredentials(userNameNotFoundException); } }
/** * Return the LdapUserDetails containing the user's information * * @param username the username to search for. * * @return An LdapUserDetails object containing the details of the located user's directory entry * * @throws UsernameNotFoundException if no matching entry is found. */ public LdapUserDetails searchForUser(String username) { if (logger.isDebugEnabled()) { logger.debug("Searching for user '" + username + "', with user search " + this.toString()); } LdapTemplate template = new LdapTemplate(initialDirContextFactory); template.setSearchControls(searchControls); try { LdapUserDetailsImpl.Essence user = (LdapUserDetailsImpl.Essence) template.searchForSingleEntry(searchBase, searchFilter, new String[] {username}, userDetailsMapper); user.setUsername(username); return user.createUserDetails(); } catch (IncorrectResultSizeDataAccessException notFound) { if (notFound.getActualSize() == 0) { throw new UsernameNotFoundException("User " + username + " not found in directory."); } // Search should never return multiple results if properly configured, so just rethrow throw notFound; } }
if (ex.getActualSize() != 0) { LOG.error("current corpus sql returned more than one row, aborting", ex); throw ex;
@Override protected final HandlerResult authenticateUsernamePasswordInternal(final UsernamePasswordCredential credential) throws GeneralSecurityException, PreventedException { if (StringUtils.isBlank(this.sql) || getJdbcTemplate() == null) { throw new GeneralSecurityException("Authentication handler is not configured correctly"); } final String username = credential.getUsername(); final String encryptedPassword = this.getPasswordEncoder().encode(credential.getPassword()); try { final String dbPassword = getJdbcTemplate().queryForObject(this.sql, String.class, username); if (!dbPassword.equals(encryptedPassword)) { throw new FailedLoginException("Password does not match value on record."); } } catch (final IncorrectResultSizeDataAccessException e) { if (e.getActualSize() == 0) { throw new AccountNotFoundException(username + " not found with SQL query"); } else { throw new FailedLoginException("Multiple records found for " + username); } } catch (final DataAccessException e) { throw new PreventedException("SQL exception while executing query for " + username, e); } return createHandlerResult(credential, this.principalFactory.createPrincipal(username), null); }
if (ex.getActualSize() != 0) { LOG.error("balance request sql returned more than one row, aborting", ex); throw ex;
@Override protected final HandlerResult authenticateUsernamePasswordInternal(final UsernamePasswordCredential transformedCredential) throws GeneralSecurityException, PreventedException { if (StringUtils.isBlank(this.sql) || StringUtils.isBlank(this.algorithmName) || getJdbcTemplate() == null) { throw new GeneralSecurityException("Authentication handler is not configured correctly"); } final String username = getPrincipalNameTransformer().transform(transformedCredential.getUsername()); final String encodedPsw = this.getPasswordEncoder().encode(transformedCredential.getPassword()); try { final Map<String, Object> values = getJdbcTemplate().queryForMap(this.sql, username); final String digestedPassword = digestEncodedPassword(encodedPsw, values); if (!values.get(this.passwordFieldName).equals(digestedPassword)) { throw new FailedLoginException("Password does not match value on record."); } return createHandlerResult(transformedCredential, this.principalFactory.createPrincipal(username), null); } catch (final IncorrectResultSizeDataAccessException e) { if (e.getActualSize() == 0) { throw new AccountNotFoundException(username + " not found with SQL query"); } else { throw new FailedLoginException("Multiple records found for " + username); } } catch (final DataAccessException e) { throw new PreventedException("SQL exception while executing query for " + username, e); } }
if (ex.getActualSize() != 0) { LOG.error("balance request sql returned more than one row, aborting", ex); throw ex;
if (ex.getActualSize() != 0) { LOG.error("balance request sql returned more than one row, aborting", ex); throw ex;