Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Autowired
- private ApLoginCustomUserDetailsService userDetailsService;
- @Autowired
- private ApLoginUserAuthenticationProvider userAuthenticationProvider;
- @Override
- protected void configure(AuthenticationManagerBuilder auth) throws Exception {
- auth.ldapAuthentication().rolePrefix("")
- .contextSource()
- .url(ldapUrl)
- .managerDn(ldapDn)
- .managerPassword(ldapPassword)
- .and().userDnPatterns("uid{0}").userDetailsContextMapper(userDetailsMapper());
- auth.userDetailsService(userDetailsService);
- auth.authenticationProvider(userAuthenticationProvider);
- }
- @Service
- public class LDAPUserDetailsMapper extends LdapUserDetailsMapper {
- @Value("${ldap.user.firstName}")
- private String ldapFirstName;
- @Value("${ldap.user.lastName}")
- private String ldapLastName;
- @Value("${ldap.user.email}")
- private String ldapEmail;
- @Autowired
- private LoginUserProfileRepository userProfileRepository;
- @Autowired
- private LoginGroupsRepository groupRepository;
- @Autowired
- private LoginCustomUserDetailsService userDetailsService;
- @Override
- public UserDetails mapUserFromContext(DirContextOperations ctx, String username,
- Collection<? extends GrantedAuthority> authorities) {
- final UserDetails originalUser = super.mapUserFromContext(ctx, username, authorities);
- System.out.println("Locked User "+originalUser.isAccountNonLocked());
- System.out.println("SIZE "+ctx.getAttributes().size());
- System.out.println("Attr "+ctx.getAttributes());
- String userName = originalUser.getUsername();
- System.out.println(userName);
- ApLoginUserProfile userProfile = userProfileRepository.findByUsername(userName);
- if (userProfile == null) {
- String password = originalUser.getPassword();
- String firstName = ctx.getStringAttribute(ldapFirstName);
- String lastName = ctx.getStringAttribute(ldapLastName);
- String email = ctx.getStringAttribute(ldapEmail);
- if (firstName.isEmpty() || lastName.isEmpty() || email.isEmpty()) {
- throw new EntityNotFoundException(ApLoginUserConstants.LDAP_USER_ATTRIBUTE_MISSING);
- }
- ApLoginUserProfile newLdapUserProfile = new ApLoginUserProfile();
- newLdapUserProfile.setUsername(userName);
- newLdapUserProfile.setPassword(password);
- newLdapUserProfile.setFirstName(firstName);
- newLdapUserProfile.setLastName(lastName);
- newLdapUserProfile.setEmail(email);
- newLdapUserProfile.setPasswordResetRequired(Boolean.FALSE);
- newLdapUserProfile.setLoginSource(ApLoginUserConstants.LOGIN_SOURCE_LDAP);
- newLdapUserProfile.setAccountNonExpired(false);
- newLdapUserProfile.setAccountNonLocked(false);
- newLdapUserProfile.setCredentialNonExpired(false);
- System.out.println("Saved");
- if (userProfileRepository.save(newLdapUserProfile) == null) {
- throw new EntityNotFoundException(ApLoginUserConstants.LDAP_USER_CREATE_FAILED);
- }
- return userDetailsService.loadUserByUsername(userName);
- }
- return userDetailsService.loadUserByUsername(userName);
- }
- @Service
- public class LoginCustomUserDetailsService implements UserDetailsService {
- private final LoginUserProfileRepository userRepository;
- @Autowired
- public LoginCustomUserDetailsService(LoginUserProfileRepository userRepository) {
- this.userRepository = userRepository;
- }
- /**
- * Find user by username
- *
- * @param username(String)
- * @return userDetails
- */
- @Override
- public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
- LoginUserProfile userProfile = userRepository.findByUsername(username);
- if (userProfile == null) {
- // Not found
- throw new UsernameNotFoundException("User " + username + " not found.");
- }
- if (userProfile.getGroups() == null) {
- // No Roles assigned to user...
- throw new UsernameNotFoundException("User not authorized.");
- }
- Collection<? extends GrantedAuthority> grantedAuthorities = getAuthorities(userProfile.getGroups());
- System.out.println("IN loadUserByUsername");
- System.out.println(userProfile.isCredentialNonExpired()+" "+userProfile.isAccountNonExpired()+" "+userProfile.isAccountNonLocked());
- System.out.println(grantedAuthorities);
- return new User(userProfile.getUsername(), userProfile.getPassword(), userProfile.isEnabled(), userProfile.isAccountNonExpired(),
- userProfile.isCredentialNonExpired(), userProfile.isAccountNonLocked(), grantedAuthorities);
- }
Add Comment
Please, Sign In to add comment