org.springframework.security.core.userdetails.UserDetailsService Java Examples

The following examples show how to use org.springframework.security.core.userdetails.UserDetailsService. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SecurityConfiguration.java    From grpc-spring-boot-starter with MIT License 6 votes vote down vote up
@Bean
// This could be your database lookup. There are some complete implementations in spring-security-web.
UserDetailsService userDetailsService() {
    return username -> {
        log.debug("Searching user: {}", username);
        switch (username) {
            case "guest": {
                return new User(username, passwordEncoder().encode(username + "Password"), Collections.emptyList());
            }
            case "user": {
                final List<SimpleGrantedAuthority> authorities =
                        Arrays.asList(new SimpleGrantedAuthority("ROLE_GREET"));
                return new User(username, passwordEncoder().encode(username + "Password"), authorities);
            }
            default: {
                throw new UsernameNotFoundException("Could not find user!");
            }
        }
    };
}
 
Example #2
Source File: SecurityConfig.java    From macrozheng with Apache License 2.0 6 votes vote down vote up
@Bean
public UserDetailsService userDetailsService() {
    //获取登录用户信息
    return new UserDetailsService() {
        @Override
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            UmsAdminExample example = new UmsAdminExample();
            example.createCriteria().andUsernameEqualTo(username);
            List<UmsAdmin> umsAdminList = umsAdminMapper.selectByExample(example);
            if (umsAdminList != null && umsAdminList.size() > 0) {
                return new AdminUserDetails(umsAdminList.get(0));
            }
            throw new UsernameNotFoundException("用户名或密码错误");
        }
    };
}
 
Example #3
Source File: SecurityContextApplication.java    From spring-cloud-zuul-ratelimit with Apache License 2.0 6 votes vote down vote up
@Bean
@Override
@SuppressWarnings("deprecation")
public UserDetailsService userDetailsService() {
    UserDetails user =
            User.withDefaultPasswordEncoder()
                    .username("user")
                    .password("user")
                    .roles("USER")
                    .build();

    UserDetails admin = User.withDefaultPasswordEncoder()
            .username("admin")
            .password("admin")
            .roles("ADMIN")
            .build();

    return new InMemoryUserDetailsManager(user, admin);
}
 
Example #4
Source File: SecurityConfig.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Autowired
public void configureGlobal( AuthenticationManagerBuilder auth, UserService userService,
    UserDetailsService userDetailsService, SecurityService securityService,
    @Lazy CustomLdapAuthenticationProvider customLdapAuthenticationProvider )
    throws Exception
{
    TwoFactorAuthenticationProvider twoFactorAuthenticationProvider = new TwoFactorAuthenticationProvider();
    twoFactorAuthenticationProvider.setPasswordEncoder( encoder() );
    twoFactorAuthenticationProvider.setUserService( userService );
    twoFactorAuthenticationProvider.setUserDetailsService( userDetailsService );
    twoFactorAuthenticationProvider.setSecurityService( securityService );

    // configure the Authentication providers

    auth
        // Two factor
        .authenticationProvider( twoFactorAuthenticationProvider )
        // LDAP Authentication
        .authenticationProvider( customLdapAuthenticationProvider )
        //  OAUTH2
        .userDetailsService( defaultClientDetailsUserDetailsService )
            // Use a non-encoding password for oauth2 secrets, since the secret is generated by the client
            .passwordEncoder(NoOpPasswordEncoder.getInstance());
}
 
Example #5
Source File: SecurityConfiguration.java    From spring-boot-demo with MIT License 6 votes vote down vote up
@Bean
protected UserDetailsService myUserDetailsService() {
    InMemoryUserDetailsManager inMemoryUserDetailsManager = new InMemoryUserDetailsManager();

    String[][] usersGroupsAndRoles = {{"salaboy", "password", "ROLE_ACTIVITI_USER", "GROUP_activitiTeam"}, {"ryandawsonuk", "password", "ROLE_ACTIVITI_USER", "GROUP_activitiTeam"}, {"erdemedeiros", "password", "ROLE_ACTIVITI_USER", "GROUP_activitiTeam"}, {"other", "password", "ROLE_ACTIVITI_USER", "GROUP_otherTeam"}, {"admin", "password", "ROLE_ACTIVITI_ADMIN"}};

    for (String[] user : usersGroupsAndRoles) {
        List<String> authoritiesStrings = Arrays.asList(Arrays.copyOfRange(user, 2, user.length));
        log.info("> Registering new user: " + user[0] + " with the following Authorities[" + authoritiesStrings + "]");
        inMemoryUserDetailsManager.createUser(new User(user[0], passwordEncoder().encode(user[1]), authoritiesStrings
                .stream()
                .map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList())));
    }


    return inMemoryUserDetailsManager;
}
 
