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:org.trustedanalytics.servicecatalog.security.JwtUserDetailsTokenConverter.java

@Override
public Authentication extractAuthentication(Map<String, ?> map) {
    if (!map.containsKey(USERNAME)) {
        return null;
    }/*  w w  w  . java 2 s . c o  m*/

    if (map.containsKey(AUTHORITIES)) {
        return super.extractAuthentication(map);
    }

    if (map.containsKey(SCOPE)) {
        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(map.get(USERNAME),
                "N/A", getAuthorities(map));
        if (map.containsKey(USER_ID)) {
            AccessTokenDetails details = new AccessTokenDetails(UUID.fromString((String) map.get(USER_ID)));
            token.setDetails(details);
        }
        return token;
    }

    return null;
}

From source file:com.greglturnquist.payroll.DatabaseLoader.java

@Override
public void run(String... strings) throws Exception {

    Manager greg = this.managers.save(new Manager("greg", "turnquist", "ROLE_MANAGER"));
    Manager oliver = this.managers.save(new Manager("oliver", "gierke", "ROLE_MANAGER"));

    SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken("greg",
            "doesn't matter", AuthorityUtils.createAuthorityList("ROLE_MANAGER")));

    this.employees.save(new Employee("Frodo", "Baggins", "ring bearer", greg));
    this.employees.save(new Employee("Bilbo", "Baggins", "burglar", greg));
    this.employees.save(new Employee("Gandalf", "the Grey", "wizard", greg));

    SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken("oliver",
            "doesn't matter", AuthorityUtils.createAuthorityList("ROLE_MANAGER")));

    this.employees.save(new Employee("Samwise", "Gamgee", "gardener", oliver));
    this.employees.save(new Employee("Merry", "Brandybuck", "pony rider", oliver));
    this.employees.save(new Employee("Peregrin", "Took", "pipe smoker", oliver));

    SecurityContextHolder.clearContext();
}

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;
    }/*from w ww  .  j a  v a2s  . com*/

    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:com.stormpath.spring.security.provider.UsernamePasswordAuthenticationTokenFactory.java

@Override
public Authentication createAuthenticationToken(Object principal, Object credentials,
        Collection<? extends GrantedAuthority> authorities, Account account) {
    UserDetails userDetails = new StormpathUserDetails(principal.toString(), (String) credentials, authorities,
            account);//  w  w  w.ja v a2  s. c  om
    UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(userDetails,
            credentials, userDetails.getAuthorities());
    return authToken;
}

From source file:com.xavin.config.security.JWTLoginFilter.java

/**
 * @Override protected boolean requiresAuthentication(HttpServletRequest
 * request, HttpServletResponse response) {
 *
 * }//  w  w w . ja v a2 s  .c o  m
 */
@Override
public Authentication attemptAuthentication(HttpServletRequest req, HttpServletResponse res)
        throws AuthenticationException, IOException, ServletException {
    String data = IOUtils.toString(req.getInputStream(), StandardCharsets.UTF_8);
    AccountCredentials creds = new ObjectMapper().readValue(data, AccountCredentials.class);
    return getAuthenticationManager().authenticate(new UsernamePasswordAuthenticationToken(creds.getUsername(),
            creds.getPassword(), Collections.emptyList()));
}

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  va 2  s . c  o  m
    System.out.println("Auth failed");
    return null;
}

From source file:com.utest.domain.service.BaseDomainServiceIntegrationTest.java

protected void loginUser(final User user) throws Exception {
    final List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
    final AuthenticatedUserInfo authUser = new AuthenticatedUserInfo(user.getId(), user.getFullName());
    final Authentication auth = new UsernamePasswordAuthenticationToken(authUser, null, grantedAuthorities);
    final SecurityContext ctx = new SecurityContextImpl();
    ctx.setAuthentication(auth);/*  w  ww. j  a  v a  2 s.c  o m*/
    SecurityContextHolder.setContext(ctx);
}

From source file:com.example.server.user.CustomUserDetailsService.java

private Authentication authenticate(User account) {
    return new UsernamePasswordAuthenticationToken(createUser(account), null,
            Collections.singleton(createAuthority(account)));
}

From source file:org.cloudfoundry.identity.uaa.login.UsernamePasswordExtractingAuthenticationManagerTests.java

@Test
public void testUsernamePassword() {
    Authentication expected = new UsernamePasswordAuthenticationToken("bar", "foo",
            AuthorityUtils.commaSeparatedStringToAuthorityList("USER"));
    Authentication output = manager.authenticate(expected);
    assertSame(expected, output);//  w w  w  .  j av a  2  s. c  o  m
}

From source file:components.OAuth2AuthenticationReadConverter.java

@Override
public OAuth2Authentication convert(DBObject source) {
    DBObject storedRequest = (DBObject) source.get("storedRequest");
    OAuth2Request oAuth2Request = new OAuth2Request(
            (Map<String, String>) storedRequest.get("requestParameters"),
            (String) storedRequest.get("clientId"), null, true, new HashSet((List) storedRequest.get("scope")),
            null, null, null, null);//from ww  w . java  2  s .co  m
    DBObject userAuthorization = (DBObject) source.get("userAuthentication");
    Object principal = getPrincipalObject(userAuthorization.get("principal"));
    Authentication userAuthentication = new UsernamePasswordAuthenticationToken(principal,
            (String) userAuthorization.get("credentials"),
            getAuthorities((List) userAuthorization.get("authorities")));
    OAuth2Authentication authentication = new OAuth2Authentication(oAuth2Request, userAuthentication);
    return authentication;
}