Advertisement
Guest User

Untitled

a guest
Oct 5th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. package com.lunapps.security.impl;
  2.  
  3. import com.lunapps.exception.UserRegistrationException;
  4. import com.lunapps.models.Authority;
  5. import com.lunapps.models.User;
  6. import com.lunapps.models.UserRole;
  7. import com.lunapps.repository.UserRepository;
  8. import com.lunapps.security.Authentication;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.security.authentication.AuthenticationManager;
  11. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  12. import org.springframework.security.core.GrantedAuthority;
  13. import org.springframework.security.core.authority.SimpleGrantedAuthority;
  14. import org.springframework.security.core.context.SecurityContextHolder;
  15. import org.springframework.security.core.userdetails.UserDetails;
  16. import org.springframework.security.core.userdetails.UsernameNotFoundException;
  17. import org.springframework.stereotype.Service;
  18. import org.springframework.transaction.annotation.Transactional;
  19.  
  20. import java.text.MessageFormat;
  21. import java.util.HashSet;
  22. import java.util.List;
  23. import java.util.Objects;
  24. import java.util.Set;
  25.  
  26. @Service
  27. public class UserDetailsServiceImpl implements Authentication {
  28. private static final String DOES_NOT_EXIST = "40401";
  29. private static final String HAS_TO_CONFIRM_REG = "40402";
  30. private static final String WRONG_PASSWORD = "40403";
  31. private static final String PASSWORDS_ARE_NOT_EQUALS = "40404";
  32.  
  33. @Autowired
  34. private UserRepository userRepository;
  35.  
  36. @Autowired
  37. private AuthenticationManager authenticationManager;
  38.  
  39. @Override
  40. @Transactional(readOnly = true)
  41. public UserDetails loadUserByUsername(final String userEmail) throws UsernameNotFoundException {
  42. User user = userRepository.findByEmail(userEmail);
  43. if (Objects.isNull(user)) {
  44. throw new UserRegistrationException(MessageFormat.format("user with email: {0} does not exist", userEmail), DOES_NOT_EXIST);
  45. }
  46.  
  47. if (user.getEnable() == Boolean.FALSE) {
  48. throw new UserRegistrationException(MessageFormat.format("user with email: {0} has to confirm registration", userEmail), HAS_TO_CONFIRM_REG);
  49. }
  50.  
  51. Set<GrantedAuthority> grantedAuthorities = new HashSet<>();
  52.  
  53. List<Authority> authorities = user.getAuthorities();
  54.  
  55. for (Authority authority : authorities) {
  56. UserRole userRole = authority.getUserRole();
  57. grantedAuthorities.add(new SimpleGrantedAuthority(userRole.name()));
  58. }
  59. return new org.springframework.security.core.userdetails.User(user.getEmail(), user.getPassword(), grantedAuthorities);
  60. }
  61.  
  62. // Perform the security
  63. @Override
  64. public void authCheck(String principal, String credentials) {
  65. UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(principal, credentials);
  66. final org.springframework.security.core.Authentication authentication = authenticationManager.authenticate(authenticationToken);
  67. SecurityContextHolder.getContext().setAuthentication(authentication);
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement