org.springframework.security.authentication.AuthenticationProvider Java Examples

The following examples show how to use org.springframework.security.authentication.AuthenticationProvider. 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: IdolSecurityCustomizerImpl.java    From find with MIT License 6 votes vote down vote up
private AuthenticationProvider communityAuthenticationProvider() {
    final Role user = new Role.Builder()
            .setName(FindCommunityRole.USER.value())
            .setPrivileges(Collections.singleton("login"))
            .build();

    final Set<String> defaultRoles;

    if (defaultRolesProperty.isEmpty()) {
        defaultRoles = Collections.emptySet();
    } else {
        defaultRoles = new HashSet<>(Arrays.asList(defaultRolesProperty.split(",")));
    }

    return new CommunityAuthenticationProvider(
            configService,
            userService,
            new Roles(Collections.singletonList(user)),
            Collections.singleton("login"),
            grantedAuthoritiesMapper,
            defaultRoles
    );
}
 
Example #2
Source File: OtpGeneratingAuthenticationProvider.java    From spring-security-otp with Apache License 2.0 6 votes vote down vote up
public OtpGeneratingAuthenticationProvider(AuthenticationProvider provider,
		Tokenstore tokenstore, LookupStrategy lookupStrategy, SendStrategy sendStrategy) {
	if (provider == null) {
		throw new IllegalArgumentException("Embedded authentication provider must not be null.");
	}
	if (tokenstore == null) {
		throw new IllegalArgumentException("Tokenstore must not be null.");
	}
	if (lookupStrategy == null) {
		throw new IllegalArgumentException("LookupStrategy must not be null.");
	}
	if (sendStrategy == null) {
		throw new IllegalArgumentException("SendStrategy must not be null.");
	}
	this.provider = provider;
	this.tokenstore = tokenstore;
	this.lookupStrategy = lookupStrategy;
	this.sendStrategy = sendStrategy;
	this.gen = new DefaultOtpGenerator(DEFAULT_OTP_LENGTH);
}
 
Example #3
Source File: SecurityConfig.java    From feast with Apache License 2.0 6 votes vote down vote up
/**
 * Initializes an AuthenticationManager if authentication has been enabled.
 *
 * @return AuthenticationManager
 */
@Bean
@ConditionalOnProperty(prefix = "feast.security.authentication", name = "enabled")
AuthenticationManager authenticationManager() {
  final List<AuthenticationProvider> providers = new ArrayList<>();

  if (securityProperties.getAuthentication().isEnabled()) {
    switch (securityProperties.getAuthentication().getProvider()) {
      case "jwt":
        providers.add(
            new DefaultJwtAuthenticationProvider(
                securityProperties.getAuthentication().getOptions()));
        break;
      default:
        throw new IllegalArgumentException(
            "Please configure an Authentication Provider if you have enabled authentication.");
    }
  }
  return new ProviderManager(providers);
}
 
Example #4
Source File: SimpleHashUtil.java    From Roothub with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
	Class<? extends Authentication> toTest = authentication.getClass();
	Authentication result = null;
	for (AuthenticationProvider provider : providers) {
		if (!provider.supports(toTest)) {
			continue;
		}
		// 调用认证提供者进行认证,如果 result 不为 null ,说明认证通过
		result = provider.authenticate(authentication);
		if (result != null) {
			break;
		}
	}
	if (result == null) {
		throw new ProviderNotFoundException("ProviderManager.providerNotFound");
	}
	return result;
}
 
Example #5
Source File: NextServerSession.java    From nextreports-server with Apache License 2.0 6 votes vote down vote up
public List<String> getRealms() {
	List<AuthenticationProvider> providers = authenticationManager.getProviders();
	if (LOG.isDebugEnabled()) {
		LOG.debug("Found " + providers.size() + " authentication providers");
	}
	
	List<String> realms = new ArrayList<String>();
	for (AuthenticationProvider provider : providers) {
		if (provider instanceof ExternalAuthenticationProvider) {
			ExternalAuthenticationProvider externalProvider = (ExternalAuthenticationProvider) provider;
			realms.add(externalProvider.getRealm());
		} else if (provider instanceof NextServerAuthenticationProvider) {
			realms.add(""); // default provider
		}
	}
	
	return realms;
}
 
