#login AppdetailsService doesn't autowire

25 messages · Page 1 of 1 (latest)

hasty flame
#

I'm currently working on a Spring Boot projecet for Thymeleaf and want to make a login, problem is i can't seem to fix this issue. Any help is welcome.

torpid cryptBOT
#

This post has been reserved for your question.

Hey @hasty flame! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

jovial wave
#

where are you defining this bean type?

hasty flame
#

under the code in the picture

#

w8

#

here

jovial wave
#

if you're referring to line 36, you haven't done it correcly

#

you ought to annotate the method with @Bean and return the object from the method

hasty flame
jovial wave
#

So you know how you can return objects from a method, right? Well, in this case, you need to return an object of the type that you wish to configure from the method annotated with @Bean.

#

Right now you're not returning anything - the method has a return type "void", when it's supposed to return type AppUserDetailService.

hasty flame
#

i did that in another class

#

if that's what you mean

#

i got this

jovial wave
#

That's what I'm asking to see by the question:

where are you defining this bean type?

hasty flame
#

import be.thomasmore.qrace.model.AppUser;
import be.thomasmore.qrace.repository.AppUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class AppUserDetailService implements UserDetailsService {

    private final AppUserRepository userRepository;

    public AppUserDetailService(AppUserRepository userRepository) {
        this.userRepository = userRepository;
    }


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // Retrieve the user from your data source based on the username
        // For example:
        AppUser user = userRepository.findByUsername(username);

        if (user == null) {
            throw new UsernameNotFoundException("User not found with username: " + username);
        }

        // Create a list of GrantedAuthority based on your user's roles/permissions
        List<GrantedAuthority> authorities = user.getRoles().stream()
                .map(role -> new SimpleGrantedAuthority(role.toString()))
                .collect(Collectors.toList());

        // Return an instance of AppUser which implements UserDetails
        return new org.springframework.security.core.userdetails.User(
                user.getUsername(),
                user.getPassword(),
                authorities
        );
    }
}```
#

can i send you the file for context?

jovial wave
#

It seems fine by me, that shouldn't be the problem.

hasty flame
#

cuz it's a mess haha

light mist
#

How are you doing your configuration?

#

With a config class?

#

@hasty flame

hasty flame