使用电子邮件而不是用户名登录Jhipster

使用电子邮件而不是用户名登录Jhipster

问题描述:

我使用Jhipster创建应用.默认情况下,它使用用户名和密码的组合来登录.我想创建电子邮件和密码登录名,以便我可以使用户名不唯一.最好的方法是什么?

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 ?

我正在使用JWT.

默认情况下,登录字段用于将用户登录到应用程序中.但是使用JHipster的最新版本(我不确定它是从哪个版本实施的),您可以通过电子邮件登录用户.

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.

在DomainUserDetailsS​​ervice.java中:

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"));

}

但是登录名仍然是唯一的,因为邮件是,名和姓不是.即使您使用的是旧版本的JHipster,我也可以肯定您可以添加此代码以支持电子邮件和登录身份验证.

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.

通过JWT和最新的JHipster版本进行了测试!

tested with JWT and last JHipster version !