#Circular reference problem (im new in spring)

1 messages · Page 1 of 1 (latest)

vocal raft
#

I'm getting the issue "Error creating bean with name 'webSecurityConfiguration': Requested bean is currently in creation: Is there an unresolvable circular reference?"
And i'm not sure how to solve it as i tried to use @Lazy all over, tried to figure out the bean structure, could you help me out?


@Configuration
@EnableWebSecurity
public class WebSecurityConfiguration {

    private final UserService userService;

    public WebSecurityConfiguration(UserService userService) {
        this.userService = userService;
    }

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

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        // Using lambdas
        http.
                authorizeHttpRequests((authorize) -> authorize
                        .requestMatchers("/registration").fullyAuthenticated().anyRequest().denyAll()
                        .requestMatchers("/admin/**").hasRole(String.valueOf(Role.ADMIN))
                        .requestMatchers("/").permitAll()
                        .anyRequest().authenticated()
                )
                .formLogin((formLogin) -> formLogin
                        .loginPage("/login")
                        .defaultSuccessUrl("/")
                        .permitAll()
                )
                .logout((logout) ->logout
                        .logoutUrl("/logout")
                        .logoutSuccessUrl("/")
                        .permitAll());
        return http.build();
    }

    @Autowired
    protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userService).passwordEncoder(bCryptPasswordEncoder());
    }
}
wet shadowBOT
# vocal raft I'm getting the issue "Error creating bean with name 'webSecurityConfiguration':...

Detected code, here are some useful tools:

Formatted code
@Configuration
@EnableWebSecurity
public class WebSecurityConfiguration {
  private final UserService userService;
  public WebSecurityConfiguration(UserService userService) {
    this .userService = userService;
  }
  @Bean
  public BCryptPasswordEncoder bCryptPasswordEncoder() {
    return new BCryptPasswordEncoder();
  }
  @Bean
  public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    // Using lambdas
    http.authorizeHttpRequests((authorize) -> authorize.requestMatchers("/registration").fullyAuthenticated().anyRequest().denyAll().requestMatchers("/admin/**").hasRole(String.valueOf(Role.ADMIN)).requestMatchers("/").permitAll().anyRequest().authenticated()).formLogin((formLogin) -> formLogin.loginPage("/login").defaultSuccessUrl("/").permitAll()).logout((logout) -> logout.logoutUrl("/logout").logoutSuccessUrl("/").permitAll());
    return http.build();
  }
  @Autowired
  protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(userService).passwordEncoder(bCryptPasswordEncoder());
  }
}
#

<@&1004656351647117403> please have a look, thanks.

vocal raft
#

I think i should separate the encoding bean into another class(?)
as i'm using it in userService class

        user.setRole(Role.USER);
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        userRepository.save(user);
        return true;