@Autowired
private AuthenticationEditorModel authenticationEditorModel;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
AuthUser authUser = null;
boolean noDbConn = false;
try {
authUser = this.authenticationEditorModel.getUser(username);
} catch (TransactionException ex) {
logger.warn(String.format("Couldn't get authentication info for user %s from DB", username), ex);
noDbConn = true;
}
if (authUser == null) {
if (this.unknownUserHandler != null) {
// Check whether the user should be added
NewUserInfo newUserInfo = this.unknownUserHandler.handleUser(username);
if (newUserInfo != null) {
if (!noDbConn) {
// Create the user on the DB
authUser = this.authenticationEditorModel.addUser(username, newUserInfo.getPassword(), newUserInfo.getAuthorities());
} else {
// No DB connectivity, so just return user as defined by UnknownUserHandler
newUserInfo.getAuthorities();
List<GrantedAuthority> grantedAuths = new ArrayList<>();
for (String role : newUserInfo.getAuthorities()) {
grantedAuths.add(new SimpleGrantedAuthority(role));
}
return new User(username, newUserInfo.getPassword(), grantedAuths);
}
}
}
if (authUser == null) {
throw new UsernameNotFoundException("No such user: " + username);
}
} else {
this.authenticationEditorModel.updateLoginCount(authUser);
}
List<GrantedAuthority> grantedAuths = new ArrayList<>();
for (AuthRole authRole : authUser.getRoles()) {
grantedAuths.add(new SimpleGrantedAuthority(authRole.getName()));
}
User user = new User(username, authUser.getPassword(), grantedAuths);
return user;
}