hej jestem początkująca i mam problem z danym błędem :
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userDetailsService': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class models.User
klasy dla podgladu :
package config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
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.access.AccessDeniedHandler;
import org.springframework.security.web.access.AccessDeniedHandlerImpl;
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
@Profile(ProfileNames.INMEMORY)
public UserDetailsService userDetailsService() {
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
User.UserBuilder userBuilder = User.withDefaultPasswordEncoder();
UserDetails user = userBuilder
.username("user")
.password("user")
.roles("USER")
.build();
UserDetails admin = userBuilder
.username("admin")
.password("admin")
.roles("ADMIN")
.build();
UserDetails test = userBuilder
.username("test")
.password("test")
.roles("USER", "ADMIN")
.build();
manager.createUser(user);
manager.createUser(admin);
manager.createUser(test);
return manager;
}
@Bean
@Profile(ProfileNames.DATABASE)
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
public AccessDeniedHandler createAccessDeniedHandler(){
AccessDeniedHandlerImpl impl = new AccessDeniedHandlerImpl();
impl.setErrorPage("/error403");//url
return impl;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/statics/**", "/webjars/**", "/", "/vehicleList.html", "/registerForm.html", "/registerSuccess.html").permitAll()
.antMatchers( "/vehicle.html").hasRole("USER")
.antMatchers( "/vehicle**").hasRole("ADMIN")
.anyRequest().authenticated(); //każde żądanie ma być uwierzytelnione
http
.formLogin()//pozwól użytkownikom uwierzytelniać się poprzez formularz
.loginPage("/login")//formularz logowania dostępny jest pod URL
.permitAll();//przyznaj dostęp wszystkim użytkownikom dla wszystkich URL powiązanych z logowaniem opartym na formularzu.
http
.logout() //pozwól wszystkim użykownikom się wylogować
.permitAll();//
http.exceptionHandling().accessDeniedHandler(createAccessDeniedHandler());
}
}
kolejna :
package repositories;
import models.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
User findByUsername(String username);
}