org.springframework.security.ldap.userdetails.LdapUserDetailsImpl Java Examples

The following examples show how to use org.springframework.security.ldap.userdetails.LdapUserDetailsImpl. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AutorizzazioneUtils.java    From govpay with GNU General Public License v3.0 6 votes vote down vote up
public static GovpayLdapUserDetails getUserDetail(String username, String password, String identificativo, List<GrantedAuthority> authorities) {
	GovpayLdapUserDetails details = new GovpayLdapUserDetails();

	LdapUserDetailsImpl.Essence essence = new LdapUserDetailsImpl.Essence();
	essence.setAccountNonExpired(true);
	essence.setAccountNonLocked(true);
	essence.setCredentialsNonExpired(true);
	essence.setEnabled(true);
	essence.setUsername(username);
	essence.setPassword(password);
	essence.setAuthorities(authorities);
	essence.setDn(identificativo);

	details.setLdapUserDetailsImpl(essence.createUserDetails());

	return details;
}
 
Example #2
Source File: AutorizzazioneUtils.java    From govpay with GNU General Public License v3.0 6 votes vote down vote up
public static GovpayLdapUserDetails getUserDetail(GovpayLdapUserDetails base, List<GrantedAuthority> authorities) {
	GovpayLdapUserDetails details = new GovpayLdapUserDetails();

	LdapUserDetailsImpl.Essence essence = new LdapUserDetailsImpl.Essence();
	essence.setAccountNonExpired(base.isAccountNonExpired());
	essence.setAccountNonLocked(base.isAccountNonLocked());
	essence.setCredentialsNonExpired(base.isCredentialsNonExpired());
	essence.setEnabled(base.isEnabled());
	essence.setUsername(base.getUsername());
	essence.setPassword(base.getPassword());
	essence.setAuthorities(authorities);
	essence.setDn(base.getIdentificativo());

	details.setLdapUserDetailsImpl(essence.createUserDetails());

	return details;
}
 
Example #3
Source File: SAMLUserDetailsService.java    From kylin with Apache License 2.0 6 votes vote down vote up
@Override
public Object loadUserBySAML(SAMLCredential samlCredential) throws UsernameNotFoundException {
    final String userEmail = samlCredential.getAttributeAsString("email");
    logger.debug("samlCredential.email:" + userEmail);
    final String userName = userEmail.substring(0, userEmail.indexOf("@"));

    UserDetails userDetails = null;
    try {
        userDetails = ldapUserDetailsService.loadUserByUsername(userName);
        if (userDetails instanceof LdapUserDetailsImpl) {
            LdapUserDetailsImpl.Essence essence = new LdapUserDetailsImpl.Essence();
            essence.setDn(((LdapUserDetailsImpl) userDetails).getDn());
            essence.setUsername(userEmail);
            essence.setPassword(userDetails.getPassword());
            essence.setAuthorities(userDetails.getAuthorities());
            essence.setTimeBeforeExpiration(((LdapUserDetailsImpl) userDetails).getTimeBeforeExpiration());
            essence.setGraceLoginsRemaining(((LdapUserDetailsImpl) userDetails).getGraceLoginsRemaining());
            userDetails = essence.createUserDetails();
        }
    } catch (org.springframework.security.core.userdetails.UsernameNotFoundException e) {
        logger.error("User not found in LDAP, check whether he/she has been added to the groups.", e);
    }
    logger.debug("userDeail by search ldap with '" + userName + "' is: " + userDetails);
    return userDetails;
}
 
Example #4
Source File: IdentityUtils.java    From influx-proxy with Apache License 2.0 5 votes vote down vote up
public static String getUserNameDN() {
    Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    if (principal instanceof LdapUserDetailsImpl) {
        List<String> name = Arrays.asList(((LdapUserDetailsImpl) principal).getDn().split(",")).stream().map(s -> s.split("=")[1]).limit(2).collect(Collectors.toList());
        return name.size()==1?name.get(0):String.format("%s(%s)", name.get(0),name.get(1));
    }
    return getUserName();
}
 
Example #5
Source File: SecurityUtils.java    From spring-tsers-auth with Apache License 2.0 5 votes vote down vote up
public User getCurrentUser() {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    Authentication authentication = securityContext.getAuthentication();
    if (authentication != null) {
        if (authentication.getPrincipal() instanceof org.springframework.security.core.userdetails.User) {
            return (User) authentication.getPrincipal();
        } else if (authentication.getPrincipal() instanceof LdapUserDetailsImpl) {
            String dn = ((LdapUserDetailsImpl) authentication.getPrincipal()).getDn();
            return new User(dn, "", Arrays.asList());
        }
    }
    throw new IllegalStateException("User not found!");
}
 
