Search in sources :

Example 1 with UsernameNotFoundException

use of org.springframework.security.core.userdetails.UsernameNotFoundException in project head by mifos.

the class LoginServiceFacadeWebTier method login.

@Override
public LoginDto login(String username, String password) {
    PersonnelBO user = this.personnelDao.findPersonnelByUsername(username);
    if (user == null) {
        throw new UsernameNotFoundException(LoginConstants.KEYINVALIDUSER);
    }
    MifosConfigurationManager configMgr = MifosConfigurationManager.getInstance();
    List<ValueListElement> localeList = personnelServiceFacade.getDisplayLocaleList();
    Locale preferredLocale = new Locale(configMgr.getString(LANGUAGE_CODE), configMgr.getString(COUNTRY_CODE));
    String listElement = "[" + preferredLocale.toString() + "]";
    Short localeId = user.getPreferredLocale();
    for (ValueListElement element : localeList) {
        if (element.getName().contains(listElement)) {
            localeId = element.getId().shortValue();
            break;
        }
    }
    user.setPreferredLocale(localeId);
    UserContext userContext = new UserContext();
    userContext.setPreferredLocale(preferredLocale);
    userContext.setLocaleId(localeId);
    userContext.setId(user.getPersonnelId());
    userContext.setName(user.getDisplayName());
    userContext.setLevel(user.getLevelEnum());
    userContext.setRoles(user.getRoles());
    userContext.setLastLogin(user.getLastLogin());
    userContext.setPasswordChanged(user.getPasswordChanged());
    userContext.setBranchId(user.getOffice().getOfficeId());
    userContext.setBranchGlobalNum(user.getOffice().getGlobalOfficeNum());
    userContext.setOfficeLevelId(user.getOffice().getLevel().getId());
    user.updateDetails(userContext);
    try {
        this.transactionHelper.startTransaction();
        this.transactionHelper.beginAuditLoggingFor(user);
        user.login(password);
        this.personnelDao.save(user);
        this.transactionHelper.commitTransaction();
        boolean isPasswordExpired = new LocalDate(user.getPasswordExpirationDate()).isBefore(new LocalDate());
        return new LoginDto(user.getPersonnelId(), user.getOffice().getOfficeId(), user.isPasswordChanged(), isPasswordExpired);
    } catch (ApplicationException e) {
        this.transactionHelper.rollbackTransaction();
        throw new BusinessRuleException(e.getKey(), e);
    } catch (Exception e) {
        this.transactionHelper.rollbackTransaction();
        throw new MifosRuntimeException(e);
    } finally {
        this.transactionHelper.closeSession();
    }
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) Locale(java.util.Locale) UserContext(org.mifos.security.util.UserContext) LocalDate(org.joda.time.LocalDate) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) BusinessRuleException(org.mifos.service.BusinessRuleException) MifosRuntimeException(org.mifos.core.MifosRuntimeException) PersonnelException(org.mifos.customers.personnel.exceptions.PersonnelException) ApplicationException(org.mifos.framework.exceptions.ApplicationException) MifosConfigurationManager(org.mifos.config.business.MifosConfigurationManager) BusinessRuleException(org.mifos.service.BusinessRuleException) ApplicationException(org.mifos.framework.exceptions.ApplicationException) PersonnelBO(org.mifos.customers.personnel.business.PersonnelBO) LoginDto(org.mifos.dto.domain.LoginDto) ValueListElement(org.mifos.dto.domain.ValueListElement) MifosRuntimeException(org.mifos.core.MifosRuntimeException)

Example 2 with UsernameNotFoundException

use of org.springframework.security.core.userdetails.UsernameNotFoundException in project Asqatasun by Asqatasun.

the class LoginController method displayDemoPage.

@RequestMapping(value = TgolKeyStore.DEMO_URL, method = RequestMethod.GET)
public String displayDemoPage(HttpServletRequest request, HttpServletResponse response, Model model) {
    Locale locale = localeResolver.resolveLocale(request);
    String languageKey = locale.getLanguage().toLowerCase();
    String lGuestUser = null;
    if (guestListByLang.containsKey(languageKey)) {
        lGuestUser = guestListByLang.get(languageKey);
    } else if (guestListByLang.containsKey("default")) {
        lGuestUser = guestListByLang.get("default");
    }
    if (StringUtils.isBlank(lGuestUser) || StringUtils.isBlank(guestPassword)) {
        return TgolKeyStore.NO_DEMO_AVAILABLE_VIEW_NAME;
    }
    if (isAuthenticated()) {
        return TgolKeyStore.ACCESS_DENIED_VIEW_NAME;
    }
    if (guestUserDetails == null) {
        try {
            guestUserDetails = tgolUserDetailsService.loadUserByUsername(lGuestUser);
        } catch (UsernameNotFoundException unfe) {
            return TgolKeyStore.NO_DEMO_AVAILABLE_VIEW_NAME;
        }
    }
    doGuestAutoLogin(request, lGuestUser);
    if (forbiddenLangForOnlineDemo.contains(languageKey)) {
        return TgolKeyStore.HOME_VIEW_REDIRECT_NAME;
    }
    Collection<Contract> contractSet = getContractDataService().getAllContractsByUser(getCurrentUser());
    if (contractSet == null || contractSet.isEmpty()) {
        return TgolKeyStore.NO_DEMO_AVAILABLE_VIEW_NAME;
    }
    String contractId = contractSet.iterator().next().getId().toString();
    model.addAttribute(TgolKeyStore.CONTRACT_ID_KEY, contractId);
    return TgolKeyStore.AUDIT_PAGE_SET_UP_REDIRECT_NAME;
}
Also used : Locale(java.util.Locale) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) Contract(org.asqatasun.webapp.entity.contract.Contract) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with UsernameNotFoundException

