基于 SAML 断言授权的 SAML2 身份验证

     2023-02-27     103

关键词:

【中文标题】基于 SAML 断言授权的 SAML2 身份验证【英文标题】:SAML2 Authentication with authorization based on SAML assertions 【发布时间】:2022-01-17 03:21:12 【问题描述】:

我正在使用spring-security-saml2-service-provider 针对 SAML IdP 对我的 SpringBoot webapp 进行身份验证 - 这很有效。我还可以使用@AuthenticationPrincipal Saml2AuthenticatedPrincipal principal 访问 REST 控制器中的 SAML 断言,但我想做的是使用 Saml2AuthenticatedPrincipal 主体中的断言中的值限制通过 url 进行的访问 - 这是 SAML 联盟中发布值的常用方法eduPersonEntitlement,并据此决定访问权限。有人做过吗?我对此的所有研究/试验都一无所获。 这是我目前所拥有的:

@EnableWebSecurity
public class SAMLSecurityConfig extends WebSecurityConfigurerAdapter 

@Autowired
RelyingPartyRegistrationRepository relyingPartyRegistrationRepository;

protected void configure(HttpSecurity http) throws Exception 

    RelyingPartyRegistrationResolver relyingPartyRegistrationResolver =
    new DefaultRelyingPartyRegistrationResolver(this.relyingPartyRegistrationRepository);

    Saml2MetadataFilter filter = new Saml2MetadataFilter(relyingPartyRegistrationResolver, new OpenSamlMetadataResolver());
    
    http
        .saml2Login(withDefaults())
            .addFilterBefore(filter, Saml2WebSsoAuthenticationFilter.class).antMatcher("/**")
        .authorizeRequests()
            .anyRequest().authenticated();
    

我认为我需要将authenticated() 替换为可能与角色有关的东西,并在用户登录时以某种方式为用户设置角色,但对此一无所知。有什么想法吗?

【问题讨论】:

【参考方案1】:

好的,开始工作了....您需要自定义 saml2Login - 用新的自定义程序替换 withDefaults() 方法(下面的 Saml2LoginSettings):

SAMLSecurityconfig.java:

@EnableWebSecurity
public class SAMLSecurityConfig extends WebSecurityConfigurerAdapter 
  
    @Autowired
    RelyingPartyRegistrationRepository relyingPartyRegistrationRepository;

    @Autowired
    Saml2LoginSettings settings;
    
    protected void configure(HttpSecurity http) throws Exception 

        RelyingPartyRegistrationResolver relyingPartyRegistrationResolver =
        new DefaultRelyingPartyRegistrationResolver(this.relyingPartyRegistrationRepository);

        Saml2MetadataFilter filter = new Saml2MetadataFilter(relyingPartyRegistrationResolver, new OpenSamlMetadataResolver());

        http
            .saml2Login(settings)
                .addFilterBefore(filter, Saml2WebSsoAuthenticationFilter.class).antMatcher("/**")  // 
            .authorizeRequests()
            .antMatchers("/attributes").hasAuthority("ADMIN")
            .anyRequest().authenticated();

使用 Saml2LoginSettings.java:

@Component
class Saml2LoginSettings implements Customizer <Saml2LoginConfigurer<HttpSecurity>> 

    @Override
    public void customize(Saml2LoginConfigurer<HttpSecurity> t) 
   
        t.successHandler(new SavedRequestAwareAuthenticationSuccessHandler() 

            @Override
            public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
                    Authentication authentication) throws IOException, ServletException 

                authentication = assignAuthorities (authentication, request);
                super.onAuthenticationSuccess(request, response, authentication);
            
        ); 
    