Example #6
Source File: SimpleHashUtil.java    From Roothub with GNU Affero General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) {
	// 初始化密码认证处理器
	PasswordEncoder passwordEncoder = new MyMessageDigestPasswordEncoder("md5");
	// 初始化认证服务
	UserDetailsService userDetailsService = new MyUserDetailsService();
	
	// 初始化认证提供者
	AuthenticationProvider provider = new MyAuthenticationProvider(userDetailsService, passwordEncoder);
	
	List<AuthenticationProvider> providers = new ArrayList<>();
	providers.add(provider);
	
	// 初始化认证管理器
	AuthenticationManager am = new MyAuthenticationManager(providers);
	MyUsernamePasswordAuthenticationFilter filter = new MyUsernamePasswordAuthenticationFilter("/login");
	filter.setAuthenticationManager(am);
	//filter.doFilter(req, res, chain);
	
}
 
Example #7
Source File: SshdServerConfiguration.java    From sshd-shell-spring-boot with Apache License 2.0 5 votes vote down vote up
private PasswordAuthenticator authProviderAuthenticator(Auth authProps) throws IllegalArgumentException {
    try {
        AuthenticationProvider authProvider = Objects.isNull(authProps.getAuthProviderBeanName())
                ? appContext.getBean(AuthenticationProvider.class)
                : appContext.getBean(authProps.getAuthProviderBeanName(), AuthenticationProvider.class);
        return new AuthProviderSshdPasswordAuthenticator(authProvider);
    } catch (BeansException ex) {
        throw new IllegalArgumentException("Expected a default or valid AuthenticationProvider bean", ex);
    }
}
 
Example #8
Source File: InceptionSecurity.java    From inception with Apache License 2.0 5 votes vote down vote up
@Autowired
public InceptionSecurity(PasswordEncoder aPasswordEncoder,
        @Lazy AuthenticationManager aAuthenticationManager,
        @Lazy AuthenticationProvider aAuthenticationProvider, DataSource aDataSource,
        UserDao aUserRepository)
{
    passwordEncoder = aPasswordEncoder;
    authenticationManager = aAuthenticationManager;
    authenticationProvider = aAuthenticationProvider;
    dataSource = aDataSource;
    userRepository = aUserRepository;
}
 
Example #9
Source File: SecurityConfig.java    From bearchoke with Apache License 2.0 5 votes vote down vote up
@Bean(name = "preAuthAuthenticationManager")
public AuthenticationManager preAuthAuthenticationManager() {
    PreAuthenticatedAuthenticationProvider preAuthProvider = new PreAuthenticatedAuthenticationProvider();
    preAuthProvider.setPreAuthenticatedUserDetailsService(preAuthUserDetailsService);

    List<AuthenticationProvider> providers = new ArrayList<AuthenticationProvider>();
    providers.add(preAuthProvider);

    return new ProviderManager(providers);
}
 
Example #10
Source File: SpringMvcSecurityConfig.java    From Hands-On-High-Performance-with-Spring-5 with MIT License 5 votes vote down vote up
@Bean
public AuthenticationProvider authenticationProviderBean() {
    DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
    authenticationProvider.setPasswordEncoder(passwordEncoder);
    authenticationProvider.setUserDetailsService(userDetailsService());
    
    return authenticationProvider;
}
 
Example #11
Source File: WebSecurityConfigurer.java    From spring-oauth-server with GNU General Public License v2.0 5 votes vote down vote up
@Bean
public AuthenticationProvider authenticationProvider() {
    DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
    daoAuthenticationProvider.setUserDetailsService(userService);
    daoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
    return daoAuthenticationProvider;
}
 
Example #12
Source File: WebAnnoSecurity.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Autowired
public WebAnnoSecurity(PasswordEncoder aPasswordEncoder,
        @Lazy AuthenticationManager aAuthenticationManager,
        @Lazy AuthenticationProvider aAuthenticationProvider, DataSource aDataSource,
        UserDao aUserRepository)
{
    passwordEncoder = aPasswordEncoder;
    authenticationManager = aAuthenticationManager;
    authenticationProvider = aAuthenticationProvider;
    dataSource = aDataSource;
    userRepository = aUserRepository;
}
 
Example #13
Source File: SecurityConfig.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Bean
@Override
protected AuthenticationManager authenticationManager() throws Exception {
    List<AuthenticationProvider> providers = new ArrayList<>(1);
    providers.add(preAuthAuthProvider());
    return new ProviderManager(providers);
}
 
