当前位置: 首页 > 知识库问答 >
问题:

Java-应用程序上下文中某些bean的依赖关系形成一个循环:

艾晋
2023-03-14

我试图创建一个jwt与Spring安全,但当我启动程序,我采取这个错误从Spring:

The dependencies of some of the beans in the application context form a cycle:

   authController (field private org.springframework.security.authentication.AuthenticationManager Spring.JWTDEMO.Api.AuthController.manager)
┌─────┐
|  webSecurityConfig (field private Spring.JWTDEMO.auth.UserDetailsService Spring.JWTDEMO.WebSecurityConfig.userService) 
↑     ↓
|  userDetailsService (field private org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder Spring.JWTDEMO.auth.UserDetailsService.passwordEncoder)
└─────┘


Action:

Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.

İt也是一个spring web程序,因此该程序与控制器一起工作。此控制器用于创建带有登录名的jwt。以下是我的authController类:

import Spring.JWTDEMO.auth.TokenManager;
import Spring.JWTDEMO.requests.LoginRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/login")
public class AuthController {
    @Autowired
    private TokenManager tokenManager;
    @Autowired
    private AuthenticationManager manager;

    @PostMapping
    public ResponseEntity<String> login(@RequestBody LoginRequest loginRequest){

        try {
       manager.authenticate(new UsernamePasswordAuthenticationToken(loginRequest.getUsername(),loginRequest.getPassword()));
       return ResponseEntity.ok(tokenManager.tokenGenerator(loginRequest.getUsername()));
        }catch (Exception e){
            throw e;
        }

    }
}

这里是WebServiceConfig类:

import Spring.JWTDEMO.auth.TokenFilter;
import Spring.JWTDEMO.auth.UserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true) 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private TokenFilter tokenFilter;
    @Autowired
    private UserDetailsService userService; 

   
    @Autowired  
    public void configurePasswordEncoder(AuthenticationManagerBuilder builder) throws Exception {
        builder.userDetailsService(userService).passwordEncoder(getBCryptPasswordEncoder());
      
    }

    @Bean
    public BCryptPasswordEncoder getBCryptPasswordEncoder(){
        return new BCryptPasswordEncoder();
    }

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

   
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeRequests().antMatchers("/login").permitAll()
                .anyRequest().authenticated()
                .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        http.addFilterBefore(tokenFilter, UsernamePasswordAuthenticationFilter.class);
    }
}

以下是UserDetailsService:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


@Service
public class UserDetailsService  implements org.springframework.security.core.userdetails.UserDetailsService {

    private Map<String ,String> users= new HashMap();

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @PostConstruct
    public void init(){
        users.put("Asd",passwordEncoder.encode("123"));
    }

   
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
       if(users.containsKey(username)){
           return new User(username,users.get(username),new ArrayList<>());
       }
       throw new UsernameNotFoundException(username);
    }
}

谢谢你的帮助阿莱尔迪

共有1个答案

郜昊苍
2023-03-14

-

  1. 简单而推荐的:

将这一行添加到应用程序中。属性

spring.main.allow-circular-references=true

**如果你刚刚读了你的控制台信息,你就可以得到问题的解决方案,下次尝试第一次读你的控制台,因为90%的解决方案都在那里:)**祝你好运

 类似资料:
  • 我正在使用JPA开发Spring Boot v1.4.2.RELEASE应用程序。 我定义了存储库接口和实现 存储库 A存储自定义 ARepositoryImpl公司 和一个服务AServiceImpl 我的应用程序不会以以下消息开始: 我遵循http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositorie

  • 当我运行我的应用程序时,我得到了下面显示的错误,即应用程序上下文中一些bean的依赖关系形成了一个循环。我不确定问题到底是从哪里来的。对于Spring Boot/Spring Security性来说还是相对较新的,所以如果您能在我的代码上提供任何提示,我们将不胜感激。 UserServiceImpl 安全配置

  • 几周前,我将Spring的版本从1.4.x.RELEASE升级到1.5.1.RELEASE。 突然之间,由于这个错误,启动Spring Boot服务成了一场斗争: “应用程序上下文中某些bean的依赖关系形成一个循环” 同样的代码,不同的版本 这很有趣,因为在我的本地环境(OSX)上,相同的代码通常都可以正常启动,但在Heroku上,在不同的部署上会随机失败(在类路径类解析上看起来是不同的顺序,由

  • 应用程序上下文中一些bean的依赖关系形成了一个循环: 我试过用@Lazy,但没用。 当我尝试将构造函数更改为setter时,它会给我错误,例如: 证券配置 UserServiceImpl JWTManager JWTManager是一个自定义类,负责生成新的JWT。它使用来自auth0的java jwt库。通用域名格式。我使用公钥/私钥对令牌进行签名。 AuthController用@RestC

  • 异常0:org.jboss.weld.exceptions.deploymentexception:weld-001443:伪作用域bean具有循环依赖关系。依赖关系路径:-托管Bean[class com.my.own.bounded_contexts.client.cache.CacheClientCommPriorizedAcceptRequestService],带有限定符[@any@de

  • 我花了5个多小时试图解决这个问题。有什么问题吗? 我补充道: 组织。格拉德尔。configureondemand=true 但问题依然存在 建筑gradle(模块:应用程序) 建造。gradle(项目:myproject) //顶级构建文件,您可以在其中添加所有子项目/模块通用的配置选项。