How to login user programatically by username?

Hi, i need to be able to programatically login user based on username. How can i obtain Credentials for specific username?


Take a look at ExternalUserCredentials. It uses username without password to log in. For instance:

public class AppStartedEventListener implements ApplicationListener<AppStartedEvent> {

    private static final String PROMO_USER_COOKIE = "PROMO_USER";

    private Logger log;

    public void onApplicationEvent(AppStartedEvent event) {
        String promoUserLogin = event.getApp().getCookieValue(PROMO_USER_COOKIE);
        if (promoUserLogin != null) {
            Connection connection = event.getApp().getConnection();
            if (!connection.isAuthenticated()) {
                try {
                    connection.login(new ExternalUserCredentials(promoUserLogin));
                } catch (LoginException e) {
                    log.warn("Unable to login promo user {}: {}", promoUserLogin, e.getMessage());
                } finally {

You can find a couple more examples of ExternalUserCredentials here: How to create a custom authentication? - #3 от пользователя Pinyazhin - CUBA.Platform