我有带有自定义身份验证提供程序的 Spring Boot 3 项目。在此,我验证标头中发送的签名并确定请求是否有效。我的身份验证提供商没有被调用。我已经尝试解决这个问题好几天了。任何帮助表示赞赏。以下是我的代码
@Configuration
@EnableWebSecurity
public class WebSecurityConfiguration {
@Bean
public AuthenticationManager authManager(
HttpSecurity http, MyAuthenticationProvider authProvider) throws Exception {
AuthenticationManagerBuilder authenticationManagerBuilder = http
.getSharedObject(AuthenticationManagerBuilder.class);
authenticationManagerBuilder.authenticationProvider(authProvider);
return authenticationManagerBuilder.build();
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
return http
.authorizeHttpRequests(authorize -> authorize
.anyRequest().authenticated())
.build();
}
}
@Component
@Slf4j
public class MyAuthenticationProvider implements AuthenticationProvider {
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
// Autheticate and return UsernamePasswordAuthenticationToken
}
@Override
public boolean supports(Class<?> authentication) {
return authentication.equals(UsernamePasswordAuthenticationToken.class);
}
}
我可以在这里找到类似的示例https://github.com/dewantrie/springboot-custom-authentication-provider并且这有效。但我的项目中的类似设置不起作用。
我的安全依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.4</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-jwt</artifactId>
<version>1.1.1.RELEASE</version>
</dependency>
我的问题是 spring 不调用
MyAuthenticationProvider
我的身份验证发生的地方
您是否尝试将 AuthenticationManager 添加到您的过滤器链中?
有一个选项可以将 AuthanticationManager 添加到过滤器链:
.authenticationManager(authManager(http, authProvider))
从 Spring Security 5.2 开始,有一个 AuthenticationManagerResolver,集成在身份验证流程中。
在此处查看更多信息:https://www.baeldung.com/spring-security-authenticationmanagerresolver