简体   繁体   中英

Jhipster login with email instead of username

I create app using Jhipster. By default it use combination of username+password to login. I would like to create email+password login so i can make username not unique. What is the best way to do this ?

I am using JWT.

By default the login field is used to log the user into the application. But with the last JHipster version (I'm not sure since which version it was implemented to) you can log user by email.

In the DomainUserDetailsService.java :

    public UserDetails loadUserByUsername(final String login) {
    log.debug("Authenticating {}", login);

    if (new EmailValidator().isValid(login, null)) {
        return userRepository.findOneWithAuthoritiesByEmail(login)
            .map(user -> createSpringSecurityUser(login, user))
            .orElseThrow(() -> new UsernameNotFoundException("User with email " + login + " was not found in the database"));
    }

    String lowercaseLogin = login.toLowerCase(Locale.ENGLISH);
    return userRepository.findOneWithAuthoritiesByLogin(lowercaseLogin)
        .map(user -> createSpringSecurityUser(lowercaseLogin, user))
        .orElseThrow(() -> new UsernameNotFoundException("User " + lowercaseLogin + " was not found in the database"));

}

But the login is still unique, as the mail is, firstname and lastname are not. Even if you have an old version of JHipster i'm pretty sure you can add this code to support both email and login authentication.

tested with JWT and last JHipster version !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM