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, boolean enabled, boolean accountNonExpired,
        boolean credentialsNonExpired, boolean accountNonLocked,
        Collection<? extends GrantedAuthority> authorities) 

Source Link

Document

Construct the User with the details required by org.springframework.security.authentication.dao.DaoAuthenticationProvider .

Usage

From source file:tr.edu.gsu.peralab.mobilesensing.web.service.CustomUserDetailsService.java

public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

    tr.edu.gsu.peralab.mobilesensing.web.entity.User domainUser = userDao.retrieveUser(username);

    User user = null;/*from w ww.j  a va2 s.co  m*/
    boolean enabled = true;
    boolean accountNonExpired = true;
    boolean credentialsNonExpired = true;
    boolean accountNonLocked = true;

    if (domainUser != null) {
        user = new User(domainUser.getUserName(), domainUser.getPassword().toLowerCase(), enabled,
                accountNonExpired, credentialsNonExpired, accountNonLocked,
                getAuthorities(domainUser.getRights()));
    }
    return user;
}

From source file:com.redhat.rhtracking.web.domain.SecureUser.java

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    Map<String, Object> request = new HashMap<>();
    request.put("username", username);
    Map<String, Object> response = service.findByUsername(request);

    if (response.get("status") != EventStatus.SUCCESS)
        throw new UsernameNotFoundException("User not found");

    Collection<GrantedAuthority> roles = new ArrayList<>();
    for (String s : (List<String>) response.get("roles")) {
        roles.add(new SimpleGrantedAuthority(s));
    }/* w  w  w . j  a  va2 s.  c  o  m*/

    return new User((String) response.get("username"), (String) response.get("password"),
            (boolean) response.get("enabled"), (boolean) response.get("accountNonExpired"),
            (boolean) response.get("credentialsNonExpired"), (boolean) response.get("accountNonLocked"), roles);
}

From source file:de.tudarmstadt.ukp.clarin.webanno.webapp.security.AnyUserDetailsService.java

@Override
public UserDetails loadUserByUsername(String aUsername) throws UsernameNotFoundException, DataAccessException {
    de.tudarmstadt.ukp.clarin.webanno.model.User user = projectRepository.getUser(aUsername);

    List<Authority> authorityList = projectRepository.listAuthorities(user);

    List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
    for (Authority authority : authorityList) {
        authorities.add(new SimpleGrantedAuthority(authority.getAuthority()));
    }/*w  ww .j a  va 2 s.  c  o  m*/
    return new User(user.getUsername(), user.getPassword(), true, true, true, true, authorities);
}

From source file:com.callcenter.service.UserService.java

@Override
public UserDetails loadUserByUsername(final String userName)
        throws UsernameNotFoundException, DataAccessException {
    final com.callcenter.domain.User user = com.callcenter.domain.User.findUserByName(userName);
    if (user == null)
        throw new UsernameNotFoundException("User with the name: " + userName + " was not found");

    final ArrayList<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();

    for (final String authorityName : user.getAuthorities()) {
        grantedAuthorities.add(new GrantedAuthorityImpl(authorityName));
    }/*ww  w. j av  a 2s  . c o  m*/
    return new User(user.getName(), user.getPassword(), true, true, true, true, grantedAuthorities);
}

From source file:org.springframework.security.jackson2.UserDeserializer.java

/**
 * This method will create {@link User} object. It will ensure successful object creation even if password key is null in
 * serialized json, because credentials may be removed from the {@link User} by invoking {@link User#eraseCredentials()}.
 * In that case there won't be any password key in serialized json.
 *//*  ww  w.ja v a 2s.  c  o  m*/
@Override
public User deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    ObjectMapper mapper = (ObjectMapper) jp.getCodec();
    JsonNode jsonNode = mapper.readTree(jp);
    Set<GrantedAuthority> authorities = mapper.convertValue(jsonNode.get("authorities"),
            new TypeReference<Set<SimpleGrantedAuthority>>() {
            });
    return new User(readJsonNode(jsonNode, "username").asText(), readJsonNode(jsonNode, "password").asText(),
            readJsonNode(jsonNode, "enabled").asBoolean(),
            readJsonNode(jsonNode, "accountNonExpired").asBoolean(),
            readJsonNode(jsonNode, "credentialsNonExpired").asBoolean(),
            readJsonNode(jsonNode, "accountNonLocked").asBoolean(), authorities);
}

From source file:com.px100systems.data.browser.controller.DbBrowserUserDetailsService.java

/**
 * Find user by name/*from  ww w  .ja v a  2s .  c  om*/
 * @param userName user name
 * @return user authentication details
 * @throws UsernameNotFoundException
 */
@Override
public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
    String password = getUsers().getProperty(userName);
    if (password == null || password.isEmpty())
        throw new UsernameNotFoundException("User not found");

    List<SimpleGrantedAuthority> authorities = new ArrayList<SimpleGrantedAuthority>();
    authorities.add(new SimpleGrantedAuthority(DEFAULT_AUTHORITY));

    return new User(userName, password, true, true, true, true, authorities);
}

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

/**
 * Get a clone of the {@link User}./*from   w  w w  .  j a  v a2  s .  c  o  m*/
 *
 * @param user
 *            the {@link User} to clone.
 * @return the clone.
 */
private User clone(final User user) {
    User c = null;
    if (user != null) {
        c = new User(user.getUsername(), user.getPassword(), user.isEnabled(), user.isAccountNonExpired(),
                user.isCredentialsNonExpired(), user.isAccountNonLocked(), user.getAuthorities());
    }
    return c;
}

From source file:org.mitre.openid.connect.service.ClientUserDetailsService.java

@Override
public UserDetails loadUserByUsername(String clientId) throws UsernameNotFoundException, DataAccessException {

    ClientDetails client = clientDetailsService.loadClientByClientId(clientId);

    String password = client.getClientSecret();
    boolean enabled = true;
    boolean accountNonExpired = true;
    boolean credentialsNonExpired = true;
    boolean accountNonLocked = true;
    List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
    GrantedAuthority roleClient = new SimpleGrantedAuthority("ROLE_CLIENT");
    authorities.add(roleClient);/*from www.  j av a2  s  .  co m*/

    return new User(clientId, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked,
            authorities);

}

From source file:org.red5.webapps.admin.controllers.RegisterUserController.java

public ModelAndView onSubmit(Object command) throws ServletException {
    log.debug("onSubmit {}", command);
    UserDetails userDetails = (UserDetails) command;
    String username = userDetails.getUsername();
    String password = userDetails.getPassword();
    log.debug("User details: username={} password={}", username, password);
    try {/*from w ww  .ja  v  a2 s .com*/
        // register user here
        if (!((JdbcUserDetailsManager) userDetailsService).userExists(username)) {
            GrantedAuthority[] auths = new GrantedAuthority[1];
            auths[0] = new GrantedAuthorityImpl("ROLE_SUPERVISOR");
            User usr = new User(username, password, true, true, true, true, auths);
            ((JdbcUserDetailsManager) userDetailsService).createUser(usr);
            if (((JdbcUserDetailsManager) userDetailsService).userExists(username)) {
                //setup security user stuff and add them to the current "cache" and current user map   
                daoAuthenticationProvider.getUserCache().putUserInCache(usr);
            } else {
                log.warn("User registration failed for: {}", username);
            }
        } else {
            log.warn("User {} already exists", username);
        }
    } catch (Exception e) {
        log.error("Error during registration", e);
    }
    return new ModelAndView(new RedirectView(getSuccessView()));
}

From source file:uk.co.threeonefour.ifictionary.web.security.service.UserDetailsServiceImpl.java

private User buildUserFromUserEntity(uk.co.threeonefour.ifictionary.web.user.model.User userEntity) {
    // convert model user to spring security user
    String username = userEntity.getUserId();
    String password = userEntity.getPassword();
    boolean enabled = true;
    boolean accountNonExpired = true;
    boolean credentialsNonExpired = true;
    boolean accountNonLocked = true;
    Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
    Collection<Role> roles = userEntity.getRoles();
    for (Role role : roles) {
        authorities.add(new SimpleGrantedAuthority(role.name()));
    }/*w  ww.  j  a v a2  s . c o m*/

    User springUser = new User(username, password, enabled, accountNonExpired, credentialsNonExpired,
            accountNonLocked, authorities);
    return springUser;
}