Example #14
Source File: MockServerConfig.java    From bearchoke with Apache License 2.0 5 votes vote down vote up
@Bean(name = "authenticationProvider")
public AuthenticationProvider authenticationProvider() {
    DaoAuthenticationProvider result = new DaoAuthenticationProvider();
    result.setUserDetailsService(inMemoryUserDetailsManager());

    return result;
}
 
Example #15
Source File: WebSecurityConfiguration.java    From Parrit with MIT License 5 votes vote down vote up
@Bean
public AuthenticationProvider authenticationProvider(UserDetailsService userDetailsService, PasswordEncoder passwordEncoder) {
    DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
    daoAuthenticationProvider.setUserDetailsService(userDetailsService);
    daoAuthenticationProvider.setPasswordEncoder(passwordEncoder);
    daoAuthenticationProvider.setHideUserNotFoundExceptions(false);
    return daoAuthenticationProvider;
}
 
Example #16
Source File: IdolSecurityCustomizerImplTest.java    From find with MIT License 5 votes vote down vote up
@Test
public void testDefaultRoles() {
    final Collection<AuthenticationProvider> authenticationProviders = idolSecurityCustomizer.getAuthenticationProviders();

    assertThat(authenticationProviders, hasSize(1));

    final Authentication authentication = authenticationProviders.stream()
            .findFirst()
            .map(authenticationProvider -> authenticationProvider.authenticate(this.foreignAuthentication))
            .orElseThrow(() -> new AssertionError("AuthenticationProvider did not authenticate"));

    assertThat(authentication.getAuthorities(), contains(authority("FindUser")));
}
 
Example #17
Source File: ReverseProxyIdolSecurityCustomizer.java    From find with MIT License 5 votes vote down vote up
@Override
public Collection<AuthenticationProvider> getAuthenticationProviders() {
    return Collections.singleton(new IdolPreAuthenticatedAuthenticationProvider(
            userService,
            grantedAuthoritiesMapper,
            Arrays.stream(preAuthenticatedRoles.split(","))
                    .map(FindCommunityRole::fromValue)
                    .map(FindCommunityRole::value)
                    .collect(Collectors.toSet())
    ));
}
 
Example #18
Source File: JwtWebSecurityConfigurerTest.java    From auth0-spring-security-api with MIT License 5 votes vote down vote up
@Test
public void shouldCreateRS256ConfigurerWithCustomAuthenticationProvider() throws Exception {
    AuthenticationProvider provider = mock(AuthenticationProvider.class);
    JwtWebSecurityConfigurer configurer = JwtWebSecurityConfigurer.forRS256("audience", "issuer", provider);

    assertThat(configurer, is(notNullValue()));
    assertThat(configurer.audience, is("audience"));
    assertThat(configurer.issuers, arrayWithSize(1));
    assertThat(configurer.issuers, arrayContaining("issuer"));
    assertThat(configurer.provider, is(notNullValue()));
    assertThat(configurer.provider, is(provider));
}
 
Example #19
Source File: JwtWebSecurityConfigurerTest.java    From auth0-spring-security-api with MIT License 5 votes vote down vote up
@Test
public void shouldCreateHS256ConfigurerWithCustomAuthenticationProvider() throws Exception {
    AuthenticationProvider provider = mock(AuthenticationProvider.class);
    JwtWebSecurityConfigurer configurer = JwtWebSecurityConfigurer.forHS256("audience", "issuer", provider);

    assertThat(configurer, is(notNullValue()));
    assertThat(configurer.audience, is("audience"));
    assertThat(configurer.issuers, arrayWithSize(1));
    assertThat(configurer.issuers, arrayContaining("issuer"));
    assertThat(configurer.provider, is(notNullValue()));
    assertThat(configurer.provider, is(provider));
}
 
Example #20
Source File: CustomSecurityConfig.java    From multitenancy with Apache License 2.0 5 votes vote down vote up
/**
 * Authentication provider which provides the logged in user's credentials for
 * verification and authentication if they are coeect
 * 
 * @return
 */
public AuthenticationProvider authProvider() {
    // The custom authentication provider defined for this app
    CustomUserDetailsAuthenticationProvider provider = new CustomUserDetailsAuthenticationProvider(
            passwordEncoder(), userDetailsService);
    return provider;
}
 
