ActiveDirectoryLdapAuthenticationProvider 和使用 userDetailsService 的身份验证
ActiveDirectoryLdapAuthenticationProvider and authentication using userDetailsService
我的应用程序中有两个不同的用户。 Ldap 用户和 api 用户。 Ldap 用户有权访问端点,而 api 用户有权访问不同的端点。我已经使用 UserDetailsService 实现了 api 用户身份验证,并在我的 application.yaml 文件中包含详细信息。
我现在面临的问题是,只有 Ldap 用户应该访问的端点现在也被我的 api 用户访问。我该如何防止这种情况。请在下面找到我的代码片段
public class ServiceSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
@Qualifier("ldapProvider")
private AuthenticationProvider authenticationProvider;
@Override
protected void configure(HttpSecurity http) throws Exception {
// security for apiuser
http
.authorizeRequests()
.antMatchers(“/abcd/**).hasRole(“admin”)
.and()
.httpBasic().and().userDetailsService(userDetailsService());
// security for ldap users
http
.csrf().disable()
.authorizeRequests()
.antMatchers(“/ghhgh” + "/**").fullyAuthenticated()
.antMatchers("/login*").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().and()
.authenticationProvider(authenticationProvider)
.exceptionHandling();
}
public UserDetailsService userDetailsService() {
UserDetails user = User.withUsername(“api”)
.password(passwordEncoder().encode(“test”))
.roles(“admin”)
return new InMemoryUserDetailsManager(user);
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
在spring安全中确实可以注册多个身份验证机制。
但是您不能将特定的身份验证提供程序注册到特定的路由。
spring 安全文档说:
ProviderManager
is the most commonly used implementation of AuthenticationManager
. ProviderManager
delegates to a List
of AuthenticationProviders
. Each AuthenticationProvider
has an opportunity to indicate that authentication should be successful, fail, or indicate it cannot make a decision and allow a downstream AuthenticationProvider
to decide.
所以在每次请求中,注册的AuthenticationProvider
一个接一个地检查,直到一个成功,或者全部失败。
要解决您的问题,您需要定义多个自定义权限,然后分配给您的用户。
然后您使用这些权限保护您的端点。
例如您为每个 ldap 用户赋予 LDAP_USER
权限,为每个 api 用户赋予 API_USER
权限。然后相应地配置您的安全性:
注册所有 AuthenticationProvider:
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(ldapProvider);
auth.userDetailsService(userDetailsService());
}
并配置端点:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
(...)
.authorizeRequests()
// security for apiuser
.antMatchers(“/abcd/**).hasRole(“API_USER”)
// security for ldap users
.antMatchers(“/ghhgh” + "/**").hasRole("LDAP_USER")
(...)
}
我的应用程序中有两个不同的用户。 Ldap 用户和 api 用户。 Ldap 用户有权访问端点,而 api 用户有权访问不同的端点。我已经使用 UserDetailsService 实现了 api 用户身份验证,并在我的 application.yaml 文件中包含详细信息。 我现在面临的问题是,只有 Ldap 用户应该访问的端点现在也被我的 api 用户访问。我该如何防止这种情况。请在下面找到我的代码片段
public class ServiceSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
@Qualifier("ldapProvider")
private AuthenticationProvider authenticationProvider;
@Override
protected void configure(HttpSecurity http) throws Exception {
// security for apiuser
http
.authorizeRequests()
.antMatchers(“/abcd/**).hasRole(“admin”)
.and()
.httpBasic().and().userDetailsService(userDetailsService());
// security for ldap users
http
.csrf().disable()
.authorizeRequests()
.antMatchers(“/ghhgh” + "/**").fullyAuthenticated()
.antMatchers("/login*").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().and()
.authenticationProvider(authenticationProvider)
.exceptionHandling();
}
public UserDetailsService userDetailsService() {
UserDetails user = User.withUsername(“api”)
.password(passwordEncoder().encode(“test”))
.roles(“admin”)
return new InMemoryUserDetailsManager(user);
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
在spring安全中确实可以注册多个身份验证机制。
但是您不能将特定的身份验证提供程序注册到特定的路由。
spring 安全文档说:
ProviderManager
is the most commonly used implementation ofAuthenticationManager
.ProviderManager
delegates to aList
ofAuthenticationProviders
. EachAuthenticationProvider
has an opportunity to indicate that authentication should be successful, fail, or indicate it cannot make a decision and allow a downstreamAuthenticationProvider
to decide.
所以在每次请求中,注册的AuthenticationProvider
一个接一个地检查,直到一个成功,或者全部失败。
要解决您的问题,您需要定义多个自定义权限,然后分配给您的用户。
然后您使用这些权限保护您的端点。
例如您为每个 ldap 用户赋予 LDAP_USER
权限,为每个 api 用户赋予 API_USER
权限。然后相应地配置您的安全性:
注册所有 AuthenticationProvider:
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(ldapProvider);
auth.userDetailsService(userDetailsService());
}
并配置端点:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
(...)
.authorizeRequests()
// security for apiuser
.antMatchers(“/abcd/**).hasRole(“API_USER”)
// security for ldap users
.antMatchers(“/ghhgh” + "/**").hasRole("LDAP_USER")
(...)
}