Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. public class CustomAuthentication implements AuthenticationProvider {
  2.  
  3. @Autowired
  4. private UserRepository userRepository;
  5.  
  6.  
  7.  
  8. @Override
  9. public Authentication authenticate(Authentication authentication) throws AuthenticationException {
  10. String userLogin = authentication.getName();
  11. String password = authentication.getCredentials().toString();
  12.  
  13. User user = userRepository.findOneByLogin(userLogin).orElse(null);
  14. if (userLogin.equalsIgnoreCase("test") && password.equals("test")) { // replace your custom code here for custom authentication
  15. return new UsernamePasswordAuthenticationToken
  16. (userLogin, password, Collections.emptyList());
  17. } else {
  18. throw new BadCredentialsException("External system authentication failed");
  19. }
  20. }
  21.  
  22. @Override
  23. public boolean supports(Class<?> authentication) {
  24. return true;
  25. }
  26. }
  27.  
  28. @Repository("userRepository")
  29. public interface UserRepository extends CrudRepository<User, Long> {
  30. @Query(value = "SELECT * FROM user u WHERE u.login = ? ", nativeQuery = true)
  31. Optional<User> findOneByLogin(String login);
  32. Optional<User> findOneById(long id);
  33. List<User> findAll();
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement