All my project was developed using this guide: text
I am using Sprin Boot: 3.3.2 and MySQL
This is the error i get:
ERROR 292047 — [ main] o.s.b.web.embedded.tomcat.Tomcat Starter : Error starting Tomcat context. Exception: org.springframework.beans.factory.Unsatisfie dDependencyException. Message: Error creating bean with name ‘securityConfig’ defined in URL [jar :nested:/home/ec2-user/easyrouteprod.jar/!BOOT-INF/classes/!/com/matancita/easyrouteprod/Security Config.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating b ean with name ‘userDetailServiceImpl’: Unsatisfied dependency expressed through field ‘userReposi tory’: Error creating bean with name ‘userRepository’ defined in com.matancita.easyrouteprod.dao. UserRepository defined in @EnableJpaRepositories declared on EasyrouteprodApplication: Cannot res olve reference to bean ‘jpaSharedEM_entityManagerFactory’ while setting bean property ‘entityMana ger’
This is my UserRepository interface:
@RepositoryRestResource
public interface UserRepository extends CrudRepository<User, Long> {
Optional<User> findByUsername(String username);
}
This is my SecurityConfig class:
@Configuration
@EnableWebSecurity
public class SecurityConfig{
private final UserDetailServiceImpl userDetailsService;
public SecurityConfig(UserDetailServiceImpl userDetailsService){
this.userDetailsService = userDetailsService;
}
@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
return authenticationConfiguration.getAuthenticationManager();
}
@Bean
PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
final AuthenticationManager authenticationManager = authenticationManager(http.getSharedObject(AuthenticationConfiguration.class));
http
.authorizeHttpRequests(authorize ->
authorize.requestMatchers(
"/login",
"/saveUser",
"/editUser/**").permitAll().anyRequest().authenticated())
.sessionManagement(securityContext -> securityContext.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.addFilterBefore(new LoginFilter("/login", authenticationManager), UsernamePasswordAuthenticationFilter.class)
.addFilterBefore(new AuthenticationFilter(), UsernamePasswordAuthenticationFilter.class)
.csrf(csrf -> csrf.disable())
.headers(header -> header.frameOptions(HeadersConfigurer.FrameOptionsConfig::disable))
.httpBasic(Customizer.withDefaults());
return http.build();
}
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
// Specify allowed origins explicitly
config.addAllowedOrigin("http://example.com");
config.addAllowedOrigin("http://localhost:3000");
config.addAllowedOrigin("http://192.168.1.3:3000");// You can specify specific origins here
config.addAllowedMethod("*"); // You can specify specific HTTP methods here
config.addAllowedHeader("*"); // You can specify specific headers here
config.setAllowCredentials(true);
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
}