Added the beginnings of authentication.

This commit is contained in:
Andrew Lalis 2019-05-11 10:56:37 +02:00 committed by andrewlalis
parent 13429e3159
commit 731f0ba596
5 changed files with 125 additions and 13 deletions

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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());
}
}