Example usage for org.springframework.security.authentication UsernamePasswordAuthenticationToken getName

List of usage examples for org.springframework.security.authentication UsernamePasswordAuthenticationToken getName

Introduction

In this page you can find the example usage for org.springframework.security.authentication UsernamePasswordAuthenticationToken getName.

Prototype

public String getName() 

Source Link

Usage

From source file:com.example.CloudFoundryAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    UsernamePasswordAuthenticationToken user = (UsernamePasswordAuthenticationToken) authentication;
    String username = user.getName();
    String password = user.getCredentials().toString();
    CloudCredentials credentials = new CloudCredentials(username, password);
    CloudFoundryClient client = new CloudFoundryClient(credentials, properties.getApi());
    OAuth2AccessToken login = client.login();
    CloudFoundryAuthentication result = new CloudFoundryAuthentication(username, login);
    return result;
}

From source file:de.thm.arsnova.entities.User.java

public User(UsernamePasswordAuthenticationToken token) {
    setUsername(token.getName());
    setType(LDAP);
}

From source file:uk.org.openeyes.oink.security.SimpleIdentityService.java

@Override
public String getUserId(Subject s) {
    for (Principal p : s.getPrincipals()) {
        if (p instanceof UsernamePasswordAuthenticationToken) {
            UsernamePasswordAuthenticationToken details = (UsernamePasswordAuthenticationToken) p;
            String name = details.getName();
            String[] parts = name.split("@");
            if (parts.length == 2) {
                return parts[0];
            }//from ww  w .  j av a 2  s  . c o m
        }
    }
    return null;
}

From source file:uk.org.openeyes.oink.security.SimpleIdentityService.java

@Override
public String getOrganisation(Subject s) {
    if (s == null) {
        return null;
    }//from   ww  w.  j  a  va2 s  .  c  o m

    for (Principal p : s.getPrincipals()) {
        if (p instanceof UsernamePasswordAuthenticationToken) {
            UsernamePasswordAuthenticationToken details = (UsernamePasswordAuthenticationToken) p;
            String name = details.getName();
            String[] parts = name.split("@");
            if (parts.length == 2) {
                return parts[1];
            }
        }
    }
    return null;
}

From source file:com.qpark.eip.core.spring.security.EipDaoAuthenticationProvider.java

/**
 * @see org.springframework.security.authentication.dao.DaoAuthenticationProvider#additionalAuthenticationChecks(org.springframework.security.core.userdetails.UserDetails,
 *      org.springframework.security.authentication.UsernamePasswordAuthenticationToken)
 *//*from  w  w  w  .  ja  v a  2 s .  com*/
@Override
protected void additionalAuthenticationChecks(final UserDetails userDetails,
        final UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
    this.logger.debug("+additionalAuthenticationChecks {}", authentication.getName());
    try {
        super.additionalAuthenticationChecks(userDetails, authentication);
    } catch (AuthenticationException e) {
        this.logger.warn(" additionalAuthenticationChecks {}/{} failed: {}", authentication.getName(),
                (userDetails == null ? "NoUserDetailsFound" : userDetails.getUsername()), e.getMessage());
        throw e;
    } finally {
        this.logger.debug("-additionalAuthenticationChecks {}", authentication.getName());
    }
}

From source file:com.allanditzel.dashboard.controller.user.UserController.java

@RequestMapping(value = "/current", method = RequestMethod.GET)
public String redirectToCurrentUser(@CurrentUser UsernamePasswordAuthenticationToken authentication)
        throws IOException {
    String username = authentication.getName();
    User user = userService.getByUsername(username);
    if (user == null) {
        throw new UnknownResourceException("Could not find the current user.");
    }//  ww  w  . ja  v a 2 s . c  o m

    return "redirect:/api/user/" + user.getId();
}

From source file:cn.net.withub.demo.bootsec.hello.security.CustomAuthenticationProvider.java

