如何在 Spring Boot 中从 Azure SAML 2.0 应用程序获取角色或组

Posted

技术标签:

【中文标题】如何在 Spring Boot 中从 Azure SAML 2.0 应用程序获取角色或组【英文标题】:How can I get roles or groups from Azure SAML 2.0 Application in Spring Boot 【发布时间】:2019-07-20 06:29:33 【问题描述】:

我有一个 Spring Boot 应用程序,我需要在其中限制对特定端点的访问。到目前为止,我可以使用 SAML 2.0 对 Azure 进行身份验证。

这是Spring中认证的主要配置

@Override
	protected void configure(HttpSecurity http) throws Exception 

		http
				.exceptionHandling()
				.authenticationEntryPoint(samlEntryPoint());

		http
				.csrf()
				.disable();


		http
				.addFilterBefore(metadataGeneratorFilter(), ChannelProcessingFilter.class)
				.addFilterAfter(samlFilter(), BasicAuthenticationFilter.class);

		http
				.authorizeRequests()
				.antMatchers("/error").permitAll()
				.antMatchers("/saml/**").permitAll()
				.anyRequest().authenticated();

		http
				.logout()
				.logoutSuccessUrl("/");

	

在 Azure 中,我已将角色添加到声明值中,如下图所示

Azure Claims

我的目标是能够立即执行以下操作:

@GetMapping("/")
	@PreAuthorize("hasRole('User')")
	public String getSample(Principal principal) 
		log.info("Get Request");
		return "Hello";
	

【问题讨论】:

【参考方案1】:

下一步是实现您自己的SAMLUserDetailsService,它将返回相应的UserDetail 实例,并授予用户Authorities 权限。

您必须从 SAMLCredential 中检索 Azure 角色列表(类似于 credential.getAtttributeAsString(<your_attribute_name>),然后您必须将这些值与您的应用程序中定义的权限列表进行映射。

【讨论】:

非常感谢您的提示,搜索并为我工作。

以上是关于如何在 Spring Boot 中从 Azure SAML 2.0 应用程序获取角色或组的主要内容,如果未能解决你的问题,请参考以下文章