Jhipster login with email instead of username

孤街醉人 提交于 2020-03-03 12:34:08

问题


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.


回答1:


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 !



来源:https://stackoverflow.com/questions/52918317/jhipster-login-with-email-instead-of-username

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!