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: 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 #3
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 #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: SecurityConfiguration.java    From e-commerce-microservice with Apache License 2.0 5 votes vote down vote up
public SecurityConfiguration(AuthenticationManagerBuilder authenticationManagerBuilder, UserDetailsService userDetailsService, TokenProvider tokenProvider, CorsFilter corsFilter, SecurityProblemSupport problemSupport) {
    this.authenticationManagerBuilder = authenticationManagerBuilder;
    this.userDetailsService = userDetailsService;
    this.tokenProvider = tokenProvider;
    this.corsFilter = corsFilter;
    this.problemSupport = problemSupport;
}
 
Example #7
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 #8
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 #9
Source File: SecurityConfiguration.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
@Bean
public UserDetailsService userDetailsService() {
    if (userDetailsServiceOverride != null) {
        return userDetailsServiceOverride.createUserDetailsService();
    }
    
    return new com.activiti.security.UserDetailsService();
}
 
Example #10
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 #11
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 #12
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 #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: 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 #15
Source File: JWTSecurityConfiguration.java    From flair-registry with Apache License 2.0 5 votes vote down vote up
@Bean
public UserDetailsService userDetailsService() {
    InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
    manager.createUser(
        User.withUsername(username)
            .password(passwordEncoder().encode(password))
            .roles(roles)
            .build());
    return manager;
}
 
Example #16
Source File: MultipleEntryPointsSecurityConfig.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public UserDetailsService userDetailsService() throws Exception {
    InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
    manager.createUser(User.withUsername("user").password(encoder().encode("userPass")).roles("USER").build());
    manager.createUser(User.withUsername("admin").password(encoder().encode("adminPass")).roles("ADMIN").build());
    return manager;
}
 
Example #17
Source File: SecurityUtils.java    From eladmin with Apache License 2.0 5 votes vote down vote up
/**
 * 获取当前登录的用户
 * @return UserDetails
 */
public static UserDetails getCurrentUser() {
    final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null) {
        throw new BadRequestException(HttpStatus.UNAUTHORIZED, "当前登录状态过期");
    }
    if (authentication.getPrincipal() instanceof UserDetails) {
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        UserDetailsService userDetailsService = SpringContextHolder.getBean(UserDetailsService.class);
        return userDetailsService.loadUserByUsername(userDetails.getUsername());
    }
    throw new BadRequestException(HttpStatus.UNAUTHORIZED, "找不到当前登录的信息");
}
 
Example #18
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 #19
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 #20
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 #21
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 #22
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 #23
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 #24
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 #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: 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 #27
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 #28
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 #29
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 #30
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")
        );
    };
}