Example #6
Source File: SpringSecurityUserContext.java    From Spring-Security-Third-Edition with MIT License 5 votes vote down vote up
@Autowired
public SpringSecurityUserContext(CalendarService calendarService,UserDetailsService userDetailsService) {
    if (calendarService == null) {
        throw new IllegalArgumentException("calendarService cannot be null");
    }
    if (userDetailsService == null) {
        throw new IllegalArgumentException("userDetailsService cannot be null");
    }
    this.calendarService = calendarService;
    this.userDetailsService = userDetailsService;
}
 
Example #7
Source File: WebSecurityConfig.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
@Bean
@Override
public UserDetailsService userDetailsService() {
    UserDetails user =
            User.withDefaultPasswordEncoder()
                    .username("user")
                    .password("password")
                    .roles("USER")
                    .build();

    return new InMemoryUserDetailsManager(user);
}
 
Example #8
Source File: SecurityConfig.java    From spring-security-oauth2-demo with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 创建两个内存用户
 * 用户名 user 密码 123456 角色 ROLE_USER
 * 用户名 admin 密码 admin 角色 ROLE_ADMIN
 *
 * @return InMemoryUserDetailsManager
 */
@Bean
@Override
public UserDetailsService userDetailsService() {
    InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
    manager.createUser(User.withUsername("user")
            .password(passwordEncoder().encode("123456"))
            .authorities("ROLE_USER").build());
    manager.createUser(User.withUsername("admin")
            .password(passwordEncoder().encode("admin"))
            .authorities("ROLE_ADMIN").build());
    return manager;
}
 
Example #9
Source File: SecurityConfig.java    From macrozheng-mall with MIT License 5 votes vote down vote up
@Bean
public UserDetailsService userDetailsService() {
    //获取登录用户信息
    return username -> {
        UmsAdmin admin = adminService.getAdminByUsername(username);
        if (admin != null) {
            List<UmsPermission> permissionList = adminService.getPermissionList(admin.getId());
            return new AdminUserDetails(admin,permissionList);
        }
        throw new UsernameNotFoundException("用户名或密码错误");
    };
}
 
Example #10
Source File: SecurityConfiguration.java    From tutorials with MIT License 5 votes vote down vote up
public SecurityConfiguration(AuthenticationManagerBuilder authenticationManagerBuilder, UserDetailsService userDetailsService,
        TokenProvider tokenProvider,
    CorsFilter corsFilter) {

    this.authenticationManagerBuilder = authenticationManagerBuilder;
    this.userDetailsService = userDetailsService;
    this.tokenProvider = tokenProvider;
    this.corsFilter = corsFilter;
}
 
Example #11
Source File: SpringSecurityUserContext.java    From Spring-Security-Third-Edition with MIT License 5 votes vote down vote up
@Autowired
public SpringSecurityUserContext(final CalendarService calendarService,
                                 final UserDetailsService userDetailsService) {
    if (calendarService == null) {
        throw new IllegalArgumentException("calendarService cannot be null");
    }
    if (userDetailsService == null) {
        throw new IllegalArgumentException("userDetailsService cannot be null");
    }
    this.calendarService = calendarService;
    this.userDetailsService = userDetailsService;
}
 
Example #12
Source File: AuthenticationConfiguration.java    From speedment-secure-rest-example with Apache License 2.0 5 votes vote down vote up
@Bean
public UserDetailsService getUserDetailsService() {
    return username -> accounts.stream()
        .filter(Account.USERNAME.equal(username))
        .findAny()
        .orElseThrow(() -> new UsernameNotFoundException(
            "Could not find the user '" + username + "'"
        ));
}
 
Example #13
Source File: SecurityConfig.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
 * 配置用户
 * 使用内存中的用户,实际项目中,一般使用的是数据库保存用户,具体的实现类可以使用JdbcDaoImpl或者JdbcUserDetailsManager
 *
 * @return
 */
