Created
August 14, 2011 13:09
-
-
Save oravecz/1144863 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RESTUserDetailsService implements UserDetailsService { | |
private static final Logger LOG = LoggerFactory.getLogger(RESTUserDetailsService.class); | |
private Client _esClient; | |
public RESTUserDetailsService() { | |
} | |
public void destroy() { | |
} | |
/** | |
* Locates the user based on the username. In the actual implementation, the search may possibly | |
* be case insensitive, or case insensitive depending on how the implementation instance is | |
* configured. In this case, the <code>UserDetails</code> object that comes back may have a | |
* username that is of a different case than what was actually requested.. | |
* | |
* @param username the username identifying the user whose data is required. | |
* @return a fully populated user record (never <code>null</code>) | |
* @throws org.springframework.security.core.userdetails.UsernameNotFoundException | |
* if the user could not be found or the user has no GrantedAuthority | |
* @throws org.springframework.dao.DataAccessException | |
* if user could not be found for a repository-specific reason | |
*/ | |
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException { | |
String index = IndexThreadLocal.getIndex(); | |
String[] types = {"profiles"}; | |
LOG.debug(String.format("Authenticating credentials for user: %s, in index: %s", username, index)); | |
try { | |
final SearchRequest searchRequest = searchRequest(index).types(types).source( | |
searchSource().query( | |
fieldQuery("username", username) | |
) | |
); | |
final SearchResponse searchResponse = _esClient.search(searchRequest).actionGet(); | |
final SearchHit[] searchHits = searchResponse.getHits().hits(); | |
if (searchHits.length == 0) { | |
LOG.warn(String.format("ES Search returned no matches for username '%s'", username)); | |
throw new UsernameNotFoundException("No record found."); | |
} | |
if (searchHits.length > 1) { | |
LOG.warn(String.format("ES Search returned improper number of record (%d) for username lookup (%s)", searchHits.length, username)); | |
throw new UsernameNotFoundException("Bad Credentials"); | |
} | |
final Object json = JSON.parse(searchHits[0].sourceAsString()); | |
if (json instanceof Map) { | |
LOG.debug(String.format("User %s record was located.", username)); | |
final Map user = (Map) json; | |
LOG.debug("PasswordHash: " + user.get("password")); | |
return new RoundTableUser(user); | |
} | |
throw new UsernameNotFoundException("Authentication Failed"); | |
} catch (Exception e) { | |
LOG.warn("Unexpected error while getting user details for " + username, e); | |
throw new DataRetrievalFailureException( | |
"Unexpected error while getting user details for " + username, e); | |
} | |
} | |
public void setEsClient(Client esClient) { | |
_esClient = esClient; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment