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

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

Introduction

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

Prototype

public UsernamePasswordAuthenticationToken(Object principal, Object credentials,
        Collection<? extends GrantedAuthority> authorities) 

Source Link

Document

This constructor should only be used by AuthenticationManager or AuthenticationProvider implementations that are satisfied with producing a trusted (i.e.

Usage

From source file:nz.net.orcon.kanban.security.NullAuthenticationProvider.java

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

    String username = authIn.getName();
    String password = authIn.getCredentials().toString();
    try {/* w  w w.jav a2s  . com*/
        List<GrantedAuthority> grantedAuths = this.securityTool.getRoles(username);
        Authentication auth = new UsernamePasswordAuthenticationToken(username, password, grantedAuths);
        logger.info("Authenticated User: " + username);
        return auth;
    } catch (Exception e) {
        logger.error("Authentication Exception: " + username, e);
    }
    return null;
}

From source file:se.omegapoint.facepalm.client.security.DbAuthenticationProvider.java

@Override
public Authentication authenticate(final Authentication authentication) throws AuthenticationException {
    final UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) authentication;
    final String username = (String) token.getPrincipal();
    final String password = (String) token.getCredentials();

    final Optional<User> user = userRepository.findByNameAndPassword(username, password);

    return user.map(
            u -> new UsernamePasswordAuthenticationToken(new AuthenticatedUser(u.username), null, emptyList()))
            .orElse(null);//from  ww  w  .j ava 2s.  c  om
}

From source file:com.mothsoft.alexis.security.CurrentUserUtil.java

public static void setSystemUserAuthentication() {
    final UserAuthenticationDetails systemUser = new UserAuthenticationDetails(true);
    SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(systemUser,
            null, UserAuthenticationDetails.ADMIN_AUTHORITIES));
}

From source file:org.callistasoftware.netcare.video.web.security.AuthenticationProvider.java

@Override
public Authentication authenticate(final Authentication auth) throws AuthenticationException {
    log.info("Authenticating {}", auth.getName());
    try {/* www  . j a  v  a  2 s .  co m*/
        final UserDetails details = this.service.loadUserByUsername(auth.getName());
        return new UsernamePasswordAuthenticationToken(details, null, details.getAuthorities());
    } catch (final UsernameNotFoundException e) {

    }

    throw new AuthenticationServiceException("Bad credentials");
}

From source file:com.wooki.services.security.WookiSecurityContextImpl.java

public void log(User user) {
    if (user == null) {
        throw new IllegalArgumentException("User cannot be null");
    }//from   ww w. jav  a  2  s . c o  m
    UsernamePasswordAuthenticationToken logged = new UsernamePasswordAuthenticationToken(
            new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), true,
                    true, true, true, user.getAuthorities()),
            user.getPassword(), user.getAuthorities());
    SecurityContextHolder.getContext().setAuthentication(logged);
}

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   ww w.  j  av  a 2 s.  c  om*/
            return auth;
        }

    }
    return null;
}

From source file:de.pksoftware.springstrap.basic.service.BasicUserDetailsService.java

/**
 * Performs an auto login after signup./*from  w w w  . j  a v  a 2 s  .  c  o m*/
 * @param newAccount
 * @param password
 */
public void autoLogin(IAccount newAccount, String password) {
    UserDetails userDetails = loadUserByUsername(newAccount.getUsername());
    UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(userDetails, password,
            userDetails.getAuthorities());
    authenticationManager.authenticate(auth);
    if (auth.isAuthenticated()) {
        SecurityContextHolder.getContext().setAuthentication(auth);
    }
}

From source file:de.chclaus.examples.controller.AuthController.java

@RequestMapping("/auth")
public String auth(@RequestParam String token) {
    // This should be a service method...
    String usernameByToken = userService.findUsernameByToken(token);
    UserDetails userDetails = userDetailsService.loadUserByUsername(usernameByToken);

    UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(userDetails, null,
            userDetails.getAuthorities());
    SecurityContextHolder.getContext().setAuthentication(authToken);

    LOG.info("User logged in. username={}, token={}", userDetails.getUsername(), token);

    return "redirect:/secured";
}

From source file:com.hp.autonomy.frontend.configuration.authentication.DefaultLoginAuthenticationProvider.java

@Override
public Authentication authenticate(final Authentication authentication) throws AuthenticationException {
    final com.hp.autonomy.frontend.configuration.authentication.Authentication<?> authenticationConfig = configService
            .getConfig().getAuthentication();

    if (!LoginTypes.DEFAULT.equalsIgnoreCase(authenticationConfig.getMethod())) {
        return null;
    }/* w  w  w  . ja  v a  2s  .  c  o  m*/

    final UsernameAndPassword defaultLogin = authenticationConfig.getDefaultLogin();

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

    if (defaultLogin.getUsername().equals(username) && defaultLogin.getPassword().equals(password)) {
        return new UsernamePasswordAuthenticationToken(username, password,
                Arrays.asList(new SimpleGrantedAuthority(roleDefault)));
    } else {
        throw new BadCredentialsException("Access is denied");
    }
}