List of usage examples for org.springframework.security.ldap.userdetails LdapUserDetailsImpl getDn
@Override
public String getDn()
From source file:it.geosolutions.geostore.services.rest.SecurityTest.java
protected void springAuthenticationTest() { doAutoLogin("admin", "admin", null); assertNotNull(SecurityContextHolder.getContext()); assertNotNull(SecurityContextHolder.getContext().getAuthentication()); final Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); authentication.getName();//from w w w.jav a 2s . c om assertEquals("admin", authentication.getCredentials()); Object principal = authentication.getPrincipal(); assertNotNull(principal); if (principal instanceof User) { User user = (User) principal; assertEquals("admin", user.getName()); } else if (principal instanceof LdapUserDetailsImpl) { LdapUserDetailsImpl userDetails = (LdapUserDetailsImpl) principal; assertEquals("uid=admin,ou=people,dc=geosolutions,dc=it", userDetails.getDn()); } assertEquals(authentication.getAuthorities().size(), 1); for (GrantedAuthority authority : authentication.getAuthorities()) { assertEquals("ROLE_ADMIN", authority.getAuthority()); } }
From source file:org.schedoscope.metascope.service.MetascopeUserService.java
/** * Get the user object for the logged in user * * @return/*from w w w. ja va2 s.com*/ * @throws NamingException */ public MetascopeUser getUser() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication == null) { return null; } Object principal = authentication.getPrincipal(); if (principal instanceof LdapUserDetailsImpl) { LdapUserDetailsImpl ldapUser = (LdapUserDetailsImpl) principal; MetascopeUser userEntity = metascopeUserRepository.findByUsername(ldapUser.getUsername()); if (userEntity == null) { createUser(ldapUser.getUsername(), "", "", sha256("" + System.currentTimeMillis()), false, null); } // sync user with ldap userEntity = metascopeUserRepository.findByUsername(ldapUser.getUsername()); DirContextAdapter dca = (DirContextAdapter) ldap.lookup(ldapUser.getDn()); Attributes attr = dca.getAttributes(); String mail = ""; String fullname = ""; try { mail = (String) attr.get("mail").get(); fullname = (String) attr.get("displayName").get(); } catch (NamingException e) { // if not found, ignore .. } boolean admin = false; for (GrantedAuthority authoritiy : ldapUser.getAuthorities()) { for (String adminGroup : config.getAdminGroups().split(",")) { String role = "ROLE_" + adminGroup.toUpperCase(); if (authoritiy.getAuthority().equalsIgnoreCase(role)) { admin = true; } } } boolean changes = false; if (userEntity.getEmail() == null || !userEntity.getEmail().equals(mail)) { userEntity.setEmail(mail); changes = true; } if (userEntity.getFullname() == null || !userEntity.getFullname().equals(fullname)) { userEntity.setFullname(fullname); changes = true; } if (admin) { if (!userEntity.isAdmin()) { changes = true; } userEntity.setUserrole(Role.ROLE_ADMIN); } else { if (userEntity.isAdmin()) { changes = true; } userEntity.setUserrole(Role.ROLE_USER); } if (changes) { metascopeUserRepository.save(userEntity); } return userEntity; } else if (principal instanceof User) { User userDetails = (User) principal; MetascopeUser user = metascopeUserRepository.findByUsername(userDetails.getUsername()); if (user == null) { LOG.warn("User from session not found. username={}", userDetails.getUsername()); return null; } return user; } return null; }