Java Code Examples for org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken#getPrincipal()

The following examples show how to use org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken#getPrincipal() . 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: ApiAuthenticationUserDetailsService.java    From todolist with MIT License 6 votes vote down vote up
@Override
public UserDetails loadUserDetails(PreAuthenticatedAuthenticationToken token) throws UsernameNotFoundException {
	String principal = (String) token.getPrincipal();

	UserDetails result = null;
	if(!Strings.isNullOrEmpty(principal)) {
		logger.debug(principal);
		String[] slices = principal.split(":");
		String email = slices[0];
		String secret = slices[1];

		try {
			AccessToken p = accessTokenService.valid(email, secret);
			result = userService.findByEmail(p.getEmail());
		} catch(Exception ex) {
			throw new UsernameNotFoundException("");
		}
	}

	return result;
}
 
Example 2
Source File: DatabaseUserDetailsService.java    From training with MIT License 6 votes vote down vote up
@Override
	public UserDetails loadUserDetails(PreAuthenticatedAuthenticationToken token) throws UsernameNotFoundException {
		UsernameContextPrincipal principal = (UsernameContextPrincipal) token.getPrincipal();

		String username = principal.getUsername();
		if (StringUtils.isBlank(username)) {
			throw new UsernameNotFoundException("No username provided");
		}

		log.debug("Lookup username {} in database", username);
		User user = userRepository.findByUsername(username);

		if (user == null) {
			throw new UsernameNotFoundException("User " + username + " not in database");
		} else {
			log.debug("User found in database");
			if (!user.isEnabled()) {
				throw new UsernameNotFoundException("User is inactive in the database");
			}
		}

		log.debug("Allowing the request to get in");
//		return new MyUserWithContext<User>(user.getUsername(), user, principal.getContext());
		return user;
	}
 
Example 3
Source File: PreAuthUserDetailsService.java    From bearchoke with Apache License 2.0 6 votes vote down vote up
@Override
public UserDetails loadUserDetails(PreAuthenticatedAuthenticationToken token) throws UsernameNotFoundException {
	String xAuthToken = (String) token.getPrincipal();
	UserDetails user = preAuthenticatedTokenCacheService.getFromCache(xAuthToken);

	if (user == null) {
           throw new UsernameNotFoundException("Pre authenticated token not found : " + xAuthToken);
       } else {
           if (log.isTraceEnabled()) {
               log.trace("Retrieved user from cache: " + user.getUsername());
           }

           // we want to update the expiration date on this key because the user is actively using it
           preAuthenticatedTokenCacheService.updateExpiration(xAuthToken);
       }

	return user;
}
 
Example 4
Source File: BaseJavaDelegateTest.java    From herd with Apache License 2.0 5 votes vote down vote up
/**
 * Asserts the given actual authentication's user ID is equal to the given expected user ID
 *
 * @param expectedUserId Expected user ID
 * @param actualAuthentication Actual authentication object
 */
private void assertAuthenticationUserIdEquals(String expectedUserId, Authentication actualAuthentication)
{
    assertNotNull(actualAuthentication);
    assertEquals(PreAuthenticatedAuthenticationToken.class, actualAuthentication.getClass());
    PreAuthenticatedAuthenticationToken preAuthenticatedAuthenticationToken = (PreAuthenticatedAuthenticationToken) actualAuthentication;
    Object principal = preAuthenticatedAuthenticationToken.getPrincipal();
    assertNotNull(principal);
    assertEquals(SecurityUserWrapper.class, principal.getClass());
    SecurityUserWrapper securityUserWrapper = (SecurityUserWrapper) principal;
    assertEquals(expectedUserId, securityUserWrapper.getUsername());
    assertNotNull(securityUserWrapper.getApplicationUser());
    assertEquals(expectedUserId, securityUserWrapper.getApplicationUser().getUserId());
}
 
Example 5
Source File: HerdUserDetailsService.java    From herd with Apache License 2.0 5 votes vote down vote up
@Override
public UserDetails loadUserDetails(PreAuthenticatedAuthenticationToken token) throws UsernameNotFoundException
{
    ApplicationUser user = (ApplicationUser) token.getPrincipal();

    Set<GrantedAuthority> authorities = new HashSet<>();
    // Add all functional points per given collection of user roles.
    authorities.addAll(securityHelper.mapRolesToFunctions(user.getRoles()));
    // Add all function points that are not mapped to any roles in the system.
    authorities.addAll(securityHelper.getUnrestrictedFunctions());
    SecurityUserWrapper result = new SecurityUserWrapper(user.getUserId(), "N/A", true, true, true, true, authorities, user);

    LOGGER.debug("Loaded User: " + result);
    return result;
}
 
Example 6
Source File: PreAuthTokenSourceTrustAuthenticationProvider.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Authentication authenticate(final Authentication authentication) {
    if (!supports(authentication.getClass())) {
        return null;
    }

    final PreAuthenticatedAuthenticationToken token = (PreAuthenticatedAuthenticationToken) authentication;
    final Object credentials = token.getCredentials();
    final Object principal = token.getPrincipal();
    final Object tokenDetails = token.getDetails();
    final Collection<GrantedAuthority> authorities = token.getAuthorities();

    if (principal == null) {
        throw new BadCredentialsException("The provided principal and credentials are not match");
    }

    final boolean successAuthentication = calculateAuthenticationSuccess(principal, credentials, tokenDetails);

    if (successAuthentication) {
        final PreAuthenticatedAuthenticationToken successToken = new PreAuthenticatedAuthenticationToken(principal,
                credentials, authorities);
        successToken.setDetails(tokenDetails);
        return successToken;
    }

    throw new BadCredentialsException("The provided principal and credentials are not match");
}