我有一个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中,我已将角色添加到声明值,如下图所示
我的目标是能够做出类似以下的事情:
@GetMapping("/")
@PreAuthorize("hasRole('User')")
public String getSample(Principal principal) {
log.info("Get Request");
return "Hello";
}
下一步将是实现您自己的SAMLUserDetailsService
,它将返回相应的UserDetail
实例,并授予用户权限Authorities
。
您必须从SAMLCredential检索Azure角色列表(类似于credential.getAtttributeAsString(<your_attribute_name>)
,然后您必须将这些值映射到应用程序中定义的权限列表。