Issue
I want to make login form with data from my own database. I made Entity User class:
@Entity
@Table(name = "Auth_data")
public class User {
@Id
@GeneratedValue
@Getter @Setter
private Long id;
@Column(name= "username")
@Getter @Setter
private String username;
@Column(name="password")
@Getter @Setter
private String password;
@Enumerated(EnumType.ORDINAL)
@Column(name="role")
@Getter @Setter
private Role role;
@Column(name = "client_ID")
@Getter @Setter
private Long clientID;
@Column(name = "instructor_ID")
@Getter @Setter
private Long instructorID;
public User() {}
public User(String username, String password, Role role, Long clientID, Long instructorID) {
this.username = username;
this.password = password;
this.role = role;
this.clientID = clientID;
this.instructorID = instructorID;
}
}
Also I made UserDetailsServiceIml class which implements UserDetailsService:
@Service
public class UserDetailsServiceIml implements UserDetailsService {
@Autowired
private UserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
final User user = userRepository.findByUsername(username);
if(user == null) {
throw new UsernameNotFoundException(username);
}
Set<GrantedAuthority> grantedAuthorities = new HashSet< >();
grantedAuthorities.add(new SimpleGrantedAuthority(user.getRole().name()));
return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(),
grantedAuthorities);
}
}
My User repository class:
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
User findByUsername(String username);
}
I also tried make Query like this:
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
@Query("Select u from User u WHERE u.username=:username")
User findByUsername(@Param("username") String username);
}
But when I enter login and password into login form I get error, because User is Null after searching in database. How can I fix it?
UPD: SOLVED! I don't know why and how. I found that hibernate created new table "auth_data", but not used my table "Auth_data". So, I renamed "Auth_data" to "auth_data" and now it's work! I also changed @Getter @Setter on @Data,changed in User repository on " User findByUsername(String username);" and changed type of field role from enum to String.
Solution
It is possible that you don't seed the database with the user you are trying to login. You can use an Application Runner class to seed it on every start if the user doesn't exist in the database. Also, you can apply the @Data
annotation from Lombok on class level to generate getters and setters for all the fields.
package com.javahowtos.dataseeddemo.dataseed;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import com.javahowtos.dataseeddemo.model.User;
import com.javahowtos.dataseeddemo.repository.UserRepository;
@Component
public class UserDataLoader implements CommandLineRunner {
@Autowired
UserRepository userRepository;
@Override
public void run(String... args) throws Exception {
loadUserData();
}
private void loadUserData() {
if (userRepository.count() == 0) {
User user1 = new User("John", "Doe");
User user2 = new User("John", "Cook");
userRepository.save(user1);
userRepository.save(user2);
}
System.out.println(userRepository.count());
}
}
Source: https://javahowtos.com/guides/107-spring/376-how-to-seed-the-database-in-spring-boot-project.html
Answered By - Aethernite
Answer Checked By - Clifford M. (JavaFixing Volunteer)