gpt4 book ai didi

java - AuthenticationProcessingFilter 和 WebSecurityConfigurerAdapter 导致循环依赖

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:22:08 27 4
gpt4 key购买 nike

在我的 Spring Boot 应用程序中,我有以下两个类:

@EnableWebSecurity
public class AppSecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private JwtAuthenticationFilter jwtAuthenticationFilter;

@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http
// TODO re-enable csrf after dev is done
.csrf()
.disable()
// we must specify ordering for our custom filter, otherwise it
// doesn't work
.addFilterAfter(jwtAuthenticationFilter,
UsernamePasswordAuthenticationFilter.class)
// we don't need Session, as we are using jwt instead. Sessions
// are harder to scale and manage
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
}

和:

@Component
public class JwtAuthenticationFilter extends
AbstractAuthenticationProcessingFilter {

/*
* we must set authentication manager for our custom filter, otherwise it
* errors out
*/
@Override
@Autowired
public void setAuthenticationManager(
AuthenticationManager authenticationManager) {
super.setAuthenticationManager(authenticationManager);
}
}

JwtAuthenticationFilter 通过其 setAuthenticationManager 方法依赖于 AuthenticationManager bean,但该 bean 是在 AppSecurityConfig 中创建的有 JwtAuthenticationFilter Autowiring 。这整个事情创建了一个循环依赖。我该如何解决这个问题?

最佳答案

我按照此处的建议解决了这个问题: Cannot pass AuthenticationManager to custom filter by @Autowired

我从 JwtAuthenticationFilter 中删除了 @Component 而不是将 JwtAuthenticationFilter Autowiring 到 WebSecurityConfig 类,我定义了 bean那里:

@Bean
public JwtAuthenticationFilter JwtAuthenticationFilter() {
return new JwtAuthenticationFilter();
}

关于java - AuthenticationProcessingFilter 和 WebSecurityConfigurerAdapter 导致循环依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52510873/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com