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.blackducksoftware.tools.appedit.web.auth.AppEditAuthenticationProvider.java

/**
 * Attempt to authenticate the given user.
 *//*ww  w  .ja v  a  2s . c om*/
@Override
public Authentication authenticate(Authentication authentication) {
    try {
        // User provided data from login page
        String username = (String) authentication.getPrincipal();
        String password = (String) authentication.getCredentials();

        validateInput(username, password);

        UsernamePasswordAuthenticationToken auth = generateAuthenticationToken(username, password);

        return auth;
    } catch (Exception e) {
        throw new AuthenticationServiceException(e.getMessage(), e);
    }
}

From source file:org.nuclos.client.remote.http.SecuredBasicAuthHttpInvokerRequestExecutor.java

@Override
protected HttpPost createHttpPost(HttpInvokerClientConfiguration config) throws IOException {
    HttpPost postMethod = super.createHttpPost(config);
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();

    if ((auth != null) && (auth.getName() != null)) {
        String base64 = auth.getName() + ":" + LangUtils.defaultIfNull(auth.getCredentials(), "");
        postMethod.setHeader("Authorization", "Basic " + new String(Base64.encodeBase64(base64.getBytes())));
    }//from   ww  w .  j  a v  a 2s  .c  o  m
    return postMethod;
}

From source file:io.github.autsia.crowly.security.CrowlyAuthenticationManager.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    try {//from   w  w  w. j  a v  a  2  s  .  c om
        CrowlyUser dbUser = userRepository.findByEmail(authentication.getName());
        if (bCryptPasswordEncoder.matches(authentication.getCredentials().toString(), dbUser.getPassword())) {
            return new UsernamePasswordAuthenticationToken(authentication.getName(),
                    authentication.getCredentials(), getAuthorities(dbUser));
        }
    } catch (Exception e) {
        logger.error(e.getMessage());
    }
    throw new BadCredentialsException(authentication.getName());
}

From source file:com.seyren.core.security.mongo.MongoAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    User user = userStore.getUser(authentication.getName());
    if (user == null) {
        throw new AuthenticationCredentialsNotFoundException("User does not exist");
    }/*w  w  w  .ja v  a  2 s.  c  o m*/
    String password = authentication.getCredentials().toString();
    if (passwordEncoder.matches(password, user.getPassword())) {
        return new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword(),
                user.getAuthorities());
    } else {
        throw new BadCredentialsException("Bad Credentials");
    }
}

From source file:org.btc4j.ws.impl.BtcDaemonServicePortImpl.java

private BtcDaemon getDaemon(String method) {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    LOG.info(auth.getName() + "@" + daemonUrl + "/" + method);
    return new BtcDaemon(daemonUrl, String.valueOf(auth.getName()), String.valueOf(auth.getCredentials()));
}

From source file:org.ngrinder.security.NGrinderPluginUserDetailsServiceTest.java

@SuppressWarnings({ "unchecked", "serial" })
@Test/*from   w  w  w  . j  a  va  2s.c om*/
public void testSecondAuth() {
    // Given that there exists two plugins.
    Authentication auth = mock(UsernamePasswordAuthenticationToken.class);
    authProvider = spy(authProvider);

    when(auth.getPrincipal()).thenReturn("hello1");
    when(auth.getName()).thenReturn("hello1");
    when(auth.getCredentials()).thenReturn("world");

    when(manager.getEnabledModulesByClass(any(OnLoginRunnable.class.getClass()), any(OnLoginRunnable.class)))
            .thenReturn(new ArrayList<OnLoginRunnable>() {
                {
                    add(defaultLoginPlugin);
                    add(mockLoginPlugin);
                }
            });

    // When user is return by plugin module.
    User user = new User();
    user.setUserName("hello1");
    user.setUserId("hello1");
    user.setEmail("helloworld@gmail.com");
    user.setRole(Role.SUPER_USER);
    user.setAuthProviderClass(mockLoginPlugin.getClass().getName());
    when(mockLoginPlugin.loadUser("hello1")).thenReturn(user);
    when(mockLoginPlugin.validateUser(anyString(), anyString(), anyString(), any(), any())).thenReturn(true);

    // Then, Auth should be succeeded.
    assertThat(authProvider.authenticate(auth), notNullValue());

    // And should be inserted into DB
    // verify(authProvider, times(1)).addNewUserIntoLocal(any(SecuredUser.class));

    reset(authProvider);
    when(mockLoginPlugin.loadUser("hello1")).thenReturn(user);
    // Then, Auth should be succeeded.
    assertThat(authProvider.authenticate(auth), notNullValue());

    // And should not be inserted into DB
    // verify(authProvider, times(0)).addNewUserIntoLocal(any(SecuredUser.class));

}

From source file:it.geosolutions.geostore.services.rest.SecurityTest.java

protected void springAuthenticationTest() {
    doAutoLogin("admin", "admin", null);

    assertNotNull(SecurityContextHolder.getContext());
    assertNotNull(SecurityContextHolder.getContext().getAuthentication());

    final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    authentication.getName();/*www . jav  a  2 s. c  o m*/

    assertEquals("admin", authentication.getCredentials());

    Object principal = authentication.getPrincipal();
    assertNotNull(principal);

    if (principal instanceof User) {
        User user = (User) principal;

        assertEquals("admin", user.getName());
    } else if (principal instanceof LdapUserDetailsImpl) {
        LdapUserDetailsImpl userDetails = (LdapUserDetailsImpl) principal;

        assertEquals("uid=admin,ou=people,dc=geosolutions,dc=it", userDetails.getDn());
    }

    assertEquals(authentication.getAuthorities().size(), 1);

    for (GrantedAuthority authority : authentication.getAuthorities()) {
        assertEquals("ROLE_ADMIN", authority.getAuthority());
    }

}

From source file:binky.reportrunner.service.impl.AuthenticationServiceImpl.java

public Authentication authenticate(Authentication authentication) throws AuthenticationException {

    logger.info("authenticate service invoked");

    if (StringUtils.isBlank((String) authentication.getPrincipal())
            || StringUtils.isBlank((String) authentication.getCredentials())) {
        logger.debug("userName blank is " + StringUtils.isBlank((String) authentication.getPrincipal()
                + " password blank is " + StringUtils.isBlank((String) authentication.getCredentials())));
        throw new BadCredentialsException("Invalid username/password");

    }/*w w w  .  j  ava2 s .c o m*/

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

    RunnerUser user = userDao.get(userName);

    EncryptionUtil enc = new EncryptionUtil();

    List<GrantedAuthority> authorities = new LinkedList<GrantedAuthority>();
    try {
        if (user != null && user.getPassword().equals(enc.hashString(password))) {
            if (user.getIsAdmin()) {
                logger.info("admin login for user: " + userName);
                authorities.add(new GrantedAuthorityImpl("ROLE_ADMIN"));
            } else {
                logger.info("user login for user: " + userName);
            }
            authorities.add(new GrantedAuthorityImpl("ROLE_USER"));
        } else {
            logger.warn("login fail for user: " + userName);

            throw new BadCredentialsException("Invalid username/password");
        }
    } catch (Exception e) {

        logger.fatal(e.getMessage(), e);
        throw new AuthenticationServiceException(e.getMessage(), e);
    }

    return new UsernamePasswordAuthenticationToken(userName, authentication.getCredentials(), authorities);

}

From source file:com.himanshu.poc.h2.springboot.AuthenticationProviderImpl.java

@Override
public Authentication authenticate(Authentication arg0) throws AuthenticationException {
    System.out.println(" User name is : " + arg0.getName());
    //arg0.setAuthenticated(false);
    //return arg0;
    if (dummyUsernamePwdMap.get(arg0.getPrincipal()) != null
            && dummyUsernamePwdMap.get(arg0.getPrincipal()).equals(arg0.getCredentials())) {
        System.out.println("Auth success");
        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(arg0.getPrincipal(),
                arg0.getCredentials(), arg0.getAuthorities());
        return token;
    }// w  ww . j  a v a  2s  .  c o m
    System.out.println("Auth failed");
    return null;
}

From source file:nl.surfnet.mujina.spring.SAMLResponseAuthenticationProvider.java

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

    logger.debug("attempting to authenticate: {}", submitted);

    User user = assertionConsumer.consume((Response) submitted.getPrincipal());

    SAMLAuthenticationToken authenticated = new SAMLAuthenticationToken(user,
            (String) submitted.getCredentials(), user.getAuthorities());

    authenticated.setDetails(submitted.getDetails());

    logger.debug("Returning with authentication token of {}", authenticated);

    return authenticated;

}