use of org.springframework.security.core.userdetails.UsernameNotFoundException in project Activiti by Activiti.

the class IdentityServiceUserDetailsService method loadUserByUsername.

@Override
public UserDetails loadUserByUsername(String userId) throws UsernameNotFoundException {
    User user = null;
    try {
        user = this.identityService.createUserQuery().userId(userId).singleResult();
    } catch (ActivitiException ex) {
    // don't care
    }
    if (null == user) {
        throw new UsernameNotFoundException(String.format("user (%s) could not be found", userId));
    }
    // if the results not null then its active...
    boolean active = true;
    // get the granted authorities
    List<GrantedAuthority> grantedAuthorityList = new ArrayList<GrantedAuthority>();
    List<Group> groupsForUser = identityService.createGroupQuery().groupMember(user.getId()).list();
    for (Group g : groupsForUser) {
        grantedAuthorityList.add(new GroupGrantedAuthority(g));
    }
    return new org.springframework.security.core.userdetails.User(user.getId(), user.getPassword(), active, active, active, active, grantedAuthorityList);
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) Group(org.activiti.engine.identity.Group) ActivitiException(org.activiti.engine.ActivitiException) User(org.activiti.engine.identity.User) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList)

Example 4 with UsernameNotFoundException

use of org.springframework.security.core.userdetails.UsernameNotFoundException in project libresonic by Libresonic.

the class SecurityService method loadUserByUsername.

public UserDetails loadUserByUsername(String username, boolean caseSensitive) throws UsernameNotFoundException, DataAccessException {
    User user = getUserByName(username, caseSensitive);
    if (user == null) {
        throw new UsernameNotFoundException("User \"" + username + "\" was not found.");
    }
    List<GrantedAuthority> authorities = getGrantedAuthorities(username);
    return new org.springframework.security.core.userdetails.User(username, user.getPassword(), authorities);
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) User(org.libresonic.player.domain.User) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority)

Example 5 with UsernameNotFoundException

use of org.springframework.security.core.userdetails.UsernameNotFoundException in project ranger by apache.

the class PasswordComparisonAuthenticator method authenticate.

// ~ Methods
// ========================================================================================================
public DirContextOperations authenticate(final Authentication authentication) {
    Assert.isInstanceOf(UsernamePasswordAuthenticationToken.class, authentication, "Can only process UsernamePasswordAuthenticationToken objects");
    // locate the user and check the password
    DirContextOperations user = null;
    String username = authentication.getName();
    String password = (String) authentication.getCredentials();
    Iterator dns = getUserDns(username).iterator();
    SpringSecurityLdapTemplate ldapTemplate = new SpringSecurityLdapTemplate(getContextSource());
    while (dns.hasNext() && user == null) {
        final String userDn = (String) dns.next();
        try {
            user = ldapTemplate.retrieveEntry(userDn, getUserAttributes());
        } catch (NameNotFoundException ignore) {
        }
    }
    if (user == null && getUserSearch() != null) {
        user = getUserSearch().searchForUser(username);
    }
    if (user == null) {
        throw new UsernameNotFoundException("User not found: " + username);
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Performing LDAP compare of password attribute '" + passwordAttributeName + "' for user '" + user.getDn() + "'");
    }
    String encodedPassword = passwordEncoder.encodePassword(password, null);
    byte[] passwordBytes = encodedPassword.getBytes();
    if (!ldapTemplate.compare(user.getDn().toString(), passwordAttributeName, passwordBytes)) {
        throw new BadCredentialsException(messages.getMessage("PasswordComparisonAuthenticator.badCredentials", "Bad credentials"));
    }
    return user;
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) SpringSecurityLdapTemplate(org.springframework.security.ldap.SpringSecurityLdapTemplate) DirContextOperations(org.springframework.ldap.core.DirContextOperations) NameNotFoundException(org.springframework.ldap.NameNotFoundException) Iterator(java.util.Iterator) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException)

Aggregations

UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)132 GrantedAuthority (org.springframework.security.core.GrantedAuthority)40 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)39 UserDetails (org.springframework.security.core.userdetails.UserDetails)36 Authentication (org.springframework.security.core.Authentication)24 Transactional (org.springframework.transaction.annotation.Transactional)20 Logger (org.slf4j.Logger)18 LoggerFactory (org.slf4j.LoggerFactory)18 java.util (java.util)16 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)15 Collectors (java.util.stream.Collectors)14 UserDetailsService (org.springframework.security.core.userdetails.UserDetailsService)14 Component (org.springframework.stereotype.Component)14 User (org.springframework.security.core.userdetails.User)13 ArrayList (java.util.ArrayList)12 HashSet (java.util.HashSet)11 UserRepository (io.github.jhipster.sample.repository.UserRepository)9 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)9 User (io.github.jhipster.sample.domain.User)6 Date (java.util.Date)6