Advertisement
Guest User

Untitled

a guest
Apr 16th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. package com.ag04.clidemo.command;
  2.  
  3. import com.ag04.clidemo.shell.InputReader;
  4. import com.ag04.clidemo.shell.ShellHelper;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.context.annotation.Lazy;
  7. import org.springframework.security.authentication.AuthenticationManager;
  8. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  9. import org.springframework.security.core.Authentication;
  10. import org.springframework.security.core.AuthenticationException;
  11. import org.springframework.security.core.context.SecurityContextHolder;
  12. import org.springframework.shell.standard.ShellComponent;
  13. import org.springframework.shell.standard.ShellMethod;
  14. import org.springframework.util.StringUtils;
  15.  
  16. @ShellComponent
  17. public class SigninCommand extends SecuredCommand {
  18.  
  19. @Lazy
  20. @Autowired
  21. ShellHelper shellHelper;
  22.  
  23. @Lazy
  24. @Autowired
  25. InputReader inputReader;
  26.  
  27. @Autowired
  28. AuthenticationManager authenticationManager;
  29.  
  30. @ShellMethod("Sign in as clidemo user")
  31. public void signin() {
  32. String username;
  33. boolean usernameInvalid = true;
  34. do {
  35. username = inputReader.prompt("Please enter your username");
  36. if (StringUtils.hasText(username)) {
  37. usernameInvalid = false;
  38. } else {
  39. shellHelper.printWarning("Username can not be empty string!");
  40. }
  41. } while (usernameInvalid);
  42. String password = inputReader.prompt("Please enter your password", null, false);
  43. Authentication request = new UsernamePasswordAuthenticationToken(username, password);
  44.  
  45. try {
  46. Authentication result = authenticationManager.authenticate(request);
  47. SecurityContextHolder.getContext().setAuthentication(result);
  48. shellHelper.printSuccess("Credentials successfully authenticated! " + username + " -> welcome to CliDemo.");
  49. } catch (AuthenticationException e) {
  50. shellHelper.printWarning("Authentication failed: " + e.getMessage());
  51. }
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement