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

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

Introduction

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

Prototype

public UsernameNotFoundException(String msg) 

Source Link

Document

Constructs a UsernameNotFoundException with the specified message.

Usage

From source file:br.com.joaops.smt.service.SystemUserDetailsServiceImpl.java

@SuppressWarnings("unchecked")
@Transactional(readOnly = true)//from www .  j  a v a 2s  .c  o  m
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    UserDetails user = null;
    SystemUserDto userDto = systemUserService.getUserByEmail(username);
    if (userDto == null) {
        String message = "";
        try {
            message = messageSource.getMessage("DigestAuthenticationFilter.usernameNotFound",
                    new Object[] { username }, LocaleContextHolder.getLocale());
        } catch (Exception e) {
            message = "Erro ao Conectar!!!";
        }
        UsernameNotFoundException ex = new UsernameNotFoundException(message);
        throw ex;
    } else {
        user = new SmtUserDetails(userDto);
        Collection<SimpleGrantedAuthority> authorities = (Collection<SimpleGrantedAuthority>) user
                .getAuthorities();
        Collection<SystemUserPermissionDto> permissions = userDto.getSystemUserPermission();
        for (SystemUserPermissionDto permission : permissions) {
            SystemModuleDto module = permission.getSystemUserPermissionId().getSystemModule();
            if (permission.getRead()) {
                authorities.add(createRole(module, "READ"));
            }
            if (permission.getAdd()) {
                authorities.add(createRole(module, "ADD"));
            }
            if (permission.getEdit()) {
                authorities.add(createRole(module, "EDIT"));
            }
            if (permission.getDelete()) {
                authorities.add(createRole(module, "DELETE"));
            }
        }
    }
    return user;
}

From source file:org.n52.oss.ui.services.OSSAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication arg0) throws AuthenticationException {
    String username = arg0.getName();
    String password = arg0.getCredentials().toString();

    AuthToken token = authenticateOSS(username, password);

    if (token.auth_token != null) {
        if (!token.isValid)
            throw new UsernameNotFoundException(
                    "Username is not validated please contact site administration!");

        final List<GrantedAuthority> grantedAuths = new ArrayList<GrantedAuthority>();
        grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
        grantedAuths.add(new SimpleGrantedAuthority("ROLE_SCRIPT_AUTHOR"));

        if (token.isAdmin)
            grantedAuths.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        final UserDetails principal = new User(username, token.auth_token, grantedAuths);
        final Authentication auth = new UsernamePasswordAuthenticationToken(principal, token.auth_token,
                grantedAuths);//from  ww  w.j  a  v a 2 s.c o m
        return auth;

    } else
        throw new UsernameNotFoundException("Wrong username/password combination");
}

From source file:com.wwpass.springsecurity.authentication.WwpassAuthenticationProvider.java

public Authentication authenticate(Authentication authentication) throws AuthenticationException {

    if (!supports(authentication.getClass())) {
        return null;
    }//from   w  w w .j  a  v a 2 s . com

    if (authentication instanceof WwpassAuthenticationToken) {
        WwpassAuthenticationToken token = (WwpassAuthenticationToken) authentication;

        String puid = token.getPuid();

        UserDetails user = null;
        if (puid != null) {
            user = userDetailsService.loadUserDetails(token);
        }
        if (user == null) {
            throw new UsernameNotFoundException("PUID not found.");
        }
        Collection<? extends GrantedAuthority> authorities = AuthorityUtils
                .commaSeparatedStringToAuthorityList(getWwpassUserRole());

        return new WwpassAuthenticationToken(user, puid, authorities);
    }

    return null;
}

From source file:org.bwgz.swim.openlane.service.OpenLaneUserDetailsServiceImpl.java

public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
    log.debug(String.format("loading user with name: %s", username));

    OpenLaneUser user = findOpenLaneUser(username);
    if (user == null) {
        log.error(String.format("cannot find user with name: %s", username));
        throw new UsernameNotFoundException(username);
    }//from   w w w.  j  a  v  a 2  s  . c o  m

    return user;
}

From source file:org.xeneo.db.security.JdbcUserManager.java

public JdbcUser loadUserByUsername(String userURI) throws UsernameNotFoundException {
    List<JdbcUser> users = loadUsersByUsername(userURI);
    if (users.size() < 1) {
        throw new UsernameNotFoundException("There is no user with UserURI: " + userURI + " registered.");
    }//  w  ww  .j a  va  2s.  c  om

    return users.get(0);
}

From source file:uk.org.rbc1b.roms.security.ROMSUserDetailsService.java

@Override
public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {

    final User user = userDao.findUser(userName);
    if (user == null) {
        throw new UsernameNotFoundException("Failed to find user [" + userName + "]");
    }//w  w w.  j av a  2s. c o m

    if (!user.isActive()) {
        throw new ForbiddenRequestException("User #" + user.getPersonId() + " is not active");
    }

    List<ApplicationAccess> userAccessList = applicationAccessDao.findUserPermissions(user.getPersonId());

    final Map<Application, ROMSGrantedAuthority> authorityMap = new HashMap<Application, ROMSGrantedAuthority>();
    for (ApplicationAccess access : userAccessList) {
        ROMSGrantedAuthority authority = new ROMSGrantedAuthority();
        authority.setApplication(Application.valueOf(access.getApplication().getCode()));
        authority.setDepartmentLevelAccess(AccessLevel.findAccessLevel(access.getDepartmentAccess()));
        authority.setNonDepartmentLevelAccess(AccessLevel.findAccessLevel(access.getNonDepartmentAccess()));
        authorityMap.put(Application.valueOf(access.getApplication().getCode()), authority);
    }

    return new ROMSUserDetails() {
        private static final long serialVersionUID = -2342863582753427493L;

        @Override
        public ROMSGrantedAuthority findAuthority(Application application) {
            return authorityMap.get(application);
        }

        @Override
        public Collection<? extends GrantedAuthority> getAuthorities() {
            return authorityMap.values();
        }

        @Override
        public String getPassword() {
            return user.getPassword();
        }

        @Override
        public String getUsername() {
            return user.getUserName();
        }

        @Override
        public Integer getUserId() {
            return user.getPersonId();
        }

        @Override
        public boolean isAccountNonExpired() {
            return true;
        }

        @Override
        public boolean isAccountNonLocked() {
            return true;
        }

        @Override
        public boolean isCredentialsNonExpired() {
            return true;
        }

        @Override
        public boolean isEnabled() {
            return true;
        }
    };
}

From source file:se.kth.csc.auth.UserService.java

@Transactional
@Override/*  w w w . j a  v  a2  s .  c  o m*/
public UserDetails loadUserDetails(Authentication token) throws UsernameNotFoundException {
    if (!token.getName().startsWith("u1")) {
        // See http://intra.kth.se/it/driftsinformation-webbtjanster/anstallda/inloggning-maste-ske-med-sma-bokstaver-1.475521
        // which allows an exploit. Counter-measured by only allowing usernames starting with "u1"
        throw new UsernameNotFoundException("This username is not in the u1 realm and was probably forged");
    }

    Account account = accountStore.fetchAccountWithPrincipalName(token.getName());

    if (account == null) {
        account = new Account();
        account.setPrincipalName(token.getName());
        for (GrantedAuthority grantedAuthority : token.getAuthorities()) {
            if (Role.ADMIN.getAuthority().equals(grantedAuthority.getAuthority())) {
                account.setAdmin(true);
                break;
            }
        }
        accountStore.storeAccount(account);

        log.info("Created user called \"{}\" with id {} and principal {}", account.getName(), account.getId(),
                account.getPrincipalName());
    }
    String name = nameService.nameUser(token.getName());

    if (account.getName() == null || !account.getName().equals(name)) {
        account.setName(name);
        log.info("User with id {} and principal {} is now called \"{}\"", account.getId(),
                account.getPrincipalName(), name);
    }

    return createUser(account);
}

From source file:cz.lbenda.coursing.server.user.UserServiceImpl.java

@Override
@Transactional(readOnly = true)//from  w  w w.  j a  v  a 2 s  .  c  o  m
public final UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    UserImpl user = repository.findByUsername(username);
    if (user == null && FIRST_USERNAME.equals(username) && count() == 0) { // This is check if database is void, then first admin user is created
        user = firstAdmin();
        LOG.info("The database is void so new user si created");
    }
    if (user == null) {
        throw new UsernameNotFoundException(username);
    }
    return new UserDetailsImpl(user);
}

From source file:de.zib.gndms.gndms.security.GridMapUserDetailsService.java

@Override
public UserDetails loadUserByUsername(final String dn) throws UsernameNotFoundException {

    List<GrantedAuthority> authorityList = new ArrayList<GrantedAuthority>(1);
    // search admin
    boolean isUser = true;
    try {/* w  ww. j  a va  2 s  .  com*/
        if (searchInGridMapfile(adminGridMapfileName, dn)) {
            authorityList.add(adminRole());
        } else if (searchInGridMapfile(gridMapfileName, dn))
            authorityList.add(userRole());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    if (isUser && authorityList.size() == 0)
        throw new UsernameNotFoundException("DN not permitted: " + dn);

    GNDMSUserDetails userDetails = new GNDMSUserDetails();
    userDetails.setAuthorities(authorityList);
    userDetails.setDn(dn);
    userDetails.setIsUser(isUser);

    return userDetails;
}

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 {// w ww .ja  v  a2 s .  co  m
        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);
        }

    }

}