Example #21
Source File: SecurityConfiguration.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
@Bean(name = "dbAuthenticationProvider")
public AuthenticationProvider dbAuthenticationProvider() {
	CustomDaoAuthenticationProvider daoAuthenticationProvider = new CustomDaoAuthenticationProvider();
	daoAuthenticationProvider.setUserDetailsService(userDetailsService());
	daoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
	return daoAuthenticationProvider;
}
 
Example #22
Source File: SecurityConfiguration.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Bean(name = "dbAuthenticationProvider")
@ConditionalOnMissingBean(AuthenticationProvider.class)
@ConditionalOnProperty(prefix = "flowable.idm.ldap", name = "enabled", havingValue = "false", matchIfMissing = true)
public AuthenticationProvider dbAuthenticationProvider(PasswordEncoder passwordEncoder, UserDetailsService userDetailsService) {
    CustomDaoAuthenticationProvider daoAuthenticationProvider = new CustomDaoAuthenticationProvider();
    daoAuthenticationProvider.setUserDetailsService(userDetailsService);
    daoAuthenticationProvider.setPasswordEncoder(passwordEncoder);
    return daoAuthenticationProvider;
}
 
Example #23
Source File: SecurityConfiguration.java    From grpc-spring-boot-starter with MIT License 5 votes vote down vote up
@Bean
/*
 * Add the authentication providers to the manager.
 */
AuthenticationManager authenticationManager() {
    final List<AuthenticationProvider> providers = new ArrayList<>();
    providers.add(jwtAuthenticationProvider());
    return new ProviderManager(providers);
}
 
Example #24
Source File: WithBasicAuthSecurityConfiguration.java    From grpc-spring-boot-starter with MIT License 5 votes vote down vote up
@Bean
AuthenticationManager authenticationManager() {
    final List<AuthenticationProvider> providers = new ArrayList<>();
    providers.add(daoAuthenticationProvider());
    // providers.add(anonymousAuthenticationProvider());
    return new ProviderManager(providers);
}
 
Example #25
Source File: WithBasicAuthSecurityConfiguration.java    From grpc-spring-boot-starter with MIT License 5 votes vote down vote up
@Bean
AuthenticationManager authenticationManager() {
    final List<AuthenticationProvider> providers = new ArrayList<>();
    providers.add(daoAuthenticationProvider());
    // providers.add(anonymousAuthenticationProvider());
    return new ProviderManager(providers);
}
 
Example #26
Source File: SecurityConfiguration.java    From grpc-spring-boot-starter with MIT License 5 votes vote down vote up
@Bean
// Add the authentication providers to the manager.
AuthenticationManager authenticationManager() {
    final List<AuthenticationProvider> providers = new ArrayList<>();
    providers.add(daoAuthenticationProvider());
    return new ProviderManager(providers);
}
 
Example #27
Source File: SecurityConfiguration.java    From code-examples with MIT License 5 votes vote down vote up
@Bean
public AuthenticationProvider daoAuthenticationProvider() {
  DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
  provider.setPasswordEncoder(passwordEncoder());
  provider.setUserDetailsPasswordService(this.databaseUserDetailPasswordService);
  provider.setUserDetailsService(this.databaseUserDetailsService);
  return provider;
}
 
Example #28
Source File: SecurityConfig.java    From Spring-Security-Third-Edition with MIT License 5 votes vote down vote up
@Bean
public AuthenticationProvider authenticationProvider(){
    ActiveDirectoryLdapAuthenticationProvider ap = new ActiveDirectoryLdapAuthenticationProvider(
                                                                "corp.jbcpcalendar.com",
                                                                   "ldap://corp.jbcpcalendar.com/");
    ap.setConvertSubErrorCodesToExceptions(true);
    return ap;
}
 
Example #29
Source File: SecurityConfig.java    From ChengFeng1.5 with MIT License 5 votes vote down vote up
@Bean("daoAuthenticationProvider")
protected AuthenticationProvider daoAuthenticationProvider() throws Exception{
    DaoAuthenticationProvider daoProvider = new DaoAuthenticationProvider();
    daoProvider.setPasswordEncoder(passwordEncoder());
    daoProvider.setUserDetailsService(userInfoService());
    return daoProvider;
}
 
Example #30
Source File: SecurityConfig.java    From Spring-Boot-Blog with MIT License 4 votes vote down vote up
/**
 * 自定义的密码验证策略
 * @return
 */
@Bean
public AuthenticationProvider authenticationProvider(){
    return new MyAuthenticationProvider();
}