【发布时间】:2022-01-22 04:15:39
【问题描述】:
我正在尝试从 Active Directory 返回的 SPNEGO 令牌中获取角色名称,以用于 Spring Security 授权。我使用kerb4j 进行身份验证,因为我的理解是它可以使用this code 从令牌(而不是后续的LDAP 查询)中获取组(即角色)信息。
在我的 Spring Web 安全配置中,我有以下内容:
class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Value("${app.service-principal}")
private String servicePrincipal;
@Value("${app.keytab-location}")
private String keytabLocation;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.exceptionHandling()
.authenticationEntryPoint(spnegoEntryPoint())
.and()
.authorizeRequests().antMatchers("/", "/home").permitAll()
.antMatchers("/hello").access("hasRole('ROLE_ADMIN')")
.anyRequest().authenticated()
.and()
.formLogin().loginPage("/login").permitAll()
.and()
.logout().permitAll().and() //spring
.addFilterBefore(spnegoAuthenticationProcessingFilter(authenticationManagerBean()),
BasicAuthenticationFilter.class);
}
@Bean
public SpnegoAuthenticationProvider kerberosServiceAuthenticationProvider() {
SpnegoAuthenticationProvider provider = new SpnegoAuthenticationProvider();
provider.setTicketValidator(sunJaasKerberosTicketValidator());
provider.setExtractGroupsUserDetailsService(new ExtractGroupsUserDetailsService());
provider.setServerSpn(servicePrincipal);
return provider;
}
ExtractGroupsUserDetailsService 仅获得 SID,例如 (S-1-2-20-132925241-12333....),而不是 AD 组名称,例如 ADMIN。怎么写ExtractGroupsUserDetailsService来提取群名? SPNEGO 令牌中是否提供此信息?
更新
简单地将hasRole SpEL 中的 ROLE_ADMIN 替换为 SID 不起作用。
更新 2
给定的 SID 字符串与传入 hasRole 的 SID 字符串不匹配,因为 hasRole 附加了 ROLE_ 传入的字符串。一旦我将ExtractGroupsUserDetailsService 更改为 SID 的前缀“ROLE_”(例如 ROLE_S-1-2-20-132925241-12333....),匹配就起作用了。
尽管如此...如何在 ExtractGroupsUserDetailsService 中获取组名(例如 ADMIN)而不是 SID?
【问题讨论】:
标签: spring-security active-directory kerberos spnego spring-security-kerberos