'how do I update my application to remove the dependency cycle between beans
I am getting a cycle dependency error on my spring authentication service class and Security Configuration class.
***************************
APPLICATION FAILED TO START
***************************
Description:
The dependencies of some of the beans in the application context form a cycle:
┌─────┐
| securityConfiguration defined in file [C:\Users\SRO\Documents\repositories\blackjack\blackjack-backend\target\classes\com\app\blackjack\configurations\SecurityConfiguration.class]
↑ ↓
| authenticationService defined in file [C:\Users\SRO\Documents\repositories\blackjack\blackjack-backend\target\classes\com\app\blackjack\services\AuthenticationService.class]
└─────┘
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.
I'm making a simple sign up + login functionality for my spring project and the only way I've learned, so far, how to login a user is by injecting the authentication manager in the service class to authenticate the login information.
My guess is that is interfering with the Security Configuration's @bean annotation but I'm not sure how to fix that. Any help?
SecurityConfiguration
@Configuration
@AllArgsConstructor
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
private final AuthenticationService authservice;
private final PasswordEncoder passwordEncoder;
@Bean(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("/api/v*/registration/**")
.permitAll()
.anyRequest()
.authenticated().and()
.formLogin();
}
protected void configureGlobal(AuthenticationManagerBuilder authenticationManager) throws Exception {
authenticationManager.userDetailsService(authservice)
.passwordEncoder(passwordEncoder.bCryptPasswordEncoder());
}
}
AuthenticationService.java
@Service
@AllArgsConstructor
public class AuthenticationService implements UserDetailsService {
private final PasswordEncoder passwordEncoder;
private final AuthenticationManager authenticationManager;
private final UserRepository userRepository;
// TODO find a place to store these error messages properly
private final static String USER_NOT_FOUND_MSG = "user with username %s not found";
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
// TODO Auto-generated method stub
return null;
}
public ResponseEntity<String> signup(RegisterRequest registerRequest) {
AppUser user = new AppUser();
user.setUsername(registerRequest.getUsername());
user.setEmail(registerRequest.getEmail());
user.setPassword(passwordEncoder.encode( registerRequest.getPassword() ));
//TODO fix proper assignments
user.setEnabled(true);
user.setLocked(false);
try {
userRepository.save(user);
} catch (DataIntegrityViolationException e)
{
return new ResponseEntity<String>("user failed to save", HttpStatus.BAD_REQUEST );
}
return new ResponseEntity<String>("successfully created user", HttpStatus.OK);
}
public ResponseEntity<String> login(LoginRequest loginRequest) {
org.springframework.security.core.Authentication authenticate = null;
try {
authenticate = authenticationManager.authenticate
(new UsernamePasswordAuthenticationToken(loginRequest.getEmail(), loginRequest.getPassword()));
} catch (Exception e ) {
return new ResponseEntity("FAILED", HttpStatus.BAD_REQUEST );
}
return new ResponseEntity<String>("SUCCESS", HttpStatus.ACCEPTED);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
