Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.lunapps.security.impl;
- import com.lunapps.exception.UserRegistrationException;
- import com.lunapps.models.Authority;
- import com.lunapps.models.User;
- import com.lunapps.models.UserRole;
- import com.lunapps.repository.UserRepository;
- import com.lunapps.security.Authentication;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.security.authentication.AuthenticationManager;
- import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
- import org.springframework.security.core.GrantedAuthority;
- import org.springframework.security.core.authority.SimpleGrantedAuthority;
- import org.springframework.security.core.context.SecurityContextHolder;
- import org.springframework.security.core.userdetails.UserDetails;
- import org.springframework.security.core.userdetails.UsernameNotFoundException;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import java.text.MessageFormat;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Objects;
- import java.util.Set;
- @Service
- public class UserDetailsServiceImpl implements Authentication {
- private static final String DOES_NOT_EXIST = "40401";
- private static final String HAS_TO_CONFIRM_REG = "40402";
- private static final String WRONG_PASSWORD = "40403";
- private static final String PASSWORDS_ARE_NOT_EQUALS = "40404";
- @Autowired
- private UserRepository userRepository;
- @Autowired
- private AuthenticationManager authenticationManager;
- @Override
- @Transactional(readOnly = true)
- public UserDetails loadUserByUsername(final String userEmail) throws UsernameNotFoundException {
- User user = userRepository.findByEmail(userEmail);
- if (Objects.isNull(user)) {
- throw new UserRegistrationException(MessageFormat.format("user with email: {0} does not exist", userEmail), DOES_NOT_EXIST);
- }
- if (user.getEnable() == Boolean.FALSE) {
- throw new UserRegistrationException(MessageFormat.format("user with email: {0} has to confirm registration", userEmail), HAS_TO_CONFIRM_REG);
- }
- Set<GrantedAuthority> grantedAuthorities = new HashSet<>();
- List<Authority> authorities = user.getAuthorities();
- for (Authority authority : authorities) {
- UserRole userRole = authority.getUserRole();
- grantedAuthorities.add(new SimpleGrantedAuthority(userRole.name()));
- }
- return new org.springframework.security.core.userdetails.User(user.getEmail(), user.getPassword(), grantedAuthorities);
- }
- // Perform the security
- @Override
- public void authCheck(String principal, String credentials) {
- UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(principal, credentials);
- final org.springframework.security.core.Authentication authentication = authenticationManager.authenticate(authenticationToken);
- SecurityContextHolder.getContext().setAuthentication(authentication);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement