Added Role object and started using bootstrap for login page

This commit is contained in:
Andrew Lalis 2019-05-14 09:14:18 +02:00 committed by andrewlalis
parent b1b5f11f18
commit 89c74f4925
6 changed files with 155 additions and 45 deletions

View File

@ -22,7 +22,8 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(this.userDetailsService);
auth.userDetailsService(this.userDetailsService)
.passwordEncoder(passwordEncoder());
}
@Override
@ -41,8 +42,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
.and()
.authorizeRequests() // Only logged in users should be able to see site content.
.antMatchers("/**")
.hasRole("USER")
.antMatchers("/**").authenticated()
.anyRequest().authenticated()
.and()

View File

@ -0,0 +1,43 @@
package nl.andrewlalis.teaching_assistant_assistant.model.security;
import nl.andrewlalis.teaching_assistant_assistant.model.BasicEntity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import java.util.List;
/**
* Represents a role that a user has, which gives the user access to certain resources.
*/
@Entity
public class Role extends BasicEntity {
/**
* The name of this role.
*/
@Column(nullable = false, unique = true)
private String name;
/**
* The list of users with this role.
*/
@ManyToMany(mappedBy = "roles")
private List<User> users;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
}

View File

@ -3,10 +3,8 @@ 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;
import javax.persistence.*;
import java.util.List;
/**
* Represents a user of the website with some credentials.
@ -23,9 +21,31 @@ public class User extends BasicEntity {
/**
* The password for this user.
*/
@Column
@Column(nullable = false)
private String password;
/**
* Whether or not this user has been activated.
*/
@Column(nullable = false)
private boolean activated = false;
/**
* Whether or not this user has been locked (no more access).
*/
@Column(nullable = false)
private boolean locked = false;
@ManyToMany(
fetch = FetchType.EAGER
)
@JoinTable(
name = "user_roles",
joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id")
)
private List<Role> roles;
@OneToOne
@JoinColumn(
name = "person_id",
@ -46,4 +66,27 @@ public class User extends BasicEntity {
return this.person;
}
public boolean isActivated() {
return activated;
}
public void setActivated(boolean activated) {
this.activated = activated;
}
public boolean isLocked() {
return locked;
}
public void setLocked(boolean locked) {
this.locked = locked;
}
public List<Role> getRoles() {
return roles;
}
public void setRoles(List<Role> roles) {
this.roles = roles;
}
}

View File

@ -3,9 +3,13 @@ package nl.andrewlalis.teaching_assistant_assistant.model.security;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
* TAA-specific implementation of Spring's UserDetails interface to supply user authentication data.
*/
public class UserDetails implements org.springframework.security.core.userdetails.UserDetails {
private User user;
@ -20,7 +24,12 @@ public class UserDetails implements org.springframework.security.core.userdetail
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return Collections.singleton(new SimpleGrantedAuthority("ROLE_USER"));
List<Role> roles = this.user.getRoles();
List<GrantedAuthority> authorities = new ArrayList<>();
for (Role role : roles) {
authorities.add(new SimpleGrantedAuthority(role.getName()));
}
return authorities;
}
@Override
@ -40,7 +49,7 @@ public class UserDetails implements org.springframework.security.core.userdetail
@Override
public boolean isAccountNonLocked() {
return true;
return !this.user.isLocked();
}
@Override
@ -50,6 +59,6 @@ public class UserDetails implements org.springframework.security.core.userdetail
@Override
public boolean isEnabled() {
return true;
return this.user.isActivated();
}
}

View File

@ -10,8 +10,7 @@
<hr>
<p>
Welcome to the Teaching Assistant Assistant, <span th:text="${user.getPerson().getFullName()}"></span>. To find the courses in this application please follow the link to
<a th:href="@{/courses}">courses</a>.
Welcome to the Teaching Assistant Assistant, <span th:text="${user.getPerson().getFullName()}"></span>. To find the courses in this application please follow the link to <a th:href="@{/courses}">courses</a>.
</p>
<p>

View File

@ -3,61 +3,77 @@
<head>
<meta charset="UTF-8">
<title>Login</title>
<link rel="stylesheet" th:href="@{/css/style.css}" type="text/css"/>
<link rel="stylesheet" th:href="@{/css/login.css}" type="text/css"/>
<link rel="stylesheet" th:href="@{/css/bootstrap.min.css}" type="text/css"/>
</head>
<body>
<div class="content_container">
<div class="page_row">
<section class="container">
<div class="row justify-content-center">
<h1>Teaching Assistant <em>Assistant</em></h1>
</div>
<div class="page_row">
<div th:if="${param.error}">
<div class="row justify-content-center">
<div class="alert alert-danger" th:if="${param.error}">
Invalid username or password!
</div>
<div th:if="${param.logout}">
<div class="alert alert-success" th:if="${param.logout}">
You have been logged out!
</div>
</div>
<div class="page_row">
<h2>Login</h2>
<div class="row justify-content-center">
<p>
Please log in to access this application.
Please log in to access to the world's most advanced course organization tool ever created. With the ability to manipulate large groups of students with ease, you might actually enjoy teaching a course.
</p>
</div>
<div class="page_row">
<form class="login_form" th:action="@{/login}" method="post">
<div class="login_form_row">
<div class="row justify-content-center">
<form th:action="@{/login}" method="post">
<div class="form-group">
<label for="username_input">Username</label>
<input id="username_input" class="form-control" type="text" name="username"/>
</div>
<div class="login_form_row">
<input id="username_input" type="text" name="username"/>
</div>
<div class="login_form_row">
<div class="form-group">
<label for="password_input">Password</label>
<input id="password_input" class="form-control" type="password" name="password"/>
</div>
<div class="login_form_row">
<input id="password_input" type="password" name="password"/>
</div>
<div class="login_form_row">
<input type="submit" value="Login"/>
</div>
<div class="login_form_row">
<a class="white_link" th:href="@{/register}">Don't have an account? Sign up here!</a>
</div>
</form>
</div>
</section>
<section class="fixed-bottom">
<div class="container-fluid">
<div class="row text-center">
<div class="col-xs-12 col-4">
<h5>Quick Links</h5>
</div>
<div class="col-xs-12 col-4">
<h5>More links</h5>
</div>
<div class="col-xs-12 col-4">
<h5>Yet more links</h5>
</div>
</div>
<div class="row">
<div class="col-12 text-center">
Social media icons go here.
</div>
</div>
<div class="row">
<div class="col-12 text-center">
<p class="h6">
&copy; 2019 Andrew Lalis, all rights reserved.
</p>
</div>
</div>
</div>
</section>
</body>
</html>