分配权限有点麻烦,但这很管用:

    private Authentication assignAuthorities (Authentication authentication, HttpServletRequest request) 
        Collection<SimpleGrantedAuthority> oldAuthorities = (Collection<SimpleGrantedAuthority>)SecurityContextHolder.getContext()
                .getAuthentication().getAuthorities();

        DefaultSaml2AuthenticatedPrincipal princ = (DefaultSaml2AuthenticatedPrincipal) authentication.getPrincipal();
        if (princ.getAttribute("urn:oid:1.3.6.1.4.1.5923.1.1.1.7").contains("urn:mace:dir:entitlement:common-lib-terms")) 

            List<SimpleGrantedAuthority> updatedAuthorities = new ArrayList<SimpleGrantedAuthority>();
            updatedAuthorities.addAll(oldAuthorities);
            updatedAuthorities.add(new SimpleGrantedAuthority("ADMIN"));
            Saml2Authentication sAuth = (Saml2Authentication) authentication;

            sAuth = new Saml2Authentication(
                    (AuthenticatedPrincipal) SecurityContextHolder.getContext().getAuthentication().getPrincipal(),
                    sAuth.getSaml2Response(),
                    updatedAuthorities
            );
            SecurityContextHolder.getContext().setAuthentication(sAuth);

            return sAuth;
        
        else 
            return authentication;
    

示例代码here

【讨论】:

用于断言的 SAML 2.0 格式属性 - 联合身份

】用于断言的SAML2.0格式属性-联合身份【英文标题】:SAML2.0formatedattributeforassertion-FederationIdentity【发布时间】:2012-12-3109:58:04【问题描述】:我们有一个SAML2.0联合环境(IDP和SP)。我想为仅为一个SP创建的断言生成自定义属性。... 查看详情

支持基于saml2.0的联合身份验证,支持sqlserver数据库管理和审计,jumpserver堡垒机v2.17.0发布(代码片段)

...机正式发布v2.17.0版本。在这一版本中,JumpServer支持基于SAML2.0的联合身份验证,支持对Windows资产的键盘操作进行记录和审计,同时用户可以通过键盘操作快速定位Windows资产会话录像,并且支持对数据库操作的SQL... 查看详情

