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

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

Introduction

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

Prototype

UserDetailsService

Source Link

Usage

From source file:com.mastercard.test.spring.security.SpringTestApplication.java

/**
 * Provide an instance of UserDetailsService to help test @WithUserDetails.
 * @return The instance of UserDetailsService.
 */// w  w w .  java2s  .  c  om
@Bean
public UserDetailsService getUserDetailsService() {
    return new UserDetailsService() {
        @Override
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            return new User(username, "password", new ArrayList<>());
        }
    };
}

From source file:com.mastercard.test.spring.security.SpringTestApplicationWith2UserDetailsServiceBeans.java

/**
 * Provide an instance of UserDetailsService to help test @WithUserDetails.
 * @return The instance of UserDetailsService.
 *//* w  ww  .  j  av a  2 s  . c o m*/
@Bean
public UserDetailsService getUserDetailsService1() {
    return new UserDetailsService() {
        @Override
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            return new User(username, "password", new ArrayList<>());
        }
    };
}

From source file:org.starfishrespect.myconsumption.server.business.security.AuthConfig.java

@Bean
UserDetailsService userDetailsService() {
    return new UserDetailsService() {

        @Override/*from  w ww .  j av  a  2s.  c  om*/
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            org.starfishrespect.myconsumption.server.business.entities.User account = mUserRepository
                    .getUser(username);

            if (account != null) {
                List<GrantedAuthority> auth = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER");
                return new User(account.getName(), account.getPassword(), auth);
            } else {
                throw new UsernameNotFoundException("could not find the user '" + username + "'");
            }
        }

    };
}

From source file:hu.petabyte.redflags.web.cfg.SecurityCfg.java

@Bean
UserDetailsService userDetailsService() {
    return new UserDetailsService() {

        @Override//from   www . ja v a2s.  c o m
        public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
            Account account = (Account) accountRepository.findByEmailAddress(email);
            if (account != null) {
                return new WebUser(account.getEmailAddress(), account.getCryptedPassword(), account.getActive(),
                        true, true, true, AuthorityUtils.createAuthorityList("USER"), account);
            } else {
                throw new UsernameNotFoundException("could not find the user '" + email + "'");
            }
        }

    };
}

From source file:com.mastercard.test.spring.security.SpringTestApplicationWith2UserDetailsServiceBeans.java

/**
 * Provide an instance of UserDetailsService to help test @WithUserDetails.
 * @return The instance of UserDetailsService.
 *///w ww.j a  va2  s .  co m
@Bean
public UserDetailsService getUserDetailsService2() {
    return new UserDetailsService() {
        @Override
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            return new User(username, "password", new ArrayList<>());
        }
    };
}

From source file:com.cocktail.config.SecurityConfig.java

@Bean
UserDetailsService userDetailsService() {
    return new UserDetailsService() {

        @Override// www .  j ava  2s. co  m
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            User user = userRepository.findByUsername(username);
            if (user != null) {
                return new org.springframework.security.core.userdetails.User(user.getUsername(),
                        user.getPassword(), true, true, true, true, AuthorityUtils.createAuthorityList("USER"));
            } else {
                throw new UsernameNotFoundException("could not find the user '" + username + "'");
            }
        }

    };
}

From source file:at.plechinger.demo.scribesec.facebook.DemoApplication.java

@Bean(name = "userService")
public UserDetailsService userDetailsService() {

    //returns a fake user details service, replace with your own.
    return new UserDetailsService() {

        @Override//w w  w .ja v  a 2 s  .co m
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            return new User(username, username, Arrays.asList(new SimpleGrantedAuthority("ROLE_USER")));
        }
    };
}

From source file:com.github.zxkane.config.SecurityTestConfiguration.java

@Bean
public UserDetailsService userDetailsService() {
    return new UserDetailsService() {
        @Override/*from   ww  w .ja  va  2s. c o m*/
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            switch (username) {
            case "staffUsername":
                return staffUser();
            }
            throw new UsernameNotFoundException(username + " was not found.");
        }
    };
}

From source file:com.toptal.conf.SecurityConfiguration.java

/**
 * User details service bean./* www  .  j  a  va2  s . c  o m*/
 * @return Singleton bean.
 * @checkstyle DesignForExtensionCheck (5 lines)
 * @checkstyle AnonInnerLengthCheck (30 lines)
 */
@Bean
@SuppressWarnings("PMD.DefaultPackage")
UserDetailsService userDetails() {
    return new UserDetailsService() {
        @Override
        public UserDetails loadUserByUsername(final String name) {
            log.debug("Load user by username '{}'", name);
            final User user = SecurityConfiguration.this.dao.findByName(name);
            if (user == null) {
                throw new UsernameNotFoundException(String.format("User '%s' not found", name));
            }
            log.debug("User '{}' has authorities {}", user.getName(), this.authorities(user));
            return new org.springframework.security.core.userdetails.User(user.getName(), user.getPassword(),
                    this.authorities(user));
        }

        private List<GrantedAuthority> authorities(final User user) {
            final List<GrantedAuthority> result = AuthorityUtils
                    .createAuthorityList(User.Role.ROLE_USER.toString());
            if (user.isManager()) {
                result.addAll(AuthorityUtils.createAuthorityList(User.Role.ROLE_MANAGER.toString()));
            }
            return result;
        }
    };
}

From source file:uk.co.caprica.bootlace.security.SecurityConfiguration.java

/**
 * Create a user details service implementation.
 * <p>/*from ww w. j  ava  2  s . co  m*/
 * This method creates a simple user details service implementation that wraps the account
 * repository to convert an Account entity into a Spring Security {@link UserDetails} instance.
 * <p>
 * This service provides username, password and roles. It does not, although it could, use any
 * of the other account-related fields (e.g. for locked or expired accounts).
 *
 * @return user details service implementation
 */
private UserDetailsService getUserDetailsService() {
    return new UserDetailsService() {
        @Override
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            Account account = accountRepository.findByUsername(username);
            if (account != null) {
                return new UserWithId(account.getUsername(), account.getPassword(), true, // enabled
                        true, // account non-expired
                        true, // credentials non-expired
                        true, // account non-locked
                        AuthorityUtils.createAuthorityList(roles(account)), account.getId());
            } else {
                throw new UsernameNotFoundException("User not found '" + username + "'");
            }
        }
    };
}