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

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

Introduction

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

Prototype

public String getUsername() 

Source Link

Usage

From source file:tn.rnu.isi.gestioninscription.application.LoginManagedBean.java

public void checkSessionInfo() {
    if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() != "anonymousUser") {
        org.springframework.security.core.userdetails.User userInf = (org.springframework.security.core.userdetails.User) SecurityContextHolder
                .getContext().getAuthentication().getPrincipal();
        userAccount = getService().getUsersRepository().findByEmail((String) userInf.getUsername());
        me = getEtudiantService().getEtudiantRepository().findByEmail((String) userInf.getUsername());
    } else {//from   w  w w.j a v a2  s  .c o  m
        FacesContext fContext = FacesContext.getCurrentInstance();
        ExternalContext extContext = fContext.getExternalContext();
        try {
            extContext.redirect(extContext.getRequestContextPath() + "/login.xhtml?faces-redirect=true");
        } catch (Exception eax) {
            Logger.getLogger(LoginManagedBean.class.getName())
                    .info(Level.SEVERE + " Exception msg: " + eax.getMessage());
        }
    }
}

From source file:org.parancoe.plugins.securityevolution.ParancoeUserDetailsService.java

@Override
public UserDetails loadUserByUsername(java.lang.String username)
        throws UsernameNotFoundException, DataAccessException {

    org.parancoe.plugins.securityevolution.User user = userDao.findByUsername(username);

    if (user == null)
        throw new UsernameNotFoundException("username " + username + " not found in the system");

    List<Authority> authorities = authorityDao.findAllAuthoritiesAssociatedToUsername(username);
    return new User(user.getUsername(), user.getPassword(), user.isEnabled(), true, true, !user.isLocked(),
            authorities2GrantedAuthorities(authorities));
}

From source file:com.ar.dev.tierra.api.controller.UsuariosController.java

@RequestMapping(value = "/updateUsuario", method = RequestMethod.POST)
public ResponseEntity<?> uploadFile(@RequestBody Usuarios usuario, OAuth2Authentication authentication) {
    try {/*  w  w  w  . j a  v  a  2 s  .  c o  m*/
        User user = (User) authentication.getPrincipal();
        Usuarios u = facadeService.getUsuariosDAO().findUsuarioByUsername(user.getUsername());
        if (u.getIdUsuario() == usuario.getIdUsuario() && u.getUsername().equals(usuario.getUsername())
                && u.getRoles().getNombreRol().equals(usuario.getRoles().getNombreRol())
                && u.getRoles().getIdRol() == usuario.getRoles().getIdRol()) {
            usuario.setIdUsuarioModificacion(u.getIdUsuario());
            usuario.setFechaModificacion(new Date());
            facadeService.getUsuariosDAO().updateUsuario(usuario);
        } else {
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }
    } catch (Exception e) {
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    }
    return new ResponseEntity<>(HttpStatus.OK);
}

From source file:org.schedoscope.metascope.service.MetascopeUserService.java

public void logoutUser(SessionRegistry sessionRegistry, String username) {
    final List<Object> allPrincipals = sessionRegistry.getAllPrincipals();
    for (final Object principal : allPrincipals) {
        if (principal instanceof User) {
            User springUserDetails = (User) principal;
            if (springUserDetails.getUsername().equals(username)) {
                for (SessionInformation sessionInformation : sessionRegistry.getAllSessions(principal, true)) {
                    sessionInformation.expireNow();
                }/* w w w. j av  a 2 s  .c om*/
            }
        }
    }
}

From source file:com.ar.dev.tierra.api.controller.UsuariosController.java

@RequestMapping(value = "/updatePhoto", method = RequestMethod.POST)
public @ResponseBody ResponseEntity<?> updateUsuario(@RequestParam("file") MultipartFile file,
        OAuth2Authentication authentication) {
    try {/*from  ww w.java2 s . co  m*/
        User user = (User) authentication.getPrincipal();
        Usuarios u = facadeService.getUsuariosDAO().findUsuarioByUsername(user.getUsername());
        if (file.getName().isEmpty() == false) {
            InputStream inputStream = file.getInputStream();
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            int nRead;
            byte[] bytes = new byte[16384];
            while ((nRead = inputStream.read(bytes, 0, bytes.length)) != -1) {
                buffer.write(bytes, 0, nRead);
            }
            buffer.flush();
            u.setImagen(buffer.toByteArray());
            facadeService.getUsuariosDAO().updateUsuario(u);
        }
    } catch (Exception e) {
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    }
    return new ResponseEntity<>(HttpStatus.OK);
}

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;/* w ww .ja va  2 s .c  o 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:org.pac4j.cas.client.CasClientWrapper.java

protected CasWrapperCredentials retrieveCredentials(final WebContext wc) throws RequiresHttpAction {

    J2EContext jc = (J2EContext) wc;//from  w ww .j a v a  2  s. com
    HttpServletRequest request = jc.getRequest();
    HttpServletResponse response = jc.getResponse();

    org.springframework.security.core.Authentication authentication = null;

    try {

        authentication = casFilterCas.attemptAuthentication(request, response);

    } catch (AuthenticationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) authentication
            .getPrincipal();

    CasWrapperCredentials casCredentials = new CasWrapperCredentials(user.getUsername(),
            this.getClass().getSimpleName(), authentication);

    return casCredentials;

}

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

@Override
public UserDetails loadUserByUsername(String string) throws UsernameNotFoundException {
    User user = dao.findByUserName(string);
    if (user == null) {
        throw new UsernameNotFoundException("user with name " + string + " not found");
    }// w w  w.  ja v  a 2s  . c o  m
    GrantedAuthority authority = new SimpleGrantedAuthority(user.getRole());
    List<GrantedAuthority> list = new ArrayList<>();
    list.add(authority);
    return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), list);
}

From source file:org.fracturedatlas.athena.helper.lock.manager.AthenaLockManager.java

private String getCurrentUsername() {
    Authentication authentication = contextHolderStrategy.getContext().getAuthentication();
    if (authentication != null && authentication.getPrincipal() != null
            && User.class.isAssignableFrom(authentication.getPrincipal().getClass())) {
        User user = (User) authentication.getPrincipal();
        return user.getUsername();
    }//from  w ww .j a  va  2  s  .c om

    return NO_USER;
}

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()));
    }//from w  w  w .j  a v a 2s .  com
    return new User(user.getUsername(), user.getPassword(), true, true, true, true, authorities);
}