我正在尝试通过@Autowired注释将过滤器JWTLoginFilter传递给WebSecurityConfig WebSecurityConfigurerAdapter。当JWTLoginFilter尝试从WebSecurityConfig获取AuthenticationManager时出现问题。
启动服务器时,出现以下错误:
描述: 应用程序上下文中某些bean的依赖性形成一个循环: 在文件[C:\ Users \ user \ workspace \ backend \ target \ classes \ pl \ dn \ schoolsystem \ service \ jwt \ JWTLoginFilter.class]中定义的JWTLoginFilter webSecurityConfig(字段pl.dn.schoolsystem.service.jwt.JWTLoginFilter pl.dn.schoolsystem.config.WebSecurityConfig.jwtLoginFilter)
描述:
应用程序上下文中某些bean的依赖性形成一个循环:
在文件[C:\ Users \ user \ workspace \ backend \ target \ classes \ pl \ dn \ schoolsystem \ service \ jwt \ JWTLoginFilter.class]中定义的JWTLoginFilter
webSecurityConfig(字段pl.dn.schoolsystem.service.jwt.JWTLoginFilter pl.dn.schoolsystem.config.WebSecurityConfig.jwtLoginFilter)
错误图片
我认为这种循环依赖注入。我对此卡住了,我不知道如何解决。
WebSecurityConfig:
@Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private Environment env; @Autowired private UserSecurityService userSecurityService; @Autowired JWTLoginFilter jwtLoginFilter; private static final String Salt = "salt"; // should be protected better @Bean public BCryptPasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(12, new SecureRandom(Salt.getBytes())); } @Bean(name = BeanIds.AUTHENTICATION_MANAGER) @Override public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable(). authorizeRequests().antMatchers("/").permitAll() .antMatchers(HttpMethod.POST, "/login").permitAll() .antMatchers("/admin/**").hasRole("ADMIN") .anyRequest().authenticated() .and() //.addFilterBefore(new JWTLoginFilter("/login", authenticationManager()), //UsernamePasswordAuthenticationFilter.class) .addFilterBefore(jwtLoginFilter, UsernamePasswordAuthenticationFilter.class) .addFilterBefore(new JWTAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userSecurityService).passwordEncoder(passwordEncoder()); } }
JWTLoginFilter:
@Component public class JWTLoginFilter extends AbstractAuthenticationProcessingFilter{ @Autowired UserService userService; @Autowired public JWTLoginFilter(@Value("/login") String url, AuthenticationManager authManager) { super(new AntPathRequestMatcher(url)); setAuthenticationManager(authManager); } @Override public Authentication attemptAuthentication(HttpServletRequest req, HttpServletResponse res) throws AuthenticationException, IOException, ServletException { System.out.println("Jestem w JwtLogginFilter.attemptAuthentication -------------------------------------"); AccountCredentials creds = new ObjectMapper() .readValue(req.getInputStream(), AccountCredentials.class); User user = userService.findByUsername(creds.getUsername()); return getAuthenticationManager().authenticate( new UsernamePasswordAuthenticationToken( creds.getUsername(), creds.getPassword(), user.getAuthorities() ) ); } @Override protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) throws IOException, ServletException { System.out.println("Jestem w JWTLogginFilter.successfulAuthentication -------------------------------------- "); System.out.println("authResult.getName(): " + authResult.getName()); TokenAuthenticationService.addAuthentication(response, authResult.getName()); } }
我正在使用Spring Boot 1.5.4。感谢您的建议
您WebSecurityConfig明确要求JWTLoginFilter将其注入其中,并JWTLoginFilter要求AuthenticationManager将其注入其构造函数中。AuthenticationManager由提供WebSecurityConfig,因此您具有循环依赖关系。
WebSecurityConfig
JWTLoginFilter
AuthenticationManager
@Component从中删除注释,JWTLoginFilter并将过滤器定义为Bean WebSecurityConfig:
@Component
@Bean public JWTLoginFilter jwtLoginFilter() { return new JWTLoginFilter("/login", authenticationManager()); }
您可能还需要UserService在此方法中手动注入(例如,通过构造函数)。
UserService