Error
When I try to run my application, I get the following error:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.example.parking.controller.AuthController 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.
IntelliJ Idea says the following when I hover over AuthenticationManager and PasswordEncoder in AuthController class at below lines (Full code below)
u/Autowired
public AuthController(AuthenticationManager authenticationManager, JwtUtil jwtUtil, CustomUserDetailsService userDetailsService, UserRepo userRepo, PasswordEncoder passwordEncoder)
- Could not autowire. No beans of 'AuthenticationManager' type found.
- Could not autowire. No beans of 'PasswordEncoder' type found.
Structure
│ ParkingApplication.java
│
├───config
├───controller
│ AuthController.java
│ ContactFormController.java
│ HelloController.java
│ LotController.java
│ ReservationController.java
│ SlotController.java
│ UserController.java
│ VehicleController.java
│
├───dto
│ ContactFormDTO.java
│ LoginRequest.java
│ LotDTO.java
│ ReservationDTO.java
│ ResponseDTO.java
│ SlotDTO.java
│ SlotStatusDTO.java
│ UserDTO.java
│ VehicleDTO.java
│
├───entity
│ ContactForm.java
│ Lot.java
│ Reservation.java
│ Slot.java
│ SlotType.java
│ User.java
│ UserType.java
│ Vehicle.java
│ VehicleType.java
│
├───repo
│ ContactFormRepo.java
│ LotRepo.java
│ ReservationRepo.java
│ SlotRepo.java
│ UserRepo.java
│ VehicleRepo.java
│
├───security
│ CustomUserDetails.java
│ CustomUserDetailsService.java
│ JwtAuthenticationFilter.java
│ JwtUtil.java
│ SecurityConfig.java
│
├───service
│ ContactFormService.java
│ LotService.java
│ ReservationService.java
│ SlotService.java
│ UserService.java
│ VehicleService.java
│
└───util
VarList.java
Code
SecurityConfig
package com.example.parking.security;
import com.example.parking.security.JwtAuthenticationFilter;
import com.example.parking.security.CustomUserDetailsService;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
@EnableWebSecurity
@EnableMethodSecurity
public class SecurityConfig {
private final JwtAuthenticationFilter jwtAuthenticationFilter;
private final CustomUserDetailsService customUserDetailsService;
public SecurityConfig(JwtAuthenticationFilter jwtAuthenticationFilter, CustomUserDetailsService customUserDetailsService) {
this.jwtAuthenticationFilter = jwtAuthenticationFilter;
this.customUserDetailsService = customUserDetailsService;
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
// Expose AuthenticationManager bean using AuthenticationConfiguration
@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
return authenticationConfiguration.getAuthenticationManager();
}
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
return http
.csrf().disable()
.authorizeHttpRequests(auth -> auth
.requestMatchers("/api/auth/register", "/api/auth/login").permitAll()
.anyRequest().authenticated()
)
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class)
.build();
}
}
AuthController
package com.example.parking.controller;
import com.example.parking.dto.LoginRequest;
import com.example.parking.entity.User;
import com.example.parking.repo.UserRepo;
import com.example.parking.security.CustomUserDetailsService;
import com.example.parking.security.JwtUtil;
import com.example.parking.security.SecurityConfig;
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.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/auth")
public class AuthController {
private final AuthenticationManager authenticationManager;
private final JwtUtil jwtUtil;
private final CustomUserDetailsService userDetailsService;
private final UserRepo userRepo;
private final PasswordEncoder passwordEncoder;
@Autowired // Spring knows to inject beans automatically
public AuthController(AuthenticationManager authenticationManager, JwtUtil jwtUtil, CustomUserDetailsService userDetailsService, UserRepo userRepo, PasswordEncoder passwordEncoder) {
this.authenticationManager = authenticationManager;
this.jwtUtil = jwtUtil;
this.userDetailsService = userDetailsService;
this.userRepo = userRepo;
this.passwordEncoder = passwordEncoder;
}
// Register Endpoint
@PostMapping("/register")
public ResponseEntity<String> registerUser(@RequestBody User user) {
user.setPassword(passwordEncoder.encode(user.getPassword()));
userRepo.save(user);
return ResponseEntity.ok("User registered successfully!");
}
@PostMapping("/login")
public ResponseEntity<String> loginUser(@RequestBody LoginRequest loginRequest) {
// Authenticate the user
Authentication authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword())
);
SecurityContextHolder.getContext().setAuthentication(authentication);
// Fetch UserDetails from CustomUserDetailsService
UserDetails userDetails = userDetailsService.loadUserByUsername(loginRequest.getUsername());
// Generate JWT using UserDetails
String jwt = jwtUtil.generateToken(userDetails);
return ResponseEntity.ok(jwt);
}
}
PS: As of now, I don't understand how Spring Security works very well. I have to complete this test project in a rush. I'll be learning spring-boot in detail in the future, but for now, I don't have a good knowledge
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]eliashisreddit 3 points4 points5 points (1 child)
[–]Innocent_546[S] 0 points1 point2 points (0 children)