使用spring安全性将数据库和SAML身份验证结合到一个应用程序中



我正在尝试使用spring-security(spring-security-starter(在spring-boot(2.2.4(应用程序中实现身份验证和授权。

用例:根据用户名,我想将用户重定向到特定的身份验证提供商

  • 如果用户名以'mit.com'结尾使用数据库对用户进行身份验证(我使用的是hibernate(-为此,我可以使用spring的UserDetailService
  • 如果用户名以'infochips.com'结尾使用SAML 2.0对用户进行身份验证协议-使用身份提供者,如Okta,SSOCircle,OneLogin等

    我不知道如何做到这一点。我尝试使用自定义过滤器,但无法做到。

我读了很多文章,但都没能做到。

我在下面编写了仅使用SAML进行身份验证的代码。它运行良好。将用户带到okta-idp进行登录。

package com.example.demo;
import static org.springframework.security.extensions.saml2.config.SAMLConfigurer.saml;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.saml.userdetails.SAMLUserDetailsService;
@EnableWebSecurity
@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
SAMLUserDetailsService userDetailsService;
@Value("${security.saml2.metadata-url}")
String metadataUrl;
@Value("${server.ssl.key-alias}")
String keyAlias;
@Value("${server.ssl.key-store-password}")
String password;
@Value("${server.port}")
String port;
@Value("${server.ssl.key-store}")
String keyStoreFilePath;   
//Uisng SAML2.0
@Override
protected void configure(final HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.antMatchers("/").permitAll()
.anyRequest().authenticated()
.and()
.apply(saml())
.serviceProvider()
.keyStore()
.storeFilePath(this.keyStoreFilePath)
.password(this.password)
.keyname(this.keyAlias)
.keyPassword(this.password)
.and()
.protocol("https")
.hostname(String.format("%s:%s", "localhost", this.port))
.basePath("/")
.and().userDetailsService(userDetailsService)
.identityProvider()
.metadataFilePath(this.metadataUrl);
}
}

任何人都可以指导我,这样我就可以这样配置,我可以使用任何IDP,如okta,ssocircle,OneLogin等

利用Spring Security的AuthenticationProvider实现多个自定义身份验证提供程序,并按适当的顺序进行注册(按顺序进行评估(。

自定义数据库身份验证提供程序

public class MitComAuthProvider implements AuthenticationProvider {
public Authentication authenticate(Authentication auth) {
// if user matches 'mit.com', auth with database
// look up and auth
// else return null (to try next auth provider)
}
}

一个自定义的SAML身份验证提供程序(由Spring Security提供并实现AuthenticationProvider(。

public class EInfoChipsAuthProvider extends SAMLAuthenticationProvider {
public Authentication authenticate(Authentication auth) {
// if user matches 'einfochips.com', auth with SAML
// super.authentication(auth)
// else return null (to try next auth provider) or throw auth exception
}
}

然后,在WebSecurityConfigurerAdapter中注册两个身份验证提供商

public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private MitComAuthProvider mitComAuthProvider;
@Autowired
private EInfoChipsAuthProvider eInfoChipsAuthProvider;
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(mitComAuthProvider);
auth.authenticationProvider(eInfoChipsAuthProvider);
}
...
}

相关内容

最新更新