Search in sources :

Example 81 with UsernameNotFoundException

use of org.springframework.security.core.userdetails.UsernameNotFoundException in project spring-thymeleaf-simplefinance by heitkergm.

the class UserDetailsDaoImpl method loadUserByUsername.

@Override
public UserDetails loadUserByUsername(final String userName) throws UsernameNotFoundException {
    List<LoginUser> users = userRepository.findByUserName(userName);
    if (users.size() <= 0) {
        throw new UsernameNotFoundException("User Name " + userName + " not found.");
    }
    LoginUser luser = users.get(0);
    UserDetails user = new User(luser.getUserName(), luser.getPassword(), YesNoEnum.toBoolean(luser.getEnabled()), true, true, true, AuthorityUtils.NO_AUTHORITIES);
    return user;
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) UserDetails(org.springframework.security.core.userdetails.UserDetails) User(org.springframework.security.core.userdetails.User) LoginUser(com.dappermoose.stsimplefinance.data.LoginUser) LoginUser(com.dappermoose.stsimplefinance.data.LoginUser)

Example 82 with UsernameNotFoundException

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

the class AclCollectionBeforeTest method setup.

@Before
public final void setup() {
    one = super.getTestPersistentBasicExpressionExperiment();
    two = super.getTestPersistentBasicExpressionExperiment();
    securityService.makePublic(one);
    securityService.makePublic(two);
    ees = new HashSet<>();
    ees.add(one);
    ees.add(two);
    try {
        userManager.loadUserByUsername(userName);
    } catch (UsernameNotFoundException e) {
        userManager.createUser(new UserDetailsImpl("foo", userName, true, null, RandomStringUtils.randomAlphabetic(10) + "@gmail.com", "key", new Date()));
    }
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) UserDetailsImpl(gemma.gsec.authentication.UserDetailsImpl) Date(java.util.Date) Before(org.junit.Before)

Example 83 with UsernameNotFoundException

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

the class ManualAuthenticationProcessingTest method before.

@Before
public void before() {
    pwd = this.randomName();
    username = this.randomName();
    try {
        userManager.loadUserByUsername(username);
    } catch (UsernameNotFoundException e) {
        String encodedPassword = passwordEncoder.encodePassword(pwd, username);
        UserDetailsImpl u = new UserDetailsImpl(encodedPassword, username, true, null, null, null, new Date());
        userManager.createUser(u);
    }
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) UserDetailsImpl(gemma.gsec.authentication.UserDetailsImpl) Date(java.util.Date) Before(org.junit.Before)

Example 84 with UsernameNotFoundException

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

the class ExecutingTaskTest method testSecurityContextManagement.

@Test
public void testSecurityContextManagement() {
    try {
        this.userManager.loadUserByUsername("ExecutingTaskTestUser");
    } catch (UsernameNotFoundException e) {
        this.userManager.createUser(new UserDetailsImpl("foo", "ExecutingTaskTestUser", true, null, "fooUser@chibi.ubc.ca", "key", new Date()));
    }
    this.runAsUser("ExecutingTaskTestUser");
    TaskCommand taskCommand = new TaskCommand();
    this.runAsAdmin();
    Task<TaskResult, TaskCommand> task = new SuccessTestTask();
    task.setTaskCommand(taskCommand);
    ExecutingTask<TaskResult> executingTask = new ExecutingTask<>(task, taskCommand);
    executingTask.setProgressAppender(progressAppender);
    executingTask.setStatusCallback(statusSecurityContextCheckerHandler);
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    Future<TaskResult> future = executorService.submit(executingTask);
    this.tryGetAnswer(future);
    assertEquals(taskCommand.getSecurityContext(), securityContextAfterInitialize);
    assertNotSame(taskCommand.getSecurityContext(), securityContextAfterFinish);
    assertEquals(null, securityContextAfterFail);
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) UserDetailsImpl(gemma.gsec.authentication.UserDetailsImpl) ExecutorService(java.util.concurrent.ExecutorService) ExecutingTask(ubic.gemma.core.job.executor.common.ExecutingTask) Date(java.util.Date) Test(org.junit.Test) BaseSpringContextTest(ubic.gemma.core.testing.BaseSpringContextTest)

Example 85 with UsernameNotFoundException

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

the class UserManagerImpl method changePasswordForUser.

@Override
@Secured({ "IS_AUTHENTICATED_ANONYMOUSLY", "RUN_AS_ADMIN" })
@Transactional
public String changePasswordForUser(String email, String username, String newPassword) throws AuthenticationException {
    Authentication currentAuthentication = SecurityContextHolder.getContext().getAuthentication();
    if (currentAuthentication == null) {
        // This would indicate bad coding somewhere
        throw new AccessDeniedException("Can't change password as no Authentication object found in context " + "for current user.");
    }
    User u = userService.findByEmail(email);
    if (u == null) {
        throw new UsernameNotFoundException("No user found for that email address.");
    }
    String foundUsername = u.getUserName();
    if (!foundUsername.equals(username)) {
        throw new AccessDeniedException("Wrong user name was provided for the email address.");
    }
    logger.debug("Changing password for user '" + username + "'");
    u.setPassword(newPassword);
    u.setEnabled(false);
    u.setSignupToken(this.generateSignupToken(username));
    u.setSignupTokenDatestamp(new Date());
    userService.update(u);
    userCache.removeUserFromCache(username);
    return u.getSignupToken();
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) AccessDeniedException(org.springframework.security.access.AccessDeniedException) User(gemma.gsec.model.User) Authentication(org.springframework.security.core.Authentication) Secured(org.springframework.security.access.annotation.Secured) Transactional(org.springframework.transaction.annotation.Transactional)

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