From 731f0ba596fc39e7a16d87eefedcece2f55c2bf9 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Sat, 11 May 2019 10:56:37 +0200 Subject: [PATCH] Added the beginnings of authentication. --- ...TeachingAssistantAssistantApplication.java | 13 ----- .../model/repositories/UserRepository.java | 10 ++++ .../model/security/User.java | 42 ++++++++++++++++ .../model/security/UserDetails.java | 49 +++++++++++++++++++ .../model/security/UserDetailsService.java | 24 +++++++++ 5 files changed, 125 insertions(+), 13 deletions(-) create mode 100644 src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/repositories/UserRepository.java create mode 100644 src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/security/User.java create mode 100644 src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/security/UserDetails.java create mode 100644 src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/security/UserDetailsService.java diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/TeachingAssistantAssistantApplication.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/TeachingAssistantAssistantApplication.java index c5adf7d..5b31ab9 100644 --- a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/TeachingAssistantAssistantApplication.java +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/TeachingAssistantAssistantApplication.java @@ -1,9 +1,5 @@ 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.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -11,15 +7,6 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class TeachingAssistantAssistantApplication implements CommandLineRunner { - @Autowired - CourseRepository courseRepository; - - @Autowired - TeamRepository teamRepository; - - @Autowired - PersonRepository personRepository; - public static void main(String[] args) { SpringApplication.run(TeachingAssistantAssistantApplication.class, args); } diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/repositories/UserRepository.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/repositories/UserRepository.java new file mode 100644 index 0000000..aa8e696 --- /dev/null +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/repositories/UserRepository.java @@ -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 { + Optional findByUsername(String username); +} diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/security/User.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/security/User.java new file mode 100644 index 0000000..49083cd --- /dev/null +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/security/User.java @@ -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; + } + +} diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/security/UserDetails.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/security/UserDetails.java new file mode 100644 index 0000000..f4730ee --- /dev/null +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/security/UserDetails.java @@ -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 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; + } +} diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/security/UserDetailsService.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/security/UserDetailsService.java new file mode 100644 index 0000000..6658a33 --- /dev/null +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/security/UserDetailsService.java @@ -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 optionalUser = this.userRepository.findByUsername(username); + optionalUser.orElseThrow(() -> new UsernameNotFoundException("Username not found.")); + + return new nl.andrewlalis.teaching_assistant_assistant.model.security.UserDetails(optionalUser.get()); + } +}