Example usage for org.springframework.security.core.userdetails UserDetailsService loadUserByUsername

List of usage examples for org.springframework.security.core.userdetails UserDetailsService loadUserByUsername

Introduction

In this page you can find the example usage for org.springframework.security.core.userdetails UserDetailsService loadUserByUsername.

Prototype

UserDetails loadUserByUsername(String username) throws UsernameNotFoundException;

Source Link

Document

Locates the user based on the username.

Usage

From source file:grails.plugin.springsecurity.SpringSecurityUtils.java

/**
 * Rebuild an Authentication for the given username and register it in the security context.
 * Typically used after updating a user's authorities or other auth-cached info.
 * <p/>/*from  w w w  . j a  va 2 s  .c o m*/
 * Also removes the user from the user cache to force a refresh at next login.
 *
 * @param username the user's login name
 * @param password optional
 */
public static void reauthenticate(final String username, final String password) {
    UserDetailsService userDetailsService = getBean("userDetailsService");
    UserCache userCache = getBean("userCache");

    UserDetails userDetails = userDetailsService.loadUserByUsername(username);
    SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(userDetails,
            password == null ? userDetails.getPassword() : password, userDetails.getAuthorities()));
    userCache.removeUserFromCache(username);
}

From source file:com.cloudseal.spring.client.userdetails.SAMLUserDetailsServiceAdapterTest.java

@Test
public void standardExternalImplementation() {
    final UserDetails details = mock(UserDetails.class);
    final UserDetailsService service = mock(UserDetailsService.class);
    when(service.loadUserByUsername(anyString())).thenReturn(details);

    final SAMLUserDetailsServiceAdapter adapter = new SAMLUserDetailsServiceAdapter();
    adapter.setUserDetailsService(service);

    adapter.loadUserBySAML(credential);// www . ja  v a2 s . c  o m
    verify(service, times(1)).loadUserByUsername(anyString());
}

From source file:org.red5.demo.auth.AggregatedUserDetailsService.java

/**
 * Retrieves a user record containing the user's credentials and access. 
 *//*from  w  w  w .ja  va 2 s  .co m*/
public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
    log.debug("loadUserByUsername: {}", userName);
    UserDetails userDetails = null;
    // get all the user details services
    Map<String, UserDetailsService> userDetailsServiceMap = applicationContext
            .getBeansOfType(UserDetailsService.class);
    log.debug("User details services: {}", userDetailsServiceMap);
    // loop through the map for any service providing a lookup, ensure that we skip 'this' class
    for (Entry<String, UserDetailsService> entry : userDetailsServiceMap.entrySet()) {
        UserDetailsService val = entry.getValue();
        if (val instanceof AggregatedUserDetailsService) {
            continue;
        }
        UserDetails tmp = val.loadUserByUsername(userName);
        if (tmp != null) {
            log.debug("User details found in {}", entry.getKey());
            userDetails = tmp;
            break;
        }
    }
    if (userDetails == null) {
        throw new UsernameNotFoundException("User " + userName + " not found");
    }
    return userDetails;
}

From source file:com.mastercard.test.spring.security.WithUserDetailsSecurityContextFactory.java

public SecurityContext createSecurityContext(WithUserDetails withUser) {
    String beanName = withUser.userDetailsServiceBeanName();
    UserDetailsService userDetailsService = StringUtils.hasLength(beanName)
            ? this.beans.getBean(beanName, UserDetailsService.class)
            : this.beans.getBean(UserDetailsService.class);
    String username = withUser.value();
    Assert.hasLength(username, "value() must be non empty String");
    UserDetails principal = userDetailsService.loadUserByUsername(username);
    Authentication authentication = new UsernamePasswordAuthenticationToken(principal, principal.getPassword(),
            principal.getAuthorities());
    SecurityContext context = SecurityContextHolder.createEmptyContext();
    context.setAuthentication(authentication);
    return context;
}

From source file:it.f2informatica.webapp.test.security.DatabaseUserDetailServiceTest.java

@Before
public void setUp() {
    UserDetailsService userDetailsService = new DatabaseUserDetailService();
    ((DatabaseUserDetailService) userDetailsService).setAuthenticationService(authenticationService);
    ((DatabaseUserDetailService) userDetailsService).setAuthorityService(authorityService);

    when(authenticationService.processLogin("username1")).thenReturn(Optional.of(createResponse()));
    when(authorityService.createAuthorities("role_admin")).thenReturn(getGrantedAuthorities());
    userAuthenticated = userDetailsService.loadUserByUsername("username1");
}

From source file:org.eclipse.hawkbit.ui.common.UserDetailsFormatter.java

@SuppressWarnings({ "squid:S1166" })
private static UserDetails loadUserByUsername(final String username) {
    final UserDetailsService userDetailsService = SpringContextHelper.getBean(UserDetailsService.class);
    try {/*  w ww  . ja v  a2 s . c o  m*/
        return userDetailsService.loadUserByUsername(username);
    } catch (final UsernameNotFoundException e) {
        return new User(username, "", Collections.emptyList());
    }
}

From source file:org.pentaho.platform.plugin.services.exporter.PentahoPlatformExporter.java

protected void exportUsersAndRoles() {
    log.debug("export users & roles");

    IUserRoleListService userRoleListService = PentahoSystem.get(IUserRoleListService.class);
    UserDetailsService userDetailsService = PentahoSystem.get(UserDetailsService.class);

    IRoleAuthorizationPolicyRoleBindingDao roleBindingDao = PentahoSystem
            .get(IRoleAuthorizationPolicyRoleBindingDao.class);
    ITenant tenant = TenantUtils.getCurrentTenant();

    //  get the user settings for this user
    IUserSettingService service = getUserSettingService();

    //User Export
    List<String> userList = userRoleListService.getAllUsers(tenant);
    for (String user : userList) {
        UserExport userExport = new UserExport();
        userExport.setUsername(user);/*from   w  w  w . j a va  2s.c o  m*/
        userExport.setPassword(userDetailsService.loadUserByUsername(user).getPassword());

        for (String role : userRoleListService.getRolesForUser(tenant, user)) {
            userExport.setRole(role);
        }

        if (service != null && service instanceof IAnyUserSettingService) {
            IAnyUserSettingService userSettings = (IAnyUserSettingService) service;
            List<IUserSetting> settings = userSettings.getUserSettings(user);
            if (settings != null) {
                for (IUserSetting setting : settings) {
                    userExport.addUserSetting(new ExportManifestUserSetting(setting));
                }
            }
        }

        this.getExportManifest().addUserExport(userExport);
    }

    // export the global user settings
    if (service != null) {
        List<IUserSetting> globalUserSettings = service.getGlobalUserSettings();
        if (globalUserSettings != null) {
            for (IUserSetting setting : globalUserSettings) {
                getExportManifest().addGlobalUserSetting(new ExportManifestUserSetting(setting));
            }
        }
    }

    //RoleExport
    List<String> roles = userRoleListService.getAllRoles();
    for (String role : roles) {
        RoleExport roleExport = new RoleExport();
        roleExport.setRolename(role);
        roleExport.setPermission(roleBindingDao.getRoleBindingStruct(null).bindingMap.get(role));
        exportManifest.addRoleExport(roleExport);
    }
}