@Transactional
@Override/*  www.j a v  a 2  s .  c  o m*/
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) authentication;
    String username = token.getName(); //???
    //?
    UserDetails userDetails = null;
    if (username != null) {
        userDetails = userDetailsService.loadUserByUsername(username);
    }

    if (userDetails == null) {
        return null;//null??
        //throw new UsernameNotFoundException("??/?");
    } else if (!userDetails.isEnabled()) {
        throw new DisabledException("?");
    } else if (!userDetails.isAccountNonExpired()) {
        throw new AccountExpiredException("?");
    } else if (!userDetails.isAccountNonLocked()) {
        throw new LockedException("??");
    } else if (!userDetails.isCredentialsNonExpired()) {
        throw new LockedException("?");
    }

    //??
    String encPass = userDetails.getPassword();

    //authentication?credentials
    if (!md5PasswordEncoder.isPasswordValid(encPass, token.getCredentials().toString(), null)) {
        throw new BadCredentialsException("Invalid username/password");
    }

    //?
    return new UsernamePasswordAuthenticationToken(userDetails, encPass, userDetails.getAuthorities());
}

From source file:com.springsource.greenhouse.account.UsernamePasswordAuthenticationProvider.java

public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) authentication;
    try {/*from w  w  w .  j a  va  2 s  .com*/
        Account account = accountRepository.authenticate(token.getName(), (String) token.getCredentials());
        return authenticatedToken(account, authentication);
    } catch (SignInNotFoundException e) {
        throw new org.springframework.security.core.userdetails.UsernameNotFoundException(token.getName(), e);
    } catch (InvalidPasswordException e) {
        throw new BadCredentialsException("Invalid password", e);
    }
}

From source file:org.axonframework.samples.trader.webui.security.TraderAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    if (!supports(authentication.getClass())) {
        return null;
    }//  w  ww  .  ja  v a2s .c om
    UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) authentication;
    String username = token.getName();
    String password = String.valueOf(token.getCredentials());
    FutureCallback<UserAccount> accountCallback = new FutureCallback<UserAccount>();
    AuthenticateUserCommand command = new AuthenticateUserCommand(username, password.toCharArray());
    try {
        commandBus.dispatch(new GenericCommandMessage<AuthenticateUserCommand>(command), accountCallback);
        // the bean validating interceptor is defined as a dispatch interceptor, meaning it is executed before
        // the command is dispatched.
    } catch (StructuralCommandValidationFailedException e) {
        return null;
    }
    UserAccount account;
    try {
        account = accountCallback.get();
        if (account == null) {
            throw new BadCredentialsException("Invalid username and/or password");
        }
    } catch (InterruptedException e) {
        throw new AuthenticationServiceException("Credentials could not be verified", e);
    } catch (ExecutionException e) {
        throw new AuthenticationServiceException("Credentials could not be verified", e);
    }

    UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken(account,
            authentication.getCredentials(), userAuthorities);
    result.setDetails(authentication.getDetails());
    return result;
}

From source file:com.ai.bss.webui.security.AiBssAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    if (!supports(authentication.getClass())) {
        return null;
    }//from  ww  w  . j  av  a2  s .  com
    UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) authentication;
    String username = token.getName();
    String password = String.valueOf(token.getCredentials());
    FutureCallback<UserAccount> accountCallback = new FutureCallback<UserAccount>();
    AuthenticateUserCommand command = new AuthenticateUserCommand(username, password.toCharArray());
    try {
        //            commandBus.dispatch(new GenericCommandMessage<AuthenticateUserCommand>(command), accountCallback);
        // the bean validating interceptor is defined as a dispatch interceptor, meaning it is executed before
        // the command is dispatched.
    } catch (StructuralCommandValidationFailedException e) {
        e.printStackTrace();
        return null;
    }
    UserAccount account;
    try {
        account = accountCallback.get();
        if (account == null) {
            throw new BadCredentialsException("Invalid username and/or password");
        }
    } catch (InterruptedException e) {
        throw new AuthenticationServiceException("Credentials could not be verified", e);
    } catch (ExecutionException e) {
        throw new AuthenticationServiceException("Credentials could not be verified", e);
    }

    UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken(account,
            authentication.getCredentials(), userAuthorities);
    result.setDetails(authentication.getDetails());
    return result;
}