@Bean
@Override
protected UserDetailsService userDetailsService() {
    InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
    manager.createUser(User.withUsername("admin").password(PasswordEncoderFactories.createDelegatingPasswordEncoder().encode("admin")).authorities("USER").build());
    return manager;
}
 
Example #14
Source File: MultiDeviceRememberMeServices.java    From spring-boot-doma2-sample with Apache License 2.0 5 votes vote down vote up
/**
 * コンストラクタ
 * 
 * @param key
 * @param userDetailsService
 * @param tokenRepository
 */
public MultiDeviceRememberMeServices(String key, UserDetailsService userDetailsService,
        MultiDeviceTokenRepository tokenRepository) {
    super(key, userDetailsService);

    this.random = new SecureRandom();
    this.tokenRepository = tokenRepository;
}
 
Example #15
Source File: SpringSecurityUserContext.java    From Spring-Security-Third-Edition with MIT License 5 votes vote down vote up
@Autowired
public SpringSecurityUserContext(CalendarService calendarService,UserDetailsService userDetailsService) {
    if (calendarService == null) {
        throw new IllegalArgumentException("calendarService cannot be null");
    }
    if (userDetailsService == null) {
        throw new IllegalArgumentException("userDetailsService cannot be null");
    }
    this.calendarService = calendarService;
    this.userDetailsService = userDetailsService;
}
 
Example #16
Source File: WebSecurityConfig.java    From api-server-seed with Apache License 2.0 5 votes vote down vote up
@Autowired
    public WebSecurityConfig(JwtAuthenticationEntryPoint unauthorizedHandler,
                             @Qualifier("RestAuthenticationAccessDeniedHandler") AccessDeniedHandler accessDeniedHandler,
                             @Qualifier("myUserDetailsService") UserDetailsService myUserDetailsService
//                             JwtAuthenticationTokenFilter authenticationTokenFilter
    ) {
        this.unauthorizedHandler = unauthorizedHandler;
        this.accessDeniedHandler = accessDeniedHandler;
        this.userDetailsService = myUserDetailsService;
//        this.authenticationTokenFilter = authenticationTokenFilter;
    }
 
Example #17
Source File: AccountController.java    From Spring-Security-Third-Edition with MIT License 5 votes vote down vote up
@Autowired
public AccountController(UserDetailsService userDetailsService) {
    if (userDetailsService == null) {
        throw new IllegalArgumentException("userDetailsService cannot be null");
    }
    this.userDetailsService = userDetailsService;
}
 
Example #18
Source File: AuthorizationServerConfiguration.java    From Hands-On-Microservices-with-Spring-Boot-and-Spring-Cloud with MIT License 5 votes vote down vote up
@Bean
@Override
public UserDetailsService userDetailsService() {
	return new InMemoryUserDetailsManager(
			User.withDefaultPasswordEncoder()
				.username("magnus")
				.password("password")
				.roles("USER")
				.build());
}
 
Example #19
Source File: WebSecurityConfig.java    From sctalk with Apache License 2.0 5 votes vote down vote up
@Autowired
public WebSecurityConfig(UserDetailsService userDetailsService,
        JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter,
        EntryPointUnauthorizedHandler entryPointUnauthorizedHandler,
        RestAccessDeniedHandler restAccessDeniedHandler) {
    this.userDetailsService = userDetailsService;
    this.jwtAuthenticationTokenFilter = jwtAuthenticationTokenFilter;
    this.entryPointUnauthorizedHandler = entryPointUnauthorizedHandler;
    this.restAccessDeniedHandler = restAccessDeniedHandler;
    this.passwordEncoder = new Md5PasswordEncoder();
}
 
Example #20
Source File: JdbcSecurityConfiguration.java    From pro-spring-boot with Apache License 2.0 5 votes vote down vote up
@Bean
public UserDetailsService userDetailsService(JdbcTemplate jdbcTemplate) {
	RowMapper<User> userRowMapper = (ResultSet rs, int i) ->
		new User(
			rs.getString("ACCOUNT_NAME"),
			rs.getString("PASSWORD"),
			rs.getBoolean("ENABLED"),
			rs.getBoolean("ENABLED"),
			rs.getBoolean("ENABLED"), 
			rs.getBoolean("ENABLED"),
			AuthorityUtils.createAuthorityList("ROLE_USER", "ROLE_ADMIN"));
	return username ->
		jdbcTemplate.queryForObject("SELECT * from ACCOUNT where ACCOUNT_NAME = ?",
				userRowMapper, username);
}
 
Example #21
Source File: SpringSecurityUserContext.java    From Spring-Security-Third-Edition with MIT License 5 votes vote down vote up
@Autowired
public SpringSecurityUserContext(final CalendarService calendarService,
                                 final UserDetailsService userDetailsService) {
    if (calendarService == null) {
        throw new IllegalArgumentException("calendarService cannot be null");
    }
    if (userDetailsService == null) {
        throw new IllegalArgumentException("userDetailsService cannot be null");
    }
    this.calendarService = calendarService;
    this.userDetailsService = userDetailsService;
}
 
Example #22
Source File: WebSecurityConfig.java    From spring-boot-study with MIT License 5 votes vote down vote up
@Bean
@Override
public UserDetailsService userDetailsService(){
    UserDetails user = User.withDefaultPasswordEncoder()
            .username("user")
            .password("password")
            .roles("USER")
            .build();

    return new InMemoryUserDetailsManager(user);
}
 
Example #23
Source File: SpringSecurityUserContext.java    From Spring-Security-Third-Edition with MIT License 5 votes vote down vote up
@Autowired
public SpringSecurityUserContext(final CalendarService calendarService,
                                 final UserDetailsService userDetailsService) {
    if (calendarService == null) {
        throw new IllegalArgumentException("calendarService cannot be null");
    }
    if (userDetailsService == null) {
        throw new IllegalArgumentException("userDetailsService cannot be null");
    }
    this.calendarService = calendarService;
    this.userDetailsService = userDetailsService;
}
 
Example #24
Source File: AccountController.java    From Spring-Security-Third-Edition with MIT License 5 votes vote down vote up
@Autowired
public AccountController(UserDetailsService userDetailsService) {
    if (userDetailsService == null) {
        throw new IllegalArgumentException("userDetailsService cannot be null");
    }
    this.userDetailsService = userDetailsService;
}
 
Example #25
Source File: SpringSecurityUserContext.java    From Spring-Security-Third-Edition with MIT License 5 votes vote down vote up
@Autowired
public SpringSecurityUserContext(final CalendarService calendarService,
                                 final UserDetailsService userDetailsService) {
    if (calendarService == null) {
        throw new IllegalArgumentException("calendarService cannot be null");
    }
    if (userDetailsService == null) {
        throw new IllegalArgumentException("userDetailsService cannot be null");
    }
    this.calendarService = calendarService;
    this.userDetailsService = userDetailsService;
}
 
Example #26
Source File: AuthenticationConfiguration.java    From java-microservice with MIT License 5 votes vote down vote up
@Bean
protected UserDetailsService userDetailsService() {
    return (email) -> {
        com.apssouza.pojos.User user = userService.getUserByEmail(email);
        return new User(
                user.getEmail(),
                user.getPassword(),
                true, true, true, true,
                AuthorityUtils.createAuthorityList("USER", "write")
        );
    };
}
 
Example #27
Source File: PasswordStorageWebSecurityConfigurer.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public UserDetailsService getUserDefaultDetailsService() {
    return new InMemoryUserDetailsManager(User
      .withUsername("baeldung")
      .password("{noop}SpringSecurity5")
      .authorities(Collections.emptyList())
      .build());
}
 
Example #28
Source File: MavenExtension.java    From spring-cloud-deployer with Apache License 2.0 5 votes vote down vote up
@Bean
public UserDetailsService userDetailsService() {
	UserBuilder users = User.builder();
	InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
	manager.createUser(users.username("user").password("{noop}password").roles("USER").build());
	return manager;
}
 
Example #29
Source File: SecurityConf.java    From codenjoy with GNU General Public License v3.0 5 votes vote down vote up
@Bean
public UserDetailsService userDetailsService() {
   User admin = UserService.buildUserDetails(adminLogin,
           passwordEncoder().encode(adminPassword),
           ROLE_ADMIN, ROLE_USER);
   return new UserService(Collections.singletonMap(adminLogin, admin), players);
}
 
Example #30
Source File: PkiSecurityConfiguration.java    From flair-engine with Apache License 2.0 5 votes vote down vote up
@Bean
@Override
public UserDetailsService userDetailsService() {
    return (username -> {
        if (properties.getAuthentication().getFlairBi().getPki().getSubjects().contains(username)) {
            return new User(username, "", AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"));
        } else {
            throw new UsernameNotFoundException("Invalid response");
        }
    });
}