我的应用程序中有两个不同的用户。Ldap用户和api用户。Ldap用户具有访问端点的权限,而api用户则具有不同的端点的权限。我已经使用UserDetailsService实现了api用户身份验证,并在我的application.yaml文件中包含了详细信息。我现在面临的问题是,我的api用户也正在访问只有Ldap用户应访问的终结点。我该如何预防。请在下面找到我的代码段
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();
}
}
在春季安全性中,确实有可能注册多个身份验证机制。
但是您不能将特定的身份验证提供程序注册到特定的路由。
春天的安全文档说:
ProviderManager
是的最常用实现AuthenticationManager
。ProviderManager
委托给List
的AuthenticationProviders
。每个人AuthenticationProvider
都有机会表明认证应该成功,失败,或者表明它不能做出决定并允许下游AuthenticationProvider
进行决定。
因此,在每个请求中,AuthenticationProvider
都将一个接一个地检查注册的s,直到一个成功或全部失败。
要解决您的问题,您需要定义多个自定义权限,并为您分配用户。
然后,您可以使用这些权限保护端点。
例如,您赋予每个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")
(...)
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句