Error: ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityConfig': Unsatisfied dependency expressed through field 'profileNotFoundException'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.Portfolio.API.Exception.ProfileNotFoundException' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Description:
Field profileNotFoundException in com.Portfolio.API.Config.SecurityConfig required a bean of type 'com.Portfolio.API.Exception.ProfileNotFoundException' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.Portfolio.API.Exception.ProfileNotFoundException' in your configuration.
My Config:
package com.Portfolio.API.Config;
import com.Portfolio.API.Exception.ProfileNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Autowired
private ProfileNotFoundException profileNotFoundException;
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/").permitAll() // Esto permite que cualquiera entre a cualquier parte de tu página web
/*
// Con esto puedes controlar quien entra a qué partes de tu aplicación/página dependiendo del rol que tengan
.antMatchers("/home").hasRole("USER")
.antMatchers("/education").hasRole("ADMIN")
.antMatchers("/experience").hasRole("USER")
.antMatchers("/projects").hasRole("USER")
.antMatchers("/skills").hasRole("USER")
*/
.anyRequest().authenticated()
.and()
.formLogin() // Se crea una página de login para identificarse
.and()
.httpBasic();
return http.build();
}
// Aquí se crean los usuarios con sus passwords y roles y se guardan en la memoria del programa,
// al terminar la ejecución, se borran
@Bean
public UserDetailsService userDetailsService(){
UserDetails user = User
.withUsername("user")
.password(passwordEncoder().encode("123456"))
.roles("USER")
.build();
UserDetails admin = User
.withUsername("admin")
.password(passwordEncoder().encode("password"))
.roles("USER", "ADMIN")
.build();
return new InMemoryUserDetailsManager(user, admin);
}
// Con este método se encriptan los passwords
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
}
My github repo: https://github.com/cynthiasaucedo/PortfolioAP/tree/master/Backend
CodePudding user response:
you have this error Field profileNotFoundException in com.Portfolio.API.Config.SecurityConfig required a bean of type 'com.Portfolio.API.Exception.ProfileNotFoundException' that could not be found.
so try to remove field with ProfileNotFoundException name;
CodePudding user response:
It seems the spring boot is unable to inject the profileNotFoundException bean you should check that. Also I can see that you haven't used it in this class so you can either check and build your application again by removing this
@Autowired
private ProfileNotFoundException profileNotFoundException;
As you the bean is not defined for this name and it seems to be a custom exception which you can throw directly when necessary. Check if it works.