我正在尝试使用 spring 的 MockMvc 类在我的 spring 启动应用程序中测试我的 spring OAuth2 授权和身份验证。我面临的基本问题是,即使我将自定义身份验证提供程序注册为 Spring Security 使用的身份验证提供程序之一,也永远不会调用它。我遵循了此处和此处找到的 spring 安全教程。
代码片段: 安全配置器类 - 这是添加自定义身份验证提供程序的地方。
@Configuration
@EnableWebSecurity
public class SecurityConfigurer extends WebSecurityConfigurerAdapter {
@Autowired
private AuthenticationProvider authenticationProvider;
@Override
public void configure(AuthenticationManagerBuilder auth) {
auth.authenticationProvider(authenticationProvider);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.httpBasic();
}
}
Run Code Online (Sandbox Code Playgroud)
自定义身份验证提供程序 - 这应该进行实际的身份验证
@Component
public class UsernamePasswordAuthProvider implements AuthenticationProvider {
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String username = authentication.getName(); …Run Code Online (Sandbox Code Playgroud)