I was following a tutorial on YouTube on SpringSecurity and ran into the following error:
DESCRIPTION:
Parameter 0 of constructor in com.example.demo.controllers.AuthenticationController required a bean of type ‘org.springframework.security.authentication.AuthenticationManager’ that could not be found.
ACTION:
Consider defining a bean of type ‘org.springframework.security.authentication.AuthenticationManager’ in your configuration.
But I’ve already declared a bean in the SecurityConfig class, as shown below. As for public default contructor, I used @RequiredArgsConstructor annotation from Lombok. I can’t see why it should still give me the same error.
AuthenticationController.java
package com.example.demo.controllers;
@RestController
@RequestMapping("/api/v1/auth")
@RequiredArgsConstructor
public class AuthenticationController {
private final AuthenticationManager authenticationManager;
private final UserDao userDao;
private final JwtUtils jwtUtils;
@PostMapping("/authenticate")
public ResponseEntity<String> authenticate(
@RequestBody AuthenticationRequest request
) {
authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(request.getEmail(), request.getPassword())
);
...
}
}
SecurityConfig.java
package com.example.demo.config;
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig {
...
@Bean
public AuthenticationManager authenticationManager(
AuthenticationConfiguration config) throws Exception {
return config.getAuthenticationManager();
}
...
}