chi*_*oni 2 spring-security saml saml-2.0 spring-boot spring-security-saml2
我正在尝试使用 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);
}
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以指导我,以便我可以配置为可以使用任何 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)
}
}
Run Code Online (Sandbox Code Playgroud)
自定义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
}
}
Run Code Online (Sandbox Code Playgroud)
然后,在您的中注册两个身份验证提供程序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);
}
...
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2281 次 |
最近记录: |