我正在尝试使用spring security(spring-security-starter)在spring boot(2.2.4)应用中实现身份验证和授权。
用例:基于用户名,我想为特定的身份验证提供程序重定向用户
如果用户名以'einfochips.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等)
答案 0 :(得分:2)
利用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 Authentication Provider(由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);
}
...
}