Example usage for org.springframework.security.core Authentication getCredentials

List of usage examples for org.springframework.security.core Authentication getCredentials

Introduction

In this page you can find the example usage for org.springframework.security.core Authentication getCredentials.

Prototype

Object getCredentials();

Source Link

Document

The credentials that prove the principal is correct.

Usage

From source file:com.javaeeeee.components.JpaAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    Optional<User> optional = usersRepository.findByUsernameAndPassword(authentication.getName(),
            authentication.getCredentials().toString());
    if (optional.isPresent()) {
        return new UsernamePasswordAuthenticationToken(authentication.getPrincipal(),
                authentication.getCredentials(), authentication.getAuthorities());

    } else {/*from w ww . j a va 2s.c  o m*/
        throw new AuthenticationCredentialsNotFoundException("Wrong credentials.");
    }
}

From source file:com.seajas.search.utilities.spring.security.service.ExtendedAuthenticationProvider.java

/**
 * Override the authenticate method to provide our own extended UserDetails based logic.
 * //from   www  .j  a  v a 2s .  co m
 * @param authentication
 * @throws AuthenticationException
 */
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    if (StringUtils.isEmpty((String) authentication.getPrincipal())
            || StringUtils.isEmpty((String) authentication.getCredentials()))
        throw new BadCredentialsException("The given username / password are invalid");

    UserDetails userDetails = extendedUserDetailsService.getUserDetails((String) authentication.getPrincipal(),
            (String) authentication.getCredentials());

    return new UsernamePasswordAuthenticationToken(userDetails, authentication.getCredentials(),
            userDetails.getAuthorities());
}

From source file:org.elasticstore.server.service.impl.ElasticSearchAuthenticationManager.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    final String username = authentication.getName();
    return new UsernamePasswordAuthenticationToken(authentication.getPrincipal(),
            authentication.getCredentials());
}

From source file:md.ibanc.rm.sessions.CustomAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {

    String username = authentication.getName();
    String password = authentication.getCredentials().toString();

    LoginFormValidator loginFormValidator = new LoginFormValidator();

    if (!loginFormValidator.isLoginValid(username)) {
        throw new BadCredentialsException("Numele de utilizator contine simboluri interzise");
    }//from w w  w . ja  va2s  .  c  o m

    if (!loginFormValidator.isValidPassword(password)) {
        throw new BadCredentialsException("Parola contine simboluri interzise");
    }

    Users users = usersService.findUserByEmail(username);

    if (users == null) {
        throw new UsernameNotFoundException("Asa utilizator nu exista in baza de date");
    }

    String hashPassword = UtilHashMD5.createMD5Hash(FormatPassword.CreatePassword(users.getId(), password));

    if (!hashPassword.equals(users.getPassword())) {
        throw new BadCredentialsException("Parola este gresita");
    }

    if (!users.getActive()) {
        throw new AccountExpiredException("Nu aveti drept sa accesati acest serviciu");
    }

    Collection<? extends GrantedAuthority> authoritiesRoles = getAuthorities(users);

    return new UsernamePasswordAuthenticationToken(username, hashPassword, authoritiesRoles);

}

From source file:com.ar.dev.tierra.api.config.security.CustomAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication auth) throws AuthenticationException {
    String username = String.valueOf(auth.getName());
    String password = String.valueOf(auth.getCredentials().toString());

    Usuarios us = null;/*from w  ww .j  a  va2 s .c  om*/
    boolean success = false;
    try {
        us = user.findUsuarioByUsername(username);
        success = passwordEncoder.matches(password, us.getPassword());
    } catch (Exception ex) {
    }
    if (success == true) {
        final List<GrantedAuthority> grantedAuths = new ArrayList<>();
        String authority;
        switch (us.getRoles().getNombreRol()) {
        case "ADMINISTRADOR":
            authority = "ROLE_ADMIN";
            break;
        case "VENDEDOR":
            authority = "ROLE_VENDEDOR";
            break;
        default:
            authority = "ROLE_NONE";
            break;
        }
        GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(authority);
        grantedAuths.add(grantedAuthority);
        final UserDetails principal = new User(username, password, grantedAuths);
        final Authentication authentication = new UsernamePasswordAuthenticationToken(principal, password,
                grantedAuths);
        us = null;
        return authentication;
    } else {
        throw new BadCredentialsException("Bad Credentials");
    }
}

From source file:io.github.azige.bbs.service.AccountService.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    String username = authentication.getName();
    String password = (String) authentication.getCredentials();

    Account account = accountRepository.findByAccountName(username);
    if (account == null) {
        throw new UsernameNotFoundException("User name not found");
    }/* w ww  . j av  a 2  s . com*/
    password = encryptPassword(password, account.getSalt());
    if (password.equals(account.getPassword())) {
        return new UsernamePasswordAuthenticationToken(account.getProfile(), password, Account.AUTHORITYS);
    }
    return authentication;
}

From source file:eu.freme.broker.security.DatabaseAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {

    String username = (String) authentication.getPrincipal();
    String password = (String) authentication.getCredentials();

    User user = userRepository.findOneByName(username);
    if (user == null) {
        throw new BadCredentialsException("authentication failed");
    }//from ww  w.  java 2s  .c o  m

    try {
        if (!PasswordHasher.check(password, user.getPassword())) {
            throw new BadCredentialsException("authentication failed");
        }
    } catch (BadCredentialsException e) {
        throw e;
    } catch (Exception e) {
        logger.error(e);
        throw new InternalServerErrorException();
    }

    Token token = tokenService.generateNewToken(user);

    AuthenticationWithToken auth = new AuthenticationWithToken(user, null,
            AuthorityUtils.commaSeparatedStringToAuthorityList(user.getRole()), token);
    return auth;

}

From source file:br.com.joaops.smt.security.SmtAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication a) throws AuthenticationException {
    String username = a.getName();
    String password = a.getCredentials().toString();

    UserDetails user = this.userDetails.loadUserByUsername(username);

    if (user == null) {
        String message = this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials",
                "AbstractUserDetailsAuthenticationProvider.badCredentials");
        throw new BadCredentialsException(message);
    }//from   w  ww.  j a  v a2 s .  c  o m

    if (!user.getUsername().equals(username)) {
        String message = this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials",
                "AbstractUserDetailsAuthenticationProvider.badCredentials");
        throw new BadCredentialsException(message);
    }

    if (!passwordEncoder.matches(password, user.getPassword())) {
        String message = this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials",
                "AbstractUserDetailsAuthenticationProvider.badCredentials");
        throw new BadCredentialsException(message);
    }

    if (user.isEnabled() == false) {
        String message = this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.disabled",
                "AbstractUserDetailsAuthenticationProvider.disabled");
        throw new DisabledException(message);
    }

    if (user.isAccountNonLocked() == false) {
        String message = this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.locked",
                "AbstractUserDetailsAuthenticationProvider.locked");
        throw new LockedException(message);
    }

    if (user.isAccountNonExpired() == false) {
        String message = this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.expired",
                "AbstractUserDetailsAuthenticationProvider.expired");
        throw new AccountExpiredException(message);
    }

    if (user.isCredentialsNonExpired() == false) {
        String message = this.messages.getMessage(
                "AbstractUserDetailsAuthenticationProvider.credentialsExpired",
                "AbstractUserDetailsAuthenticationProvider.credentialsExpired");
        throw new CredentialsExpiredException(message);
    }

    return new UsernamePasswordAuthenticationToken(user, user.getPassword(), user.getAuthorities());
}

From source file:at.ac.univie.isc.asio.security.HttpMethodRestrictionFilterTest.java

@Test
public void should_keep_other_token_properties() throws Exception {
    final TestingAuthenticationToken token = new TestingAuthenticationToken("user", "secret",
            Collections.<GrantedAuthority>singletonList(Permission.INVOKE_UPDATE));
    token.setDetails("details");
    setAuthentication(token);// w w w.j  a  v  a2 s. com
    request.setMethod(HttpMethod.GET.name());
    subject.doFilter(request, response, chain);
    final Authentication filtered = getAuthentication();
    assertThat(filtered.getPrincipal(), equalTo(token.getPrincipal()));
    assertThat(filtered.getCredentials(), equalTo(token.getCredentials()));
    assertThat(filtered.getDetails(), equalTo(token.getDetails()));
}

From source file:com.katropine.oauth.CustomUserAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {

    LOGGER.warning("!!!Authenticate: " + authentication.getPrincipal().toString() + ":"
            + authentication.getCredentials().toString());

    if (!supports(authentication.getClass())) {
        return null;
    }/*from  w w  w . j  a  v  a2s  . c  om*/
    if (authentication.getCredentials() == null) {
        LOGGER.warning("No credentials found in request.");
        boolean throwExceptionWhenTokenRejected = false;
        if (throwExceptionWhenTokenRejected) {
            throw new BadCredentialsException("No pre-authenticated credentials found in request.");
        }
        return null;
    }

    User user = userDAO.getByEmail(authentication.getPrincipal().toString());

    BCryptPasswordEncoder enc = new BCryptPasswordEncoder();
    if (!enc.matches(authentication.getCredentials().toString(), user.getPassword())) {
        throw new BadCredentialsException("Bad User Credentials.");
    }

    List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
    CustomUserPasswordAuthenticationToken auth = new CustomUserPasswordAuthenticationToken(
            authentication.getPrincipal(), authentication.getCredentials(), grantedAuthorities);

    return auth;

}