@Override
public Authentication authenticate(Authentication authentication) {
String username = (String) authentication.getPrincipal();
String password = (String) authentication.getCredentials();
User user = userDAO.login(username, password);
if (user == null) {
LOG.warn("Could not authenticate user with username: " + username);
throw new GlobalCronAuthenticationException("Authentication failed");
}
Collection<GrantedAuthority> auth = new ArrayList<GrantedAuthority>();
for (Role role : user.getRoles()) {
auth.add(new Authority(role.getName()));
}
user.registerLogin();
userDAO.upsert(user);
AuditLog auditLog = new AuditLog();
auditLog.setMessage("User logged in");
auditLog.setUser(user.getUsername());
auditLog.setType(AuditLogType.LOGIN);
auditLogDAO.upsert(auditLog);
LOG.info(format("User %s successfully logged in with roles [%s]", username, auth));
return new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword(), auth);
}