Search in sources :

Example 1 with User

use of org.baeldung.rolesauthorities.model.User in project tutorials by eugenp.

the class CustomAuthenticationProvider method authenticate.

@Override
public Authentication authenticate(Authentication auth) throws AuthenticationException {
    final User user = userRepository.findByEmail(auth.getName());
    if ((user == null)) {
        throw new BadCredentialsException("Invalid username or password");
    }
    final Authentication result = super.authenticate(auth);
    return new UsernamePasswordAuthenticationToken(user, result.getCredentials(), result.getAuthorities());
}
Also used : User(org.baeldung.rolesauthorities.model.User) Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException)

Example 2 with User

use of org.baeldung.rolesauthorities.model.User in project tutorials by eugenp.

the class SetupDataLoader method onApplicationEvent.

@Override
@Transactional
public void onApplicationEvent(ContextRefreshedEvent event) {
    if (alreadySetup) {
        return;
    }
    // == create initial privileges
    Privilege readPrivilege = createPrivilegeIfNotFound("READ_PRIVILEGE");
    Privilege writePrivilege = createPrivilegeIfNotFound("WRITE_PRIVILEGE");
    // == create initial roles
    List<Privilege> adminPrivileges = Arrays.asList(readPrivilege, writePrivilege);
    createRoleIfNotFound("ROLE_ADMIN", adminPrivileges);
    List<Privilege> rolePrivileges = new ArrayList<>();
    createRoleIfNotFound("ROLE_USER", rolePrivileges);
    Role adminRole = roleRepository.findByName("ROLE_ADMIN");
    User user = new User();
    user.setFirstName("Admin");
    user.setLastName("Admin");
    user.setEmail("admin@test.com");
    user.setPassword(passwordEncoder.encode("admin"));
    user.setRoles(Arrays.asList(adminRole));
    user.setEnabled(true);
    userRepository.save(user);
    Role basicRole = roleRepository.findByName("ROLE_USER");
    User basicUser = new User();
    basicUser.setFirstName("User");
    basicUser.setLastName("User");
    basicUser.setEmail("user@test.com");
    basicUser.setPassword(passwordEncoder.encode("user"));
    basicUser.setRoles(Arrays.asList(basicRole));
    basicUser.setEnabled(true);
    userRepository.save(basicUser);
    alreadySetup = true;
}
Also used : Role(org.baeldung.rolesauthorities.model.Role) User(org.baeldung.rolesauthorities.model.User) ArrayList(java.util.ArrayList) Privilege(org.baeldung.rolesauthorities.model.Privilege) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with User

use of org.baeldung.rolesauthorities.model.User in project tutorials by eugenp.

the class MyUserDetailsService method loadUserByUsername.

// API
@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
    try {
        User user = userRepository.findByEmail(email);
        if (user == null) {
            throw new UsernameNotFoundException("No user found with username: " + email);
        }
        org.springframework.security.core.userdetails.User userDetails = new org.springframework.security.core.userdetails.User(user.getEmail(), user.getPassword(), user.isEnabled(), true, true, true, getAuthorities(user.getRoles()));
        return userDetails;
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) User(org.baeldung.rolesauthorities.model.User) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException)

Aggregations

User (org.baeldung.rolesauthorities.model.User)3 ArrayList (java.util.ArrayList)1 Privilege (org.baeldung.rolesauthorities.model.Privilege)1 Role (org.baeldung.rolesauthorities.model.Role)1 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1 Authentication (org.springframework.security.core.Authentication)1 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)1 Transactional (org.springframework.transaction.annotation.Transactional)1