服务提供者之间的 SAML 2.0 身份验证断言(C#、.net、MVC4、组件空间)

】服务提供者之间的SAML2.0身份验证断言(C#、.net、MVC4、组件空间)【英文标题】:SAML2.0authenticationassertionbetweenserviceproviders(C#,.net,MVC4,ComponentSpace)【发布时间】:2012-08-2115:22:59【问题描述】:我正在构建的系统有问题。我已经成... 查看详情

如何使用 SAML 2.0 响应进行特定 DNN 用户角色组身份验证和授权?

】如何使用SAML2.0响应进行特定DNN用户角色组身份验证和授权?【英文标题】:HowtouseSAML2.0responseforspecificDNNuserrolegroupauthenticationandauthorization?【发布时间】:2019-11-1920:08:11【问题描述】:我正在寻找使用SAML2.0响应将外部用户登录... 查看详情

如何设置本地测试 SAML2.0 身份提供程序?

...2.0断言。我现在想测试这段代码。我知道我可以使用各种基于云的服务来充当我的测试身份提供者(IdP),但是这些需要公开我的新的、未经测试的SP端点。目前我只是 查看详情

将 java webapp 转换为使用 SAML2 身份验证

】将javawebapp转换为使用SAML2身份验证【英文标题】:convertjavawebapptouseSAML2authentication【发布时间】:2013-05-1215:24:16【问题描述】:我在尝试了解如何完成此任务时遇到了一些问题:1)具有数据库身份验证的现有简单webapp。(简单... 查看详情

JWT 身份验证回退到相同路径的 SAML2

】JWT身份验证回退到相同路径的SAML2【英文标题】:JWTauthenticationwithfallbacktoSAML2forthesamepath【发布时间】:2020-08-0804:39:29【问题描述】:我在我的一个SpringBoot应用程序中使用spring-security-saml2-service-provider进行身份验证,我在另一... 查看详情

使用 Node.js 和 SPA 进行 SAML2.0 身份验证

】使用Node.js和SPA进行SAML2.0身份验证【英文标题】:SAML2.0AuthenticationwithNode.jsandSPA【发布时间】:2017-11-1911:02:00【问题描述】:大约2天来,我一直在摸索如何解决看似简单的任务,但它开始让我发疯。我有一个应用程序,用户将... 查看详情

使用基于 SAML 的基本身份验证进行身份验证?

】使用基于SAML的基本身份验证进行身份验证?【英文标题】:AuthenticateusingSAML-basedBasicAuthentication?【发布时间】:2015-08-1219:55:40【问题描述】:我有一个用例,其中Web应用程序需要让用户以两种不同的方式进行身份验证,但通过... 查看详情

基于 SAML 的 SSO 用于身份验证和 LDAP 用于授权 - Spring Boot Security

】基于SAML的SSO用于身份验证和LDAP用于授权-SpringBootSecurity【英文标题】:SAMLbasedSSOforAuthenticationandLDAPforauthorization-SpringBootSecurity【发布时间】:2020-09-2721:06:21【问题描述】:将Gsuite与CloudIdentity专业版结合使用a)将GSuite初始化为iDP... 查看详情

无法针对架构验证 SAML 2.0 断言

】无法针对架构验证SAML2.0断言【英文标题】:unabletovalidateSAML2.0assertionagainstschema【发布时间】:2015-06-2920:16:49【问题描述】:我正在使用SpringSAML,但在使用SAML断言时遇到问题(来自Wikipediaarticle的示例断言<saml:Assertionxmlns:saml=... 查看详情

更改 Spring Security SAML2 登录的 URL

...个具有多种身份验证类型的应用程序(即基本和特殊的预授权登录)。我正在尝试在我的安全配置中添加SAML2RelyingParty注册,我正在尝试从以下位置更改默认路径:/login/saml2/sso/registration 查看详情

ITfoxtec.Identity.Saml2.Saml2RequestException:“不完全是一个断言元素。”

】ITfoxtec.Identity.Saml2.Saml2RequestException:“不完全是一个断言元素。”【英文标题】:ITfoxtec.Identity.Saml2.Saml2RequestException:\'ThereisnotexactlyoneAssertionelement.\'【发布时间】:2020-11-0415:28:04【问题描述】:早安,我们使用了ITfoxtec库版本1... 查看详情

如何读取加密的断言?

】如何读取加密的断言?【英文标题】:Howtoreadencryptedassertions?【发布时间】:2021-12-1019:41:59【问题描述】:我正在编写一个SAML2服务提供者/依赖方。我的IdP以加密形式(EncryptedAssertion元素)返回断言。ITfoxtec.Identity.Saml2是否支... 查看详情

spring-security saml2:如何获取当前用户?

...ervice-provider。我正在尝试通过IDP进行身份验证以获取当前断言,以便 查看详情

ITfoxtec - ADFS SAML2 根据验证程序,远程证书无效

...dure【发布时间】:2021-10-0711:44:44【问题描述】:我是SAML2身份验证的新手,并尝试过使用ITfoxtec。当我运行我的应用程序时出现此错误AuthenticationException 查看详情

是否可以在单个应用程序中拥有 SAML 2.0 身份验证和 JWT 身份验证?

】是否可以在单个应用程序中拥有SAML2.0身份验证和JWT身份验证?【英文标题】:IsitpossibletohaveSAML2.0AuthenticationandJWTAuthenticationinasingleApplication?【发布时间】:2019-10-2114:42:26【问题描述】:我们的应用程序目前有自己的使用JWT的身... 查看详情

.net 核心和 SAML 2.0

...13:45:27【问题描述】:我们的大学已迁移到使用Shibboleth的基于SAML的身份验证/授权方法。我至少已经研究了一天如何将SAML与.net核心Web应用程序一起使用。我不能使用身份服务器4,因为它用于OaTH/OpenID。我在msdn上找到了一些指向... 查看详情