Spring boot 每个url模式的身份验证提供程序-Spring启动
Spring boot 每个url模式的身份验证提供程序-Spring启动,spring-boot,spring-security,Spring Boot,Spring Security,在使用Spring引导安全性为每个url模式配置不同的身份验证提供程序时,我遇到了一个问题。我正试图在SpringBoot应用程序中配置安全性,并希望在基本身份验证和所有API仅由令牌保护的情况下使用swagger。我让它几乎可以工作,但注意到API除了由IDAuthProvider类验证的令牌保护之外,它还由基本身份验证保护。我不想这样,还注意到,如果我删除该行: sessionCreationPolicy(sessionCreationPolicy.STATELESS)。 它似乎工作正常,但
在使用Spring引导安全性为每个url模式配置不同的身份验证提供程序时,我遇到了一个问题。我正试图在SpringBoot应用程序中配置安全性,并希望在基本身份验证和所有API仅由令牌保护的情况下使用swagger。我让它几乎可以工作,但注意到API除了由IDAuthProvider类验证的令牌保护之外,它还由基本身份验证保护。我不想这样,还注意到,如果我删除该行:
sessionCreationPolicy(sessionCreationPolicy.STATELESS)。
它似乎工作正常,但仍然在请求中添加了头基本{token},这是我不想要的您知道我如何配置它,使所有的招摇过市的东西都由基本身份验证保护,API的东西由令牌保护吗 我的配置如下所示:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Configuration
@Order(1)
public class SwaggerSecurityConfig extends WebSecurityConfigurerAdapter {
private final AuthenticationProvider userPassAuthProvider;
@Autowired
SwaggerSecurityConfig(UserPassAuthProvider userPassAuthProvider) {
this.userPassAuthProvider = userPassAuthProvider;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher("/swagger**").
authorizeRequests().
antMatchers("/swagger**").authenticated().
and().httpBasic().and().csrf().disable();
}
@Override
public void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth.authenticationProvider(userPassAuthProvider);
}
}
@Configuration
@Order(2)
public class APISecurityConfig extends WebSecurityConfigurerAdapter {
private final AuthenticationProvider idAuthProvider;
@Autowired
APISecurityConfig(IDAuthProvider idAuthProvider) {
this.idAuthProvider = idAuthProvider;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher("/api/v1/**").
authorizeRequests().anyRequest().authenticated().
and().
addFilterBefore(idpAuthenticationFilter(authenticationManager()), BasicAuthenticationFilter.class).sessionManagement().
and().
csrf().disable();
}
@Override
public void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth.authenticationProvider(idAuthProvider);
}
IDPAuthenticationFilter idpAuthenticationFilter(AuthenticationManager auth) {
return new IDPAuthenticationFilter(auth, new OrRequestMatcher(new AntPathRequestMatcher(ApiRouter.API_PATH + "/**", HttpMethod.GET.toString()), new AntPathRequestMatcher(ApiRouter.API_PATH + "/**", HttpMethod.POST.toString()), new AntPathRequestMatcher(ApiRouter.API_PATH + "/**", HttpMethod.DELETE.toString()), new AntPathRequestMatcher("/swagger**", HttpMethod.GET.toString())));
}
}
}