/** * Get the principals of the logged in user, in this case the distinguished * name. * * @return the distinguished name of the logged in user. */ public String getPrincipal() { Authentication authentication = SecurityContextHolder.getContext() .getAuthentication(); if (authentication != null) { Object principal = authentication.getPrincipal(); if (!(principal instanceof LdapUserDetails)) { throw new IllegalArgumentException( "The principal property of the authentication object -" + "needs to be a LdapUserDetails."); } else { LdapUserDetails details = (LdapUserDetails) principal; return details.getDn(); } } else { log.warn("No Authentication object set in SecurityContext - " + "returning empty String as Principal"); return ""; } }
/** * Obtains the authorities for the user who's directory entry is represented by * the supplied LdapUserDetails object. * * @param userDetails the user who's authorities are required * @return the set of roles granted to the user. */ public final GrantedAuthority[] getGrantedAuthorities(LdapUserDetails userDetails) { String userDn = userDetails.getDn(); if (logger.isDebugEnabled()) { logger.debug("Getting authorities for user " + userDn); } Set roles = getGroupMembershipRoles(userDn, userDetails.getUsername()); // Temporary use of deprecated method Set oldGroupRoles = getGroupMembershipRoles(userDn, userDetails.getAttributes()); if (oldGroupRoles != null) { roles.addAll(oldGroupRoles); } Set extraRoles = getAdditionalRoles(userDetails); if (extraRoles != null) { roles.addAll(extraRoles); } if (defaultRole != null) { roles.add(defaultRole); } return (GrantedAuthority[]) roles.toArray(new GrantedAuthority[roles.size()]); }
/** * Get the principals of the logged in user, in this case the distinguished * name. * * @return the distinguished name of the logged in user. */ public String getPrincipal() { Authentication authentication = SecurityContextHolder.getContext() .getAuthentication(); if (authentication != null) { Object principal = authentication.getPrincipal(); if (!(principal instanceof LdapUserDetails)) { throw new IllegalArgumentException( "The principal property of the authentication object -" + "needs to be a LdapUserDetails."); } else { LdapUserDetails details = (LdapUserDetails) principal; return details.getDn(); } } else { log.warn("No Authentication object set in SecurityContext - " + "returning empty String as Principal"); return ""; } }
public LdapUserDetails authenticate(String username, String password) { LdapUserDetails user = null; // If DN patterns are configured, try authenticating with them directly Iterator dns = getUserDns(username).iterator(); while (dns.hasNext() && (user == null)) { user = bindWithDn((String) dns.next(), username, password); } // Otherwise use the configured locator to find the user // and authenticate with the returned DN. if ((user == null) && (getUserSearch() != null)) { LdapUserDetails userFromSearch = getUserSearch().searchForUser(username); user = bindWithDn(userFromSearch.getDn(), username, password); } if (user == null) { throw new BadCredentialsException( messages.getMessage("BindAuthenticator.badCredentials", "Bad credentials")); } return user; }
byte[] passwordBytes = LdapUtils.getUtf8Bytes(encodedPassword); if (!ldapTemplate.compare(user.getDn(), passwordAttributeName, passwordBytes)) { throw new BadCredentialsException(messages.getMessage("PasswordComparisonAuthenticator.badCredentials", "Bad credentials"));
public Essence(LdapUserDetails copyMe) { setDn(copyMe.getDn()); setAttributes(copyMe.getAttributes()); setUsername(copyMe.getUsername()); setPassword(copyMe.getPassword()); setEnabled(copyMe.isEnabled()); setAccountNonExpired(copyMe.isAccountNonExpired()); setCredentialsNonExpired(copyMe.isCredentialsNonExpired()); setAccountNonLocked(copyMe.isAccountNonLocked()); setControls(copyMe.getControls()); setAuthorities(copyMe.getAuthorities()); }