Skip to content

Instantly share code, notes, and snippets.

@sergiomichels
Last active December 14, 2015 22:19
Show Gist options
  • Select an option

  • Save sergiomichels/5157749 to your computer and use it in GitHub Desktop.

Select an option

Save sergiomichels/5157749 to your computer and use it in GitHub Desktop.
Class that get's the username and password and try to open a database connection with this information
class UserDatabaseAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider {
def dataSource
@Override
protected void additionalAuthenticationChecks(UserDetails userDetails,
UsernamePasswordAuthenticationToken token) throws AuthenticationException {
}
@Override
protected UserDetails retrieveUser(String username,
UsernamePasswordAuthenticationToken token) throws AuthenticationException {
String password = (String) token.getCredentials();
if (!StringUtils.hasText(password)) {
throw new BadCredentialsException("Please enter password");
}
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
try {
Connection connection = dataSource.getConnection(username, password)
dataSource.setCredentialsForCurrentThread(username, password)
}catch(SQLException sql) {
throw new BadCredentialsException("Invalid username or password!");
}
return new User(username, password, true, true, true, true, authorities);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment