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

The following examples show how to use org.springframework.security.core.userdetails.UserDetailsService. These examples are extracted from open source projects. 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 Project: grpc-spring-boot-starter   Source File: SecurityConfiguration.java    License: 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
@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 Project: macrozheng   Source File: SecurityConfig.java    License: 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
@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 Project: spring-boot-demo   Source File: SecurityConfiguration.java    License: 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
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
@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
@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 Project: activiti6-boot2   Source File: SecurityConfiguration.java    License: 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 Project: api-server-seed   Source File: WebSecurityConfig.java    License: 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
@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
/**
 * コンストラクタ
 * 
 * @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 Project: spring-boot-demo   Source File: SecurityConfig.java    License: 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
@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 Project: flair-registry   Source File: JWTSecurityConfiguration.java    License: 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 Project: tutorials   Source File: MultipleEntryPointsSecurityConfig.java    License: 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 Project: eladmin   Source File: SecurityUtils.java    License: 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 Project: Spring-Security-Third-Edition   Source File: AccountController.java    License: 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
@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 Project: tutorials   Source File: SecurityConfiguration.java    License: 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
/**
 * 创建两个内存用户
 * 用户名 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
@Bean
@Override
public UserDetailsService userDetailsService() {
	return new InMemoryUserDetailsManager(
			User.withDefaultPasswordEncoder()
				.username("magnus")
				.password("password")
				.roles("USER")
				.build());
}
 
Example 23
Source Project: sctalk   Source File: WebSecurityConfig.java    License: 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 Project: pro-spring-boot   Source File: JdbcSecurityConfiguration.java    License: 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
@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 Project: spring-boot-study   Source File: WebSecurityConfig.java    License: 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
@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 Project: Spring-Security-Third-Edition   Source File: AccountController.java    License: 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
@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 Project: java-microservice   Source File: AuthenticationConfiguration.java    License: 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")
        );
    };
}