Example usage for org.springframework.security.core.userdetails User User

List of usage examples for org.springframework.security.core.userdetails User User

Introduction

In this page you can find the example usage for org.springframework.security.core.userdetails User User.

Prototype

public User(String username, String password, Collection<? extends GrantedAuthority> authorities) 

Source Link

Document

Calls the more complex constructor with all boolean arguments set to true .

Usage

From source file:org.apigw.appmanagement.revision.ApplicationManagementRevisionListenerTest.java

private SecurityContext createSecurityContext(final String username, String... roles) {
    final List<SimpleGrantedAuthority> authorities = new ArrayList<>();
    for (String role : roles) {
        authorities.add(new SimpleGrantedAuthority(role));
    }// w ww  .  j  a va2 s.c  om
    return new SecurityContext() {
        @Override
        public Authentication getAuthentication() {
            return new UsernamePasswordAuthenticationToken(new User(username, "", authorities), null);
        }

        @Override
        public void setAuthentication(Authentication authentication) {

        }
    };
}

From source file:fr.mycellar.interfaces.web.security.MyCellarAuthenticationProvider.java

@Override
protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication)
        throws AuthenticationException {
    if (StringUtils.isBlank(username)) {
        throw new UsernameNotFoundException("Username is empty.");
    }//from  ww w  .j  a v  a  2  s.  com
    logger.debug("Security verification for username '{}'.", username);

    fr.mycellar.domain.user.User user = userServiceFacade.getUserByEmail(username);
    if (user == null) {
        throw new UsernameNotFoundException("Username '" + username + "' not found.");
    }
    return new User(user.getEmail(), user.getPassword(), getAuthoritiesFromProfile(user.getProfile()));
}

From source file:org.socialsignin.exfmproxy.mvc.workaround.auth.WorkaroundExFmUserDetailsService.java

public UserDetails loadUserByUsername(String workaroundUserName) throws UsernameNotFoundException {

    String[] usernameAndPassword = workaroundUserName
            .split(WorkaroundUsernamePasswordAuthenticationFilter.USERNAME_PASSWORD_DELIMITER);
    if (usernameAndPassword.length != 2) {
        throw new UsernameNotFoundException(workaroundUserName);
    } else {//from w  ww . jav a 2s.c  om
        String username = usernameAndPassword[0];
        String password = usernameAndPassword[1];
        ExFmTemplate exFmTemplate = new ExFmTemplate(baseUrl, username, password);
        ExFmProfile exfmProfile = null;
        try {
            exfmProfile = exFmTemplate.meOperations().getUserProfile();
        } catch (NotAuthorizedException e) {

        }
        if (exfmProfile != null) {
            List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
            authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
            return new User(workaroundUserName, password, authorities);
        } else {
            throw new UsernameNotFoundException(username);
        }

    }

}

From source file:com.qpark.eip.core.spring.auth.DatabaseUserProvider.java

/**
 * Map the {@link AuthenticationType} to a {@link User}.
 *
 * @param auth/* ww w .j a  v  a2s. c  o  m*/
 *            the {@link AuthenticationType}.
 * @return the {@link User}.
 */
private User getUser(final AuthenticationType auth) {
    List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(auth.getGrantedAuthority().size() + 1);
    authorities.add(new SimpleGrantedAuthority("ROLE_ANONYMOUS"));
    for (GrantedAuthorityType grantedAuthority : auth.getGrantedAuthority()) {
        authorities.add(new SimpleGrantedAuthority(grantedAuthority.getRoleName()));
    }
    User u = new User(auth.getUserName(), auth.getPassword(), authorities);
    return u;
}

From source file:io.gravitee.management.idp.memory.authentication.InMemoryAuthentificationProvider.java