Example #6
Source File: CustomUserDetailsContextMapper.java    From airsonic-advanced with GNU General Public License v3.0 4 votes vote down vote up
@Override
public UserDetails mapUserFromContext(DirContextOperations ctx, String username,
                                      Collection<? extends GrantedAuthority> authorities) {
    String dn = ctx.getNameInNamespace();

    LOG.debug("Mapping user details from context with DN: " + dn);

    // User must be defined in Airsonic, unless auto-shadowing is enabled.
    User user = securityService.getUserByName(username, false);
    if (user == null && !settingsService.isLdapAutoShadowing()) {
        throw new BadCredentialsException("User does not exist.");
    }

    if (user == null) {
        User newUser = new User(username, null, true, 0L, 0L, 0L, Set.of(Role.STREAM, Role.SETTINGS));
        securityService.createUser(newUser, "", "Autogenerated for new LDAP user");
        LOG.info("Created local user '{}' for DN {}", username, dn);
        user = securityService.getUserByName(username, false);
    }

    // LDAP authentication must be enabled for the given user.
    if (!user.isLdapAuthenticated()) {
        throw new BadCredentialsException("LDAP authentication disabled for user.");
    }

    LdapUserDetailsImpl.Essence essence = new LdapUserDetailsImpl.Essence();
    essence.setDn(dn);

    Object passwordValue = ctx.getObjectAttribute(passwordAttributeName);

    if (passwordValue != null) {
        essence.setPassword(mapPassword(passwordValue));
    }

    essence.setUsername(user.getUsername());

    // Add the supplied authorities
    for (GrantedAuthority authority : securityService.getGrantedAuthorities(user)) {
        essence.addAuthority(authority);
    }

    // Check for PPolicy data

    PasswordPolicyResponseControl ppolicy = (PasswordPolicyResponseControl) ctx
            .getObjectAttribute(PasswordPolicyControl.OID);

    if (ppolicy != null) {
        essence.setTimeBeforeExpiration(ppolicy.getTimeBeforeExpiration());
        essence.setGraceLoginsRemaining(ppolicy.getGraceLoginsRemaining());
    }

    return essence.createUserDetails();

}
 
Example #7
Source File: CustomUserDetailsContextMapper.java    From airsonic with GNU General Public License v3.0 4 votes vote down vote up
public UserDetails mapUserFromContext(DirContextOperations ctx, String username,
                                      Collection<? extends GrantedAuthority> authorities) {
    String dn = ctx.getNameInNamespace();

    LOG.debug("Mapping user details from context with DN: " + dn);

    // User must be defined in Airsonic, unless auto-shadowing is enabled.
    User user = securityService.getUserByName(username, false);
    if (user == null && !settingsService.isLdapAutoShadowing()) {
        throw new BadCredentialsException("User does not exist.");
    }

    if (user == null) {
        User newUser = new User(username, "", null, true, 0L, 0L, 0L);
        newUser.setStreamRole(true);
        newUser.setSettingsRole(true);
        securityService.createUser(newUser);
        LOG.info("Created local user '" + username + "' for DN " + dn);
        user = securityService.getUserByName(username, false);
    }

    // LDAP authentication must be enabled for the given user.
    if (!user.isLdapAuthenticated()) {
        throw new BadCredentialsException("LDAP authentication disabled for user.");
    }

    LdapUserDetailsImpl.Essence essence = new LdapUserDetailsImpl.Essence();
    essence.setDn(dn);

    Object passwordValue = ctx.getObjectAttribute(passwordAttributeName);

    if (passwordValue != null) {
        essence.setPassword(mapPassword(passwordValue));
    }

    essence.setUsername(user.getUsername());

    // Add the supplied authorities
    for (GrantedAuthority authority : securityService.getGrantedAuthorities(user.getUsername())) {
        essence.addAuthority(authority);
    }

    // Check for PPolicy data

    PasswordPolicyResponseControl ppolicy = (PasswordPolicyResponseControl) ctx
            .getObjectAttribute(PasswordPolicyControl.OID);

    if (ppolicy != null) {
        essence.setTimeBeforeExpiration(ppolicy.getTimeBeforeExpiration());
        essence.setGraceLoginsRemaining(ppolicy.getGraceLoginsRemaining());
    }

    return essence.createUserDetails();

}