Guest User

Untitled

a guest
Aug 20th, 2018
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. package com.demo;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5. import org.springframework.security.authentication.*;
  6. import org.springframework.security.core.*;
  7. import org.springframework.security.core.authority.SimpleGrantedAuthority;
  8. import org.springframework.security.core.context.SecurityContextHolder;
  9. import org.springframework.security.core.userdetails.User;
  10. import org.springframework.security.core.userdetails.UserDetails;
  11. import org.springframework.security.core.userdetails.UserDetailsService;
  12. import org.springframework.security.provisioning.InMemoryUserDetailsManager;
  13.  
  14. public class AuthenticationSample {
  15. private static AuthenticationManager authenticationManager = new AuthenticationManagerImpl();
  16.  
  17. public static void main(String[] args) throws Exception
  18. {
  19. Scanner sc = new Scanner(System.in);
  20. System.out.println("Please enter Username: ");
  21. String name = sc.nextLine();
  22. System.out.println("Please enter Password: ");
  23. String password = sc.nextLine();
  24.  
  25. try {
  26. Authentication request = new UsernamePasswordAuthenticationToken(name, password);
  27. Authentication result = authenticationManager.authenticate(request);
  28. SecurityContextHolder.getContext().setAuthentication(result);
  29.  
  30. } catch (AuthenticationException e) {
  31. System.out.println("Authentication failed: " + e.getMessage());
  32. System.exit(1);
  33. }
  34.  
  35. System.out.println("Successfully authenticated. Security context contains: "
  36. + SecurityContextHolder.getContext().getAuthentication());
  37. }
  38. }
  39.  
  40. class AuthenticationManagerImpl implements AuthenticationManager {
  41.  
  42. public UserDetailsService userDetailsService() {
  43. InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
  44. manager.createUser(User.withUsername("ankidaemon").password("password").roles("ADMIN").build());
  45. manager.createUser(User.withUsername("test").password("password").roles("USER").build());
  46. return manager;
  47. }
  48.  
  49. public Authentication authenticate(Authentication auth) throws AuthenticationException {
  50.  
  51. UserDetails user=userDetailsService().loadUserByUsername(auth.getName());
  52. if(user !=null){
  53. if(user.getPassword().equals(auth.getCredentials())){
  54. if(user.getUsername().equals("ankidaemon")){
  55. return new UsernamePasswordAuthenticationToken(auth.getName(), auth.getCredentials(),
  56. new ArrayList<GrantedAuthority>(){
  57. {
  58. add(new SimpleGrantedAuthority("ROLE_ADMIN"));
  59. }
  60. });
  61. }
  62. else{
  63. System.out.println("User is"+user.getUsername());
  64. return new UsernamePasswordAuthenticationToken(auth.getName(), auth.getCredentials(),
  65. new ArrayList<GrantedAuthority>(){
  66. {
  67. add(new SimpleGrantedAuthority("ROLE_USER"));
  68. }
  69. });
  70. }
  71. }
  72. }
  73.  
  74. throw new BadCredentialsException("Bad Credentials");
  75.  
  76. }
  77. }
Add Comment
Please, Sign In to add comment