My spring project throwing error as follows
APPLICATION FAILED TO START
Description:
Field userDetailsService in required a bean of type 'org.springframework.security.core.userdetails.UserDetailsService' that could not be found.
The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'org.springframework.security.core.userdetails.UserDetailsService' in your configuration.
This is my controller
package org.studyeasy.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.studyeasy.entity.Post;
import org.studyeasy.services.PostsService;
@RestController
public class PostsController {
@Autowired
private PostsService service;
@RequestMapping("/posts")
public List<Post> getPosts(){
return service.getPosts();
}
@RequestMapping("/posts/{id}")
public Post getPost(@PathVariable int id) {
return service.getPost(id);
}
@RequestMapping(method=RequestMethod.POST, value="/posts")
public void addPost(@RequestBody Post listElement) {
service.addPost(listElement);
}
@RequestMapping(method=RequestMethod.PUT, value="/posts/{id}")
public void updatePost(@RequestBody Post post) {
service.updatePost(post);
}
@RequestMapping(method=RequestMethod.DELETE, value="/posts/{id}")
public void deletePost(@PathVariable int id) {
service.deletePost(id);
}
}
This is my entity class
package org.studyeasy.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="posts")
public class Post {
@Id
@Column(name="id")
int postId;
@Column(name="title")
String title;
@Column(name="body")
String body;
public Post() {}
public Post(int postId, String title, String body) {
this.postId = postId;
this.title = title;
this.body = body;
}
public int getPostId() {
return postId;
}
public void setPostId(int postId) {
this.postId = postId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
@Override
public String toString() {
return "Post [postId=" postId ", title=" title ", body=" body "]";
}
}
This is repository class
package org.studyeasy.repository;
import org.springframework.data.repository.CrudRepository;
import org.studyeasy.entity.Post;
public interface PostRepository extends CrudRepository<Post, Integer> {
}
I'm new to spring boot so please help I'm not able to find where I'm making mistake
Thanks...
CodePudding user response:
I also faced same issue because i forgot to annotate my classes. Check weather you have annotated your service class as @Service and Repository as @Repository.
If this doesn't solve the issue than remove the unwanted dependencies from Pom file.
Hope this solves the issue.
CodePudding user response:
Add a bean for UserDetailsService
@Autowired
private UserDetailsService userDetailsService;
@Bean
public UserDetailsService userDetailsService() {
return super.userDetailsService();
}
CodePudding user response:
You are not making the bean correctly. Use @Service annotation in your service implementation class.
CodePudding user response:
I think you have made some mistakes in annotation
There may be following reason
You may have missed any annotation related to @Component class like @Controller or @Service ect.
You may have done mistake in @Autowired annotation
May be you made some changes in main class So you need to check all these to solve this issue.
CodePudding user response:
As the error states, you need to create the UserDetailsService
bean and configure it.
CodePudding user response:
Your error is not related to your snippet. Looks like you added a spring security depedency that you don't seem to use.
Either remove this depedency or if you need to retrive a user principal from Spring security : add a configuration that implements UserDetailsService (a Way to retrive a User from an authentication method) as asked.
https://www.boraji.com/spring-security-5-custom-userdetailsservice-example
CodePudding user response:
I think you are using any kind of authentication. and you didn't implement a userDetailsService interface.
@Service
public class UserPrincipalService implements UserDetailsService {
private final UserRepository userRepository;
@Autowired
public UserPrincipalService(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
Optional<User> user = userRepository.findUserByUserName(username);
user.orElseThrow(() -> new UsernameNotFoundException("This user name is not found"));
return new UserPrincipal(user.get());
}
}
implement this class and load the user data for your data source.
CodePudding user response:
@Autowired
private UserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String username)throws UsernameNotFoundException {
User user = userRepository.getUserByUsername(username);
if (user == null) {
throw new UsernameNotFoundException("Could not find user");
}
return new MyUserDetails(user);
}
you can use constructor injection while define bean.
private User user;
public MyUserDetails(User user) {
this.user = user;
}
and you can get by this in userDetails.