#login AppdetailsService doesn't autowire
25 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @hasty flame! Please use
/closeor theClose Postbutton 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.
where are you defining this bean type?
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
can you explain what you mean with the return statement?
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.
That's what I'm asking to see by the question:
where are you defining this bean type?
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?
It seems fine by me, that shouldn't be the problem.
cuz it's a mess haha
heyy! srry i took a break because it was a little too much