使用Spring Security在一个应用程序中合并数据库和SAML身份验证

问题描述 投票:0回答:1

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

用例:基于用户名,我想为特定的身份验证提供程序重定向用户

  • 如果用户名以'mit.com']结尾>使用数据库验证用户身份(我正在使用休眠)-为此,我可以使用spring的UserDetailService
  • 如果用户名以'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等)

我正在尝试使用spring security(spring-security-starter)在spring boot(2.2.4)应用中实现身份验证和授权。用例:基于用户名,我要将用户重定向到...

spring-boot spring-security saml saml-2.0 spring-security-saml2
1个回答
0
投票
自定义数据库身份验证提供程序

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) } }

© www.soinside.com 2019 - 2024. All rights reserved.