@Override
public org.springframework.security.authentication.AuthenticationProvider configure() throws Exception {

    boolean found = true;
    int userIdx = 0;

    while (found) {
        String user = environment.getProperty("users[" + userIdx + "].user");
        found = (user != null && user.isEmpty());

        if (found) {
            String username = environment.getProperty("users[" + userIdx + "].username");
            String password = environment.getProperty("users[" + userIdx + "].password");
            String roles = environment.getProperty("users[" + userIdx + "].roles");
            List<GrantedAuthority> authorities = AuthorityUtils.commaSeparatedStringToAuthorityList(roles);
            userIdx++;/*from w  ww  .ja  v  a  2 s . c  o  m*/

            User newUser = new User(username, password, authorities);
            LOGGER.debug("Add an in-memory user: {}", newUser);
            userDetailsService.createUser(newUser);
        }
    }

    return this;
}

From source file:sample.contact.service.impl.UserServiceImpl.java

@Override
public void createUserWithAuthority(String username, String password, String authority) {
    if (!jdbcUserDetailsManager.userExists(username)) {
        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        grantedAuthorities.add(new SimpleGrantedAuthority(authority));
        UserDetails userDetails = new User(username, password, grantedAuthorities);
        jdbcUserDetailsManager.createUser(userDetails);
    }// w w  w. ja va2 s.co m
}

From source file:org.opentides.util.SecurityUtilTest.java

@Test
public void testGetUser() {
    List<GrantedAuthority> auths = new ArrayList<>();
    auths.add(new SimpleGrantedAuthority("ROLE1"));
    auths.add(new SimpleGrantedAuthority("ROLE2"));

    UserDetails userDetails = new User("admin", "password", auths);
    SessionUser sessionUser = new SessionUser(userDetails);
    UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(sessionUser,
            null, auths);/*from  w  ww.ja  va2 s.co  m*/
    SecurityContextHolder.getContext().setAuthentication(authentication);

    SecurityUtil securityUtil = new SecurityUtil();
    SessionUser actual = securityUtil.getUser();
    assertNotNull(actual);
    assertEquals("admin", actual.getUsername());
}

From source file:com.netflix.genie.web.security.x509.X509UserDetailsService.java

/**
 * {@inheritDoc}//from  w  w w. j  a  v a2s  .  c  o  m
 */
@Override
public UserDetails loadUserDetails(final PreAuthenticatedAuthenticationToken token)
        throws UsernameNotFoundException {
    log.debug("Entering loadUserDetails with token {}", token);

    // Assuming format of the principal is {username}:{role1,role2....}
    final Object principalObject = token.getPrincipal();
    if (principalObject == null || !(principalObject instanceof String)) {
        throw new UsernameNotFoundException("Expected principal to be a String");
    }

    final String principal = (String) principalObject;
    final String[] usernameAndRoles = principal.split(":");
    if (usernameAndRoles.length != 2) {
        throw new UsernameNotFoundException(
                "User and roles not found. Must be in format {user}:{role1,role2...}");
    }

    final String username = usernameAndRoles[0];
    final String[] roles = usernameAndRoles[1].split(",");
    if (roles.length == 0) {
        throw new UsernameNotFoundException("No roles found. Unable to authenticate");
    }

    // If the certificate is valid the client is at the least a valid user
    final Set<GrantedAuthority> authorities = Sets.newHashSet(USER_AUTHORITY);
    for (final String role : roles) {
        authorities.add(new SimpleGrantedAuthority(ROLE_PREFIX + role.toUpperCase()));
    }

    final User user = new User(username, "NA", authorities);
    log.info("User {} authenticated via client certificate", user);
    return user;
}

From source file:reconf.server.services.security.UpsertUserService.java

private HttpStatus upsert(Client client, List<GrantedAuthority> authorities) {
    HttpStatus status;/*from   w  ww  . j  a  v a2  s. co  m*/

    User user = new User(client.getUsername(), client.getPassword(), authorities);
    if (userDetailsManager.userExists(client.getUsername())) {
        userDetailsManager.updateUser(user);
        status = HttpStatus.OK;
    } else {
        userDetailsManager.createUser(user);
        status = HttpStatus.CREATED;
    }
    return status;
}