Reputation: 47
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 'readerRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'readerRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class java.io.Reader
package com.example.readinglist;
import java.io.Reader;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Repository;
@Repository
public interface ReaderRepository extends JpaRepository<Reader, String> {
UserDetails findOne(String username);
}
ReadingListApplication:
package com.example.readinglist;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ReadinglistApplication {
public static void main(String[] args) {
SpringApplication.run(ReadinglistApplication.class, args);
}
}
SecurityConfiguration file:
package com.example.readinglist;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{
@Autowired
private ReaderRepository readerRepository;
@Override
protected void configure(HttpSecurity http) throws Exception{
http.authorizeRequests()
.antMatchers("/")
.access("hasRole('Reader')")
.antMatchers("/**").permitAll().and().formLogin().loginPage("/login")
.failureUrl("/login?error=true");
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(new UserDetailsService(){
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
return readerRepository.findOne(username);
}
});
}
}
Upvotes: 0
Views: 524
Reputation: 19
You have imported the wrong Reader entity --> You imported "import java.io.Reader;" You need to import your custom Reader entity class.
Upvotes: 1
Reputation: 69450
Your repository declaration s wrong. It must be:
public interface ReaderRepository extends JpaRepository<<Name of your entity>, String> {
if your entity name is Reader
you have imported the wrong class
Upvotes: 0
Reputation: 55
I think readerRepository.findOne(username)
does not return a UserDetails Object. If this is true, you have to create a new UserDetails object based on the informations in the Reader Object and return it
Upvotes: 0