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:fr.univlorraine.mondossierweb.utils.EmptyJMeterAuthenticationProvider.java

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

    // use the credentials to try to authenticate against the third party system

    List<GrantedAuthority> grantedAuths = new ArrayList<>();
    return new UsernamePasswordAuthenticationToken("toto54", "", grantedAuths);

}

From source file:samples.WithCustomUserSecurityContextFactory.java

public SecurityContext createSecurityContext(WithCustomUser customUser) {
    SecurityContext context = SecurityContextHolder.createEmptyContext();

    User principal = new User();
    principal.setEmail(customUser.email());
    principal.setFirstName(customUser.firstName());
    principal.setLastName(customUser.lastName());
    principal.setId(customUser.id());/*from  w  ww .  ja  v  a2 s.c om*/
    Authentication auth = new UsernamePasswordAuthenticationToken(principal, "password",
            AuthorityUtils.createAuthorityList("ROLE_USER"));
    context.setAuthentication(auth);
    return context;
}

From source file:br.com.saude.controle.LoginControle.java

public String logar() {
    usuario = usuarioFacade.buscarUsuario(email, senha);
    if (usuario != null) {
        List<GrantedAuthority> roles = new ArrayList<>();
        roles.add(new GrantedAuthorityImpl(usuario.getPermissaoUsuario().toString()));

        SecurityContext context = SecurityContextHolder.getContext();

        context.setAuthentication(new UsernamePasswordAuthenticationToken(email, senha, roles));
        if (context.getAuthentication().isAuthenticated()) {
            return "/Home?faces-redirect=true";
        } else {//  w w  w  . j a  v  a 2  s . com
            //message de erro
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Erro", "");
            FacesContext.getCurrentInstance().addMessage(null, message);
            return "Login";
        }
    } else {
        //message de usuario invalido
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Usurio Invlido", "");
        FacesContext.getCurrentInstance().addMessage(null, message);
        return "Login";
    }

}

From source file:ch.ralscha.extdirectspring_itest.LoginController.java

@RequestMapping("/login")
@ResponseBody/*from w w w .  ja  v a 2  s .c o m*/
public void login() {
    UserDetails ud = userManager.loadUserByUsername("jimi");
    if (ud != null) {
        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(ud, null,
                ud.getAuthorities());
        SecurityContextHolder.getContext().setAuthentication(token);
    }
}

From source file:com.golonzovsky.oauth2.google.security.DefaultUserAuthenticationConverter.java

public Authentication extractAuthentication(Map<String, ?> map) {
    if (map.containsKey(USERNAME)) {
        return new UsernamePasswordAuthenticationToken(map.get(USERNAME), "N/A", getAuthorities(map));
    }/*  w w w . ja v a  2 s  .  com*/
    return null;
}

From source file:org.zalando.stups.oauth2.spring.server.AbstractAuthenticationExtractor.java

@Override
public OAuth2Authentication extractAuthentication(final Map<String, Object> map, final String clientId) {
    UsernamePasswordAuthenticationToken user = new UsernamePasswordAuthenticationToken(getPrincipal(map), "N/A",
            AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"));
    user.setDetails(map);/*from   w ww  .j  a v  a  2  s .  c om*/

    // at the moment there is other way
    Set<String> scopes = resolveScopes(map);

    //
    OAuth2Request request = new OAuth2Request(null, clientId, null, true, scopes, null, null, null, null);
    return new OAuth2Authentication(request, user);
}

From source file:com.vaadinspring.components.CustomAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    String name = authentication.getName();
    String password = authentication.getCredentials().toString();
    Users user = userPresenter.getUser(name);
    if (user.getLogin() != null && password.equals(user.getPassword())) {
        List<GrantedAuthority> grantedAuths = new ArrayList<GrantedAuthority>(1);
        grantedAuths.add(new SimpleGrantedAuthority(user.getRole().getRole_name()));
        Authentication auth = new UsernamePasswordAuthenticationToken(name, password, grantedAuths);
        SecurityContextHolder.getContext().setAuthentication(auth);
        return auth;
    } else {/*from  w  w w.j a  va 2  s  . c o m*/
        return null;
    }
}

From source file:nl.surfnet.coin.api.basic.MockBasicAuthenticationManager.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
            authentication.getPrincipal(), authentication.getCredentials(),
            Arrays.asList(new SimpleGrantedAuthority("ROLE_USER")));
    token.setDetails(authentication.getDetails());
    return token;
}

From source file:br.com.itw.qopsearch.api.security.ScaAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    String login = authentication.getName().trim().toLowerCase();
    //        String password = authentication.getCredentials().toString();

    //        byte[] decodedBytes = Base64.decode(password.getBytes());
    //        String decodedPasswd = new String(decodedBytes, Charset.forName("UTF-8"));

    if (EmailValidator.getInstance().isValid(login)) {
        //        if (ValidarEmail.validate(login) && login.split("\\@")[0].equals(decodedPasswd)) {
        List<GrantedAuthority> grantedAuths = new ArrayList<>();
        grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
        Authentication auth = new UsernamePasswordAuthenticationToken(login, null, grantedAuths);
        return auth;
    } else {/*w w w.j av a 2  s . c om*/
        return null;
    }
}

From source file:org.meruvian.yama.web.DefaultCredentialsService.java

@Override
public void registerAuthentication(String userId) {
    User user = userRepository.findById(userId);
    UserDetails userDetails = userDetailsService.loadUserByUsername(user.getUsername());

    Authentication auth = new UsernamePasswordAuthenticationToken(userDetails, null,
            userDetails.getAuthorities());
    SecurityContextHolder.getContext().setAuthentication(auth);
}