Ars
Ars

Reputation: 639

Error creating bean with name 'webSecurityConfig' because UserService Bean doesn't exist when I try to autowire it into WebSecurityConfig

All spring mvc + hibernate + spring security examples say that I need to create MyUserService class which extends UserDetailsService from springSecurity to bind my database with spring security. But when I do so I get:

Error creating bean with name 'webSecurityConfig' no such bean UserSerivce is defined

because it doesn't exist yet when I try to autowire it into SecurityConfig. When I download any online example, it gives me the same error.

My project in Git - start_over branch.

My WebSecurityConfig class:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {


@Autowired
private UserService userService;


@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(userService).passwordEncoder(passwordEncoder());
}


@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests().antMatchers("/login", "/").anonymous()
            .antMatchers("/admin", "/admin**").hasRole("ADMIN")
            .anyRequest().authenticated()
            .and()
            .formLogin().loginPage("/login").permitAll()
            .and()
            .logout().logoutSuccessUrl("/login").permitAll()
            .and()
            .csrf().disable();
}

@Bean
public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
}

}

my UserService:

public interface UserService extends UserDetailsService {

    void save(User user);

    List<User> getAllUsers();
}

my UserServiceImpl

@Service
public class UserServiceImp implements UserService{

    @Autowired
    private UserDao userDao;

    @Transactional
    public void save(User user) {
        userDao.save(user);
    }

    @Transactional(readOnly = true)
    public List<User> getAllUsers() {
        return userDao.getAllUsers();
    }

    @Transactional(readOnly=true)
    @Override
    public UserDetails loadUserByUsername(final String username)
            throws UsernameNotFoundException {

        com.rjproject.entities.User user = userDao.findByUserName(username);
        List<GrantedAuthority> authorities =
                buildUserAuthority(user.getAuthorities());

        return buildUserForAuthentication(user, authorities);

    }

    private org.springframework.security.core.userdetails.User buildUserForAuthentication(com.rjproject.entities.User user,
                                                                                          List<GrantedAuthority> authorities) {
        return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(),
                user.isEnabled(), true, true, true, authorities);
    }

    private List<GrantedAuthority> buildUserAuthority(Set<Authorities> userRoles) {

        Set<GrantedAuthority> setAuths = new HashSet<GrantedAuthority>();

        // Build user's authorities
        for (Authorities userRole : userRoles) {
            setAuths.add(new SimpleGrantedAuthority(userRole.getAuthority()));
        }

        List<GrantedAuthority> Result = new ArrayList<GrantedAuthority>(setAuths);

        return Result;
    }
}

UPD:

I have RunJob Class that creates users on startup and UserService autowires there without errors. Here it is:

@Service
public class RunJob implements InitializingBean {

    private static final Logger logger = Logger.getLogger(RunJob.class);
    @Autowired
    private UserService userService;

    public void afterPropertiesSet() {

        User userA = new User();
        userA.setUsername("ARS");
        userA.setPassword("ART");
        Authorities authorityA = new Authorities();
        authorityA.setAuthority("ADMIN");
        authorityA.setUser(userA);
        Set<Authorities> roles = new HashSet<>();
        roles.add(authorityA);
        userA.setAuthorities(roles);
        userService.save(userA);
        logger.info("user " + userA.getUsername() + " " + userA.getPassword() + " is saved with " + authorityA.getAuthority() + " authority");

        User userB = new User();
        userB.setUsername("John");
        userB.setPassword("Doe");
        Authorities authorityB = new Authorities();
        authorityB.setAuthority("USER");
        authorityB.setUser(userB);
        roles.clear();
        roles.add(authorityB);
        userB.setAuthorities(roles);
        userService.save(userB);
        logger.info("user " + userB.getUsername() + " " + userB.getPassword() + " is saved with " + authorityB.getAuthority() + " authority");
    }
}

I get exact same error when I download any online example. Something with SpringSecurity itself. UPD2: here is all config classes.

webMvcConfig

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.rjproject"})
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private ApplicationContext applicationContext;

    @Bean
    public SpringResourceTemplateResolver templateResolver() {
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setApplicationContext(applicationContext);
        templateResolver.setPrefix("/WEB-INF/templates/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode(TemplateMode.HTML);
        templateResolver.setCacheable(true);
        return templateResolver;
    }

    @Bean
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver());
        templateEngine.setEnableSpringELCompiler(true);
        return templateEngine;
    }

    @Bean
    public ViewResolver viewResolver() {
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(templateEngine());
        return viewResolver;
    }

    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }
}

WebAppInit

public class WebAppInit extends AbstractAnnotationConfigDispatcherServletInitializer {


    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { HibernateConfig.class, WebSecurityConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] { WebMvcConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}

Upvotes: 2

Views: 7349

Answers (2)

Ars
Ars

Reputation: 639

If your project have multiple config classes like mine have WebMvcConfig and WebSecurityConfig you should define annotation @ComponentScan(basePackages = {"com.rjproject"}) for every config class.

Upvotes: 3

vahidsaneei1983
vahidsaneei1983

Reputation: 1

1 : In your case (javaconfig) you have to define a Bean that return userService object, where did it defined?!

2 : Other way, I think if you add setter method for userService in WebSecurityConfig class then problem get solved! just like this

public void setUserService(UserService userService){
     this.userService = userService;
}

and next step here, in implementing class from UserService interface you have to add @Service annotation above of class and set 'value' attribute of @Service to "userService".

Upvotes: 0

Related Questions