Added the beginnings of authentication.
This commit is contained in:
parent
13429e3159
commit
731f0ba596
|
@ -1,9 +1,5 @@
|
||||||
package nl.andrewlalis.teaching_assistant_assistant;
|
package nl.andrewlalis.teaching_assistant_assistant;
|
||||||
|
|
||||||
import nl.andrewlalis.teaching_assistant_assistant.model.repositories.CourseRepository;
|
|
||||||
import nl.andrewlalis.teaching_assistant_assistant.model.repositories.PersonRepository;
|
|
||||||
import nl.andrewlalis.teaching_assistant_assistant.model.repositories.TeamRepository;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.CommandLineRunner;
|
import org.springframework.boot.CommandLineRunner;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
@ -11,15 +7,6 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class TeachingAssistantAssistantApplication implements CommandLineRunner {
|
public class TeachingAssistantAssistantApplication implements CommandLineRunner {
|
||||||
|
|
||||||
@Autowired
|
|
||||||
CourseRepository courseRepository;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
TeamRepository teamRepository;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
PersonRepository personRepository;
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(TeachingAssistantAssistantApplication.class, args);
|
SpringApplication.run(TeachingAssistantAssistantApplication.class, args);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
package nl.andrewlalis.teaching_assistant_assistant.model.repositories;
|
||||||
|
|
||||||
|
import nl.andrewlalis.teaching_assistant_assistant.model.security.User;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public interface UserRepository extends JpaRepository<User, Long> {
|
||||||
|
Optional<User> findByUsername(String username);
|
||||||
|
}
|
|
@ -0,0 +1,42 @@
|
||||||
|
package nl.andrewlalis.teaching_assistant_assistant.model.security;
|
||||||
|
|
||||||
|
import nl.andrewlalis.teaching_assistant_assistant.model.BasicEntity;
|
||||||
|
import nl.andrewlalis.teaching_assistant_assistant.model.people.Person;
|
||||||
|
|
||||||
|
import javax.persistence.Column;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.JoinColumn;
|
||||||
|
import javax.persistence.OneToOne;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a user of the website with some credentials.
|
||||||
|
*/
|
||||||
|
@Entity
|
||||||
|
public class User extends BasicEntity {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A unique username for the user.
|
||||||
|
*/
|
||||||
|
@Column(nullable = false, unique = true)
|
||||||
|
private String username;
|
||||||
|
|
||||||
|
@Column
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
@OneToOne
|
||||||
|
@JoinColumn(name = "person_id", nullable = true, referencedColumnName = "id")
|
||||||
|
private Person person;
|
||||||
|
|
||||||
|
public String getUsername() {
|
||||||
|
return this.username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPassword() {
|
||||||
|
return this.password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Person getPerson() {
|
||||||
|
return this.person;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,49 @@
|
||||||
|
package nl.andrewlalis.teaching_assistant_assistant.model.security;
|
||||||
|
|
||||||
|
import org.springframework.security.core.GrantedAuthority;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
public class UserDetails implements org.springframework.security.core.userdetails.UserDetails {
|
||||||
|
|
||||||
|
private User user;
|
||||||
|
|
||||||
|
public UserDetails(User user) {
|
||||||
|
this.user = user;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<? extends GrantedAuthority> getAuthorities() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPassword() {
|
||||||
|
return this.user.getPassword();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getUsername() {
|
||||||
|
return this.user.getUsername();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isAccountNonExpired() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isAccountNonLocked() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isCredentialsNonExpired() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isEnabled() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package nl.andrewlalis.teaching_assistant_assistant.model.security;
|
||||||
|
|
||||||
|
import nl.andrewlalis.teaching_assistant_assistant.model.repositories.UserRepository;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public class UserDetailsService implements org.springframework.security.core.userdetails.UserDetailsService {
|
||||||
|
|
||||||
|
private UserRepository userRepository;
|
||||||
|
|
||||||
|
protected UserDetailsService(UserRepository userRepository) {
|
||||||
|
this.userRepository = userRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||||
|
Optional<User> optionalUser = this.userRepository.findByUsername(username);
|
||||||
|
optionalUser.orElseThrow(() -> new UsernameNotFoundException("Username not found."));
|
||||||
|
|
||||||
|
return new nl.andrewlalis.teaching_assistant_assistant.model.security.UserDetails(optionalUser.get());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue