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:org.openengsb.opencit.ui.web.AbstractCitPageTest.java

private void mockAuthentication() {
    AuthenticationManager authManager = mock(AuthenticationManager.class);
    final Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
    authorities.add(new GrantedAuthorityImpl("ROLE_USER"));
    when(authManager.authenticate(any(Authentication.class))).thenAnswer(new Answer<Authentication>() {
        @Override//from  w  w  w  .  java 2 s.co m
        public Authentication answer(InvocationOnMock invocation) {
            Authentication auth = (Authentication) invocation.getArguments()[0];
            if (auth.getCredentials().equals("password")) {
                return new UsernamePasswordAuthenticationToken(auth.getPrincipal(), auth.getCredentials(),
                        authorities);
            }
            throw new BadCredentialsException("wrong password");
        }
    });
    appContext.putBean("authenticationManager", authManager);
}

From source file:com.web.mavenproject6.config.CustomAuthenticationProvider.java

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

    System.err.println("!!!!Password " + password);
    for (Users u : userServiceImp.list()) {
        if (u.getUsername().equals(name) && u.getPassword().equals(password)) {
            final List<GrantedAuthority> grantedAuths = new ArrayList<GrantedAuthority>();
            grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
            final UserDetails principal = new User(name, password, grantedAuths);
            final Authentication auth = new UsernamePasswordAuthenticationToken(principal, password,
                    grantedAuths);//from   w  ww .  ja v a 2 s .c o m
            return auth;
        }

    }
    return null;
}

From source file:net.maritimecloud.identityregistry.security.MCAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    String name = authentication.getName();
    String password = authentication.getCredentials().toString();
    logger.debug("In authenticate");
    // The login name is the org shortname
    // Organization org = this.organizationService.getOrganizationByShortName(name);
    // if an org was found, test the password
    // if (org != null && (new BCryptPasswordEncoder().matches(password, org.getPasswordHash()))) {
    if (!password.isEmpty()) {
        List<GrantedAuthority> grantedAuths = new ArrayList<>();
        grantedAuths.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
        Authentication auth = new UsernamePasswordAuthenticationToken(name, authentication.getCredentials(),
                grantedAuths);/*from ww  w.  ja  v a2  s  .  c  om*/
        logger.debug("Got authenticated: " + auth.isAuthenticated());
        return auth;
    } else {
        logger.debug("Didn't get authenticated");
        throw new BadCredentialsException("Bad Credentials");
    }
}

From source file:mum.edu.sec.AuthenticationEventListener.java

@Override
public void onApplicationEvent(AbstractAuthenticationEvent authenticationEvent) {
    if (authenticationEvent instanceof InteractiveAuthenticationSuccessEvent) {
        // ignores to prevent duplicate logging with AuthenticationSuccessEvent
        return;//from  w  w w.  j  a va  2  s.c o m
    }
    Authentication authentication = authenticationEvent.getAuthentication();
    String auditMessage = "Login attempt with username: " + authentication.getName() + " pass: '"
            + authentication.getCredentials() + "'" + "\t\tSuccess: " + authentication.isAuthenticated();
    System.err.println(auditMessage);
    //logger.info(auditMessage);
}

From source file:com.cosw.productsmaster.authsec.UserAuthenticationProvider.java

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");
    System.out.println("hola" + result);
    if (result) {
        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        UserAuthenticationToken auth = new UserAuthenticationToken(authentication.getPrincipal(),
                authentication.getCredentials(), grantedAuthorities);

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

}

From source file:org.exoplatform.acceptance.security.CrowdAuthenticationProviderMock.java

/**
 * {@inheritDoc}//from  ww w.j  ava 2 s. c o  m
 * Performs authentication with the same contract as {@link
 * org.springframework.security.authentication.AuthenticationManager#authenticate(Authentication)}.
 */
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    String name = authentication.getName();
    String password = authentication.getCredentials().toString();
    try {
        UserDetails user = crowdUserDetailsServiceMock.loadUserByUsername(name);
        if (user.getPassword().equals(password)) {
            return new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword(),
                    user.getAuthorities());
        } else {
            throw new BadCredentialsException("Invalid username or password");
        }
    } catch (UsernameNotFoundException unnfe) {
        throw new BadCredentialsException("Invalid username or password", unnfe);
    }
}

From source file:com.company.project.web.controller.service.CustomAuthenticationProvider.java

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

    // CustomUserDetailsService will take care of password comparison
    // return null if username is not existing or password comparison fails
    UserDetails userDetails = customUserDetailsService.loadUserByUsername(name);

    if (userDetails == null) {
        throw new BadCredentialsException("Username not found or password incorrect.");
    }//from   w  w w.j a  va 2  s .co m

    if (userDetails != null) {

        // 3. Preferably clear the password in the user object before storing in authentication object           
        //return new UsernamePasswordAuthenticationToken(name, null, userDetails.getAuthorities());
        // OR
        return new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());

        // use authentication.getPrincipal() to get the "userDetails" object
    }
    return null;
}

From source file:com.kabiliravi.kaman.web.AuthenticationProviderImpl.java

@Override
public Authentication authenticate(Authentication auth) throws AuthenticationException {
    return new UsernamePasswordAuthenticationToken(auth.getPrincipal(), auth.getCredentials());
}

From source file:com.mec.Security.CustomAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    Authentication auth = null;/*from  ww w. j a v  a 2  s  .c om*/
    final String name = authentication.getName();
    final String password = authentication.getCredentials().toString();
    if (name != null && password != null) {
        Usuario u = userDAO.getUser(name, password);
        if (u != null) {
            final UserDetails principal = new User(u.getId() + ";" + name, password, u.getRoles());
            auth = new UsernamePasswordAuthenticationToken(principal, password, u.getRoles());
        }
    }
    return auth;
}

From source file:com.ushahidi.swiftriver.core.api.auth.crowdmapid.CrowdmapIDAuthenticationProvider.java

@Transactional(readOnly = true)
@Override/*from ww  w . ja v  a 2 s .c  o m*/
public Authentication authenticate(Authentication authentication) throws AuthenticationException {

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

    User user = userDao.findByUsernameOrEmail(username);

    if (user == null || !crowdmapIDClient.signIn(username, password)) {
        throw new BadCredentialsException(String.format("Invalid username/password pair for %s", username));
    }
    Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
    for (Role role : user.getRoles()) {
        authorities.add(new SimpleGrantedAuthority("ROLE_" + role.getName().toUpperCase()));
    }

    UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken(username,
            authentication.getCredentials(), authorities);
    result.setDetails(authentication.getDetails());
    return result;
}