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();
}
}
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;
}
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);
}
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);
}
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;
}
Aggregations