Advertisement
Guest User

Untitled

a guest
Aug 31st, 2015
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. package com.websopti.wotms.auth;
  2.  
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.Collection;
  6.  
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.security.authentication.AuthenticationProvider;
  9. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  10. import org.springframework.security.core.Authentication;
  11. import org.springframework.security.core.AuthenticationException;
  12. import org.springframework.security.core.GrantedAuthority;
  13. import org.springframework.security.core.authority.SimpleGrantedAuthority;
  14. import org.springframework.security.core.userdetails.UsernameNotFoundException;
  15. import org.springframework.stereotype.Component;
  16.  
  17. import com.websopti.wotms.entity.User;
  18. import com.websopti.wotms.enums.ErrorKey;
  19. import com.websopti.wotms.service.UserService;
  20.  
  21. @Component
  22. public class CustomAuthenticationProvider implements AuthenticationProvider {
  23.  
  24. @Autowired
  25. public UserService userService;
  26.  
  27. @Override
  28. public Authentication authenticate(Authentication authentication) throws AuthenticationException {
  29.  
  30. String email = authentication.getPrincipal().toString();
  31. String password = authentication.getCredentials().toString();
  32.  
  33. Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
  34.  
  35. String username = null;
  36.  
  37. try {
  38. username = userService.authenticate(email, password);
  39. } catch (IOException e) {
  40. e.printStackTrace();
  41. }
  42.  
  43. if(username != null && !username.equals("0")){
  44.  
  45. User user = userService.findByEmail(email);
  46.  
  47. if (user != null) {
  48.  
  49. authorities.add(new SimpleGrantedAuthority(user.getRole().name()));
  50. return (new UsernamePasswordAuthenticationToken(user, null, authorities));
  51.  
  52. } else {
  53.  
  54. User newUser = new User();
  55. newUser.setName(username);
  56. newUser.setEmail(email);
  57. newUser.setPassword(password);
  58.  
  59. userService.register(newUser);
  60.  
  61. newUser = userService.findById(newUser.getId());
  62. authorities.add(new SimpleGrantedAuthority(newUser.getRole().name()));
  63.  
  64. return (new UsernamePasswordAuthenticationToken(newUser, null, authorities));
  65. }
  66.  
  67. } else {
  68.  
  69. throw new UsernameNotFoundException(ErrorKey.USER_NOT_FOUND.name());
  70.  
  71. }
  72.  
  73.  
  74. }
  75.  
  76. @Override
  77. public boolean supports(Class<?> authentication) {
  78.  
  79. return authentication.equals(UsernamePasswordAuthenticationToken.class);
  80. }
  81.  
  82. }
  83.  
  84. package com.websopti.wotms.auth;
  85.  
  86. import org.springframework.beans.factory.annotation.Autowired;
  87. import org.springframework.security.core.userdetails.UserDetails;
  88. import org.springframework.security.core.userdetails.UserDetailsService;
  89. import org.springframework.security.core.userdetails.UsernameNotFoundException;
  90. import org.springframework.stereotype.Service;
  91.  
  92. import com.websopti.wotms.entity.User;
  93. import com.websopti.wotms.enums.ErrorKey;
  94. import com.websopti.wotms.service.UserService;
  95.  
  96. @Service
  97. public class AuthenticationService implements UserDetailsService {
  98.  
  99. @Autowired
  100. public UserService userService;
  101.  
  102. @Override
  103. public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
  104.  
  105. User user = userService.findByEmail(email);
  106.  
  107. if(user != null)
  108. return user;
  109. else
  110. throw new UsernameNotFoundException(ErrorKey.USER_NOT_FOUND.name());
  111. }
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement