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.companyname.services.PlatUserAuthenticationCache.java

private String getHashKey(Authentication authentication) {
    String auth = authentication.getName() + ":" + (String) authentication.getCredentials();
    byte[] encodedAuth = Base64.encode(auth.getBytes(Charset.forName("US-ASCII")));
    String key = "Key-" + new String(encodedAuth);

    logger.info("authentication caching operation uses a key = " + key);

    return key;/*from  w ww  .  j a v a  2  s .  c  om*/
}

From source file:org.ligoj.app.http.security.RestAuthenticationProvider.java

@Override
public Authentication authenticate(final Authentication authentication) {
    final String userpassword = StringUtils.defaultString(authentication.getCredentials().toString(), "");
    final String userName = StringUtils.lowerCase(authentication.getPrincipal().toString());

    // First get the cookie
    final HttpClientBuilder clientBuilder = HttpClientBuilder.create();
    clientBuilder.setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build());
    final HttpPost httpPost = new HttpPost(getSsoPostUrl());

    // Do the POST
    try (CloseableHttpClient httpClient = clientBuilder.build()) {
        final String content = String.format(getSsoPostContent(), userName, userpassword);
        httpPost.setEntity(new StringEntity(content, StandardCharsets.UTF_8));
        httpPost.setHeader("Content-Type", "application/json");
        final HttpResponse httpResponse = httpClient.execute(httpPost);
        if (HttpStatus.SC_NO_CONTENT == httpResponse.getStatusLine().getStatusCode()) {
            // Succeed authentication, save the cookies data inside the authentication
            return newAuthentication(userName, userpassword, authentication, httpResponse);
        }/* w ww  . j a va  2s  .  c  om*/
        log.info("Failed authentication of {}[{}] : {}", userName, userpassword.length(),
                httpResponse.getStatusLine().getStatusCode());
        httpResponse.getEntity().getContent().close();
    } catch (final IOException e) {
        log.warn("Remote SSO server is not available", e);
    }
    throw new BadCredentialsException("Invalid user or password");
}

From source file:com.github.iexel.fontus.web.security.CustomAuthenticationProvider.java

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

    if ((name.equals("admin")) && (password.equals("admin"))) {
        List<GrantedAuthority> grantedAuths = new ArrayList<>();
        grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
        grantedAuths.add(new SimpleGrantedAuthority("ROLE_ADMINISTRATOR"));
        Authentication auth = new UsernamePasswordAuthenticationToken(name, password, grantedAuths);
        return auth;
    }/*  w w w.  j a v  a 2 s .c o m*/

    if ((name.equals("user")) && (password.equals("user"))) {
        List<GrantedAuthority> grantedAuths = new ArrayList<>();
        grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
        Authentication auth = new UsernamePasswordAuthenticationToken(name, password, grantedAuths);
        return auth;
    }

    return null;
}

From source file:br.com.sicva.seguranca.ProvedorAutenticacao.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    String name = authentication.getName();
    String password = authentication.getCredentials().toString();
    UsuariosDao usuariosDao = new UsuariosDao();
    Usuarios usuario = usuariosDao.PesquisarUsuario(name);

    if (usuario == null || !usuario.getUsuariosCpf().equalsIgnoreCase(name)) {
        throw new BadCredentialsException("Username not found.");
    }//w  w  w .j a v  a 2 s  . com

    if (!GenerateMD5.generate(password).equals(usuario.getUsuarioSenha())) {
        throw new LockedException("Wrong password.");
    }
    if (!usuario.getUsuarioAtivo()) {
        throw new DisabledException("User is disable");
    }
    List<GrantedAuthority> funcoes = new ArrayList<>();
    funcoes.add(new SimpleGrantedAuthority("ROLE_" + usuario.getFuncao().getFuncaoDescricao()));
    Collection<? extends GrantedAuthority> authorities = funcoes;
    return new UsernamePasswordAuthenticationToken(name, password, authorities);

}

From source file:edu.eci.test.AAAUserAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    String user = authentication.getPrincipal().toString();
    String pwd = authentication.getCredentials().toString();

    //PUT Auth Bean here

    boolean result = user.equals("myuser") && pwd.equals("mypassword");
    //= aaaProxy.isValidUser(authentication.getPrincipal()
    //.toString(), authentication.getCredentials().toString());

    if (result) {
        List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
        AAAUserAuthenticationToken auth = new AAAUserAuthenticationToken(authentication.getPrincipal(),
                authentication.getCredentials(), grantedAuthorities);

        return auth;
    } else {/*from  ww w.j  a  v a  2 s .  com*/
        throw new BadCredentialsException("Bad User Credentials.");
    }

}

From source file:org.openlmis.fulfillment.security.CustomUserAuthenticationConverterTest.java

private void checkAuthentication(UUID userId, Authentication authentication) {
    assertEquals(userId, authentication.getPrincipal());
    assertEquals("N/A", authentication.getCredentials());
    assertTrue(authentication.isAuthenticated());
}

From source file:net.thewaffleshop.passwd.security.AccountAuthenticationProvider.java

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

    Account user = accountService.authenticateUser(userName, password);
    SecretKey sk = accountAPI.getSecretKey(user, password);

    List<SimpleGrantedAuthority> auths = Arrays.asList(new SimpleGrantedAuthority("ROLE_USER"));
    Authentication auth = new AccountAuthenticationToken(userName, password, auths, user, sk);
    return auth;/*from   w w  w. j  a  v  a  2s.c o  m*/
}

From source file:net.thewaffleshop.nimbus.security.ForwardingAuthenticationHandler.java

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws IOException, ServletException {
    // extract account and password
    String password = (String) authentication.getCredentials();
    AccountUser accountUser = (AccountUser) authentication.getPrincipal();
    Account account = accountUser.getAccount();
    // decode the secret key
    SecretKey secretKey = accountAPI.getSecretKey(account, password);
    // store the account and secret key in the session
    HttpSession session = request.getSession();
    session.setAttribute("account", account);
    session.setAttribute("secretKey", secretKey);

    // forward request to success MVC method
    request.getRequestDispatcher("/authenticationSuccess").forward(request, response);
}

From source file:com.mycompany.apps.oauth2.authentication.security.CustomUserAuthenticationProvider.java

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

    if (authentication.getPrincipal().equals("user") && authentication.getCredentials().equals("user")) {

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

        return auth;

    } else if (authentication.getPrincipal().equals("admin")
            && authentication.getCredentials().equals("admin")) {

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

        return auth;

    } else if (authentication.getPrincipal().equals("user1")
            && authentication.getCredentials().equals("user1")) {

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

    } else {//from w  ww .j a  va 2  s . co  m
        throw new BadCredentialsException("Bad User Credentials.");
    }
}

From source file:com.gisnet.cancelacion.web.controller.AutenticarUsuario.java

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

    FindResponse<UsuarioInfo> find = service.findByUsername(username);
    UsuarioInfo usuario = find.getInfo();

    if (usuario != null) {
        if (service.loguear(new FindByRequest(username, password))) {
            List<GrantedAuthority> grants = new ArrayList<>();
            for (String rol : usuario.getRoles()) {
                grants.add(new SimpleGrantedAuthority(rol));
            }/*from   ww  w .ja v a  2  s.  c  o m*/
            return new UsernamePasswordAuthenticationToken(username, password, grants);
        }
        throw new AuthenticationServiceException("Autenticacion fallida");
    }
    throw new UsernameNotFoundException("Usuario no encontrado.");
}