0
votes

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);
            }
        });
    }
}
3
Isn't the message Not a managed type: class java.io.Reader clear? java.io.Reader is not an entityJens

3 Answers

1
votes

You have imported the wrong Reader entity --> You imported "import java.io.Reader;" You need to import your custom Reader entity class.

0
votes

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

0
votes

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