简体   繁体   中英

Entity sign in jhipster

I create a new project jhipster using angular 2 , mongodb . I create an entity called doctor with attributes name (string) , login(string) , password (string) and specialty (string) . I want sign in into my application using this the login and the password of this entity,is that possible ?

there are 2 ways to go.

the first way is about to solve what you are actually asking for: a additional user management beside the classical (as far as I understand, you didn't say you don't want to remove login for normal users). It's not the ideal model but sometimes there are use cases for that. However in Spring, all that magic should happen in UserDetailsService (an interface from spring-security) which you implement. JHipster does this for the classical scenario in "src/main/java/package/security/DomainUserDetailsService.java". For your case, you should have a DoctorRepository like this:

@SuppressWarnings("unused")
public interface DoctorRepository extends MongoRepository<Doctor,Long> {
  //...

  Optional<Doctor> fineOneByLogin(@Param String login);

  //...
}

so you can rebuild the loadByUsername to

@Transactional
public UserDetails loadUserByUsername(final String login) {
    log.debug("Authenticating {}", login);
    String lowercaseLogin = login.toLowerCase(Locale.ENGLISH);
    Optional<User> userFromDatabase = userRepository.findOneWithAuthoritiesByLogin(lowercaseLogin);
    return userFromDatabase.map(user -> {
        if (!user.getActivated()) {
            throw new UserNotActivatedException("User " + lowercaseLogin + " was not activated");
        }
        List<GrantedAuthority> grantedAuthorities = user.getAuthorities().stream()
                .map(authority -> new SimpleGrantedAuthority(authority.getName()))
            .collect(Collectors.toList());
        return new org.springframework.security.core.userdetails.User(lowercaseLogin,
            user.getPassword(),
            grantedAuthorities);
    }).orElseGet(() -> {
        List<GrantedAuthority> grantedAuthorities = null; // add your logic for that here
        return doctorRepository.findOneByLogin(String login).map(doctor -> {
            return new org.springframework.security.core.userdetails.User(lowercaseLogin,
                doctor.getPassword(),
                grantedAuthorities);

        }).orElseThrow(() -> new UsernameNotFoundException("User " + lowercaseLogin + " was not found in the " +
    "database"));
    });
}

(source code written from mind, not tested)

keep in mind you must store the passwords according the configured PasswordEncoder

the better way

While mongo has no real relationship management, there still are patterns to model the fact, that a user might be a doctor (as a nested attribute for example), and therefore add doctor features, based on this fact.

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