在 Spring security 中,我想对以 api/** 开头的 url 使用基本身份验证,对于以/ldap/ 开头的 url 使用 LDAP Rest 身份验证.我的当前代码也允许 ldap/具有基本身份验证。
即使我将它们用作单独的 AuthenticationProviders(如 LdapAuthProvider 和 BasicAuthProvider),问题仍然存在,我如何使用它来指向特定的 url
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {
@Configuration
@Order(1)
public class BasicAuthenticationProvider extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/swagger-ui*", "/info", "/health").permitAll()
.and().authorizeRequests().antMatchers("/order/**").fullyAuthenticated()
.and().httpBasic().and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and().csrf().disable()
.anonymous().disable();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(inMemoryUserDetailsManager());
}
}
@Configuration
@Order(2)
public class LdapAuthenticationProvider extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/ldap/**").fullyAuthenticated().and().httpBasic()
.and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().csrf().disable();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// auth.ldapAuthentication() code here......
}
}
}
最佳答案
据我了解,您在一个应用程序中有多个入口点,并且有不同类型的用户可以访问应用程序的不同部分。
你应该看看这个 Baeldung 教程:Multiple Entry Points in Spring Security
关于spring - 特定 url 的多个身份验证提供程序 - Spring Boot Security,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55213278/