GithubHelp home page GithubHelp logo

Comments (1)

Snailclimb avatar Snailclimb commented on May 27, 2024

最近在学习这个项目中的Spring Security,用其中的代码做了一个自己的项目,在线下用mvn packagejava -jar都运行正常,使用DevOps部署到线上出现了循环依赖的异常。(为了使idea生成下面的图,加了几处@Autowired注解)

  1. 要生成userController,就需要通过自动装配方式使用userService

    public class UserController {
     private final UserService userService;
  2. 其中userService需要bCryptPasswordEncoder,也是通过@Autowired方式装配

    public class UserService {
     @Autowired
     private final BCryptPasswordEncoder bCryptPasswordEncoder;
  3. bCryptPasswordEncodersecurityConfig中由注解@Bean制造出

    public class SecurityConfig extends WebSecurityConfigurerAdapter {
       @Bean
       public BCryptPasswordEncoder bCryptPasswordEncoder() {
           return new BCryptPasswordEncoder();
       }

    bCrypt

  4. 在此之前,securityConfig需要得到userDetailsServiceImpl

    public class SecurityConfig extends WebSecurityConfigurerAdapter {
     @Autowired
     UserDetailsServiceImpl userDetailsServiceImpl;
  5. userDetailsServiceImpl又需要使用userService来调用它的find方法

    @Service
     public class UserDetailsServiceImpl implements UserDetailsService {
    
        private final UserService userService;
    
        @Autowired
        public UserDetailsServiceImpl(UserService userService) {
            this.userService = userService;
        }
    
        @Override
        public UserDetails loadUserByUsername(String name) {
            User user = userService.find(name);
            return new JwtUser(user);
        }
    
      }

    UserDetailsServiceImpl

  6. 但此时的userService还没有制造好……于是有了循环依赖异常org.springframework.beans.factory.UnsatisfiedDependencyException --> UnsatisfiedDependencyException --> ... --> beans.factory.BeanCurrentlyInCreationException

image
IMG_1515

通过改写loadUserByUsername的方式,去除了UserDetailsServiceImplUserService的依赖,临时性地解决了这个异常。

不好意思 ,刚看到。我目前把登录接口暴露出去,这个问题也没有了。

from spring-security-jwt-guide.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.