Guest User

Untitled

a guest
Apr 11th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. @Autowired
  2. private ApLoginCustomUserDetailsService userDetailsService;
  3.  
  4. @Autowired
  5. private ApLoginUserAuthenticationProvider userAuthenticationProvider;
  6.  
  7. @Override
  8. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  9. auth.ldapAuthentication().rolePrefix("")
  10. .contextSource()
  11. .url(ldapUrl)
  12. .managerDn(ldapDn)
  13. .managerPassword(ldapPassword)
  14. .and().userDnPatterns("uid{0}").userDetailsContextMapper(userDetailsMapper());
  15. auth.userDetailsService(userDetailsService);
  16. auth.authenticationProvider(userAuthenticationProvider);
  17. }
  18.  
  19. @Service
  20. public class LDAPUserDetailsMapper extends LdapUserDetailsMapper {
  21.  
  22. @Value("${ldap.user.firstName}")
  23. private String ldapFirstName;
  24.  
  25. @Value("${ldap.user.lastName}")
  26. private String ldapLastName;
  27.  
  28. @Value("${ldap.user.email}")
  29. private String ldapEmail;
  30.  
  31. @Autowired
  32. private LoginUserProfileRepository userProfileRepository;
  33.  
  34. @Autowired
  35. private LoginGroupsRepository groupRepository;
  36.  
  37. @Autowired
  38. private LoginCustomUserDetailsService userDetailsService;
  39.  
  40. @Override
  41. public UserDetails mapUserFromContext(DirContextOperations ctx, String username,
  42. Collection<? extends GrantedAuthority> authorities) {
  43.  
  44. final UserDetails originalUser = super.mapUserFromContext(ctx, username, authorities);
  45. System.out.println("Locked User "+originalUser.isAccountNonLocked());
  46. System.out.println("SIZE "+ctx.getAttributes().size());
  47. System.out.println("Attr "+ctx.getAttributes());
  48. String userName = originalUser.getUsername();
  49.  
  50. System.out.println(userName);
  51. ApLoginUserProfile userProfile = userProfileRepository.findByUsername(userName);
  52.  
  53. if (userProfile == null) {
  54.  
  55. String password = originalUser.getPassword();
  56. String firstName = ctx.getStringAttribute(ldapFirstName);
  57. String lastName = ctx.getStringAttribute(ldapLastName);
  58. String email = ctx.getStringAttribute(ldapEmail);
  59.  
  60. if (firstName.isEmpty() || lastName.isEmpty() || email.isEmpty()) {
  61. throw new EntityNotFoundException(ApLoginUserConstants.LDAP_USER_ATTRIBUTE_MISSING);
  62. }
  63.  
  64. ApLoginUserProfile newLdapUserProfile = new ApLoginUserProfile();
  65. newLdapUserProfile.setUsername(userName);
  66. newLdapUserProfile.setPassword(password);
  67. newLdapUserProfile.setFirstName(firstName);
  68. newLdapUserProfile.setLastName(lastName);
  69. newLdapUserProfile.setEmail(email);
  70. newLdapUserProfile.setPasswordResetRequired(Boolean.FALSE);
  71. newLdapUserProfile.setLoginSource(ApLoginUserConstants.LOGIN_SOURCE_LDAP);
  72. newLdapUserProfile.setAccountNonExpired(false);
  73. newLdapUserProfile.setAccountNonLocked(false);
  74. newLdapUserProfile.setCredentialNonExpired(false);
  75. System.out.println("Saved");
  76. if (userProfileRepository.save(newLdapUserProfile) == null) {
  77. throw new EntityNotFoundException(ApLoginUserConstants.LDAP_USER_CREATE_FAILED);
  78. }
  79. return userDetailsService.loadUserByUsername(userName);
  80. }
  81. return userDetailsService.loadUserByUsername(userName);
  82. }
  83.  
  84. @Service
  85. public class LoginCustomUserDetailsService implements UserDetailsService {
  86.  
  87. private final LoginUserProfileRepository userRepository;
  88.  
  89. @Autowired
  90. public LoginCustomUserDetailsService(LoginUserProfileRepository userRepository) {
  91. this.userRepository = userRepository;
  92. }
  93.  
  94. /**
  95. * Find user by username
  96. *
  97. * @param username(String)
  98. * @return userDetails
  99. */
  100. @Override
  101. public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
  102.  
  103. LoginUserProfile userProfile = userRepository.findByUsername(username);
  104.  
  105. if (userProfile == null) {
  106. // Not found
  107. throw new UsernameNotFoundException("User " + username + " not found.");
  108. }
  109.  
  110. if (userProfile.getGroups() == null) {
  111. // No Roles assigned to user...
  112. throw new UsernameNotFoundException("User not authorized.");
  113. }
  114.  
  115. Collection<? extends GrantedAuthority> grantedAuthorities = getAuthorities(userProfile.getGroups());
  116.  
  117. System.out.println("IN loadUserByUsername");
  118. System.out.println(userProfile.isCredentialNonExpired()+" "+userProfile.isAccountNonExpired()+" "+userProfile.isAccountNonLocked());
  119. System.out.println(grantedAuthorities);
  120. return new User(userProfile.getUsername(), userProfile.getPassword(), userProfile.isEnabled(), userProfile.isAccountNonExpired(),
  121. userProfile.isCredentialNonExpired(), userProfile.isAccountNonLocked(), grantedAuthorities);
  122. }
Add Comment
Please, Sign In to add comment