Hi, i need to be able to programatically login user based on username. How can i obtain Credentials for specific username?
Hello,
Take a look at ExternalUserCredentials. It uses username without password to log in. For instance:
@Order(10)
@Component
public class AppStartedEventListener implements ApplicationListener<AppStartedEvent> {
private static final String PROMO_USER_COOKIE = "PROMO_USER";
@Inject
private Logger log;
@Override
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 {
event.getApp().removeCookie(PROMO_USER_COOKIE);
}
}
}
}
}
You can find a couple more examples of ExternalUserCredentials here: How to create a custom authentication? - #3 от пользователя Pinyazhin - CUBA.Platform