Changeset 6489bb9


Ignore:
Timestamp:
01/05/23 01:40:08 (2 years ago)
Author:
Bojan <bojantrpeski123@…>
Branches:
main
Children:
e440971
Parents:
f11774e
Message:

Spring security added with register and login controllers

Location:
FullyStocked
Files:
4 added
7 edited

Legend:

Unmodified
Added
Removed
  • FullyStocked/pom.xml

    rf11774e r6489bb9  
    3434            <artifactId>postgresql</artifactId>
    3535        </dependency>
    36 
     36        <dependency>
     37            <groupId>org.springframework.boot</groupId>
     38            <artifactId>spring-boot-starter-security</artifactId>
     39        </dependency>
     40        <dependency>
     41            <groupId>org.thymeleaf.extras</groupId>
     42            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
     43        </dependency>
    3744        <dependency>
    3845            <groupId>org.projectlombok</groupId>
  • FullyStocked/src/main/java/com/bazi/fullystocked/Models/User.java

    rf11774e r6489bb9  
    55import lombok.Data;
    66import lombok.NoArgsConstructor;
     7import org.springframework.security.core.GrantedAuthority;
     8import org.springframework.security.core.userdetails.UserDetails;
    79
     10import java.util.Collection;
    811
    912
     
    1316@NoArgsConstructor
    1417@Table(name="users")
    15 public class User{
     18public class User implements UserDetails {
    1619    @Id
    1720    @GeneratedValue(strategy = GenerationType.IDENTITY)
     
    4649    }
    4750
     51    @Override
     52    public Collection<? extends GrantedAuthority> getAuthorities() {
     53        return null;
     54    }
     55
     56    @Override
     57    public String getPassword() {
     58        return userpassword;
     59    }
     60
     61    @Override
     62    public boolean isAccountNonExpired() {
     63        return true;
     64    }
     65
     66    @Override
     67    public boolean isAccountNonLocked() {
     68        return true;
     69    }
     70
     71    @Override
     72    public boolean isCredentialsNonExpired() {
     73        return true;
     74    }
     75
     76    @Override
     77    public boolean isEnabled() {
     78        return true;
     79    }
    4880}
  • FullyStocked/src/main/java/com/bazi/fullystocked/Repositories/WorkersRepository.java

    rf11774e r6489bb9  
    33import com.bazi.fullystocked.Models.Invoices;
    44import com.bazi.fullystocked.Models.Locations;
     5import com.bazi.fullystocked.Models.User;
    56import com.bazi.fullystocked.Models.Workers;
    67import org.springframework.data.jpa.repository.JpaRepository;
     
    1314    List<Workers> findAllByLocation(Locations location);
    1415    List<Workers> findAllByLocationIsNull();
     16
    1517}
  • FullyStocked/src/main/java/com/bazi/fullystocked/Services/AuthService.java

    rf11774e r6489bb9  
    55import com.bazi.fullystocked.Models.User;
    66import com.bazi.fullystocked.Models.Workers;
     7import org.springframework.security.core.userdetails.UserDetailsService;
    78
    8 public interface AuthService {
     9public interface AuthService extends UserDetailsService {
    910    User login(String username, String password);
    1011    Workers registerWorker(String firstname, String lastname, String username, String email, String password);
  • FullyStocked/src/main/java/com/bazi/fullystocked/Services/Implementations/AuthServiceImpl.java

    rf11774e r6489bb9  
    1414import com.bazi.fullystocked.Repositories.WorkersRepository;
    1515import com.bazi.fullystocked.Services.AuthService;
     16import org.springframework.security.core.userdetails.UserDetails;
    1617import org.springframework.security.crypto.password.PasswordEncoder;
    1718import org.springframework.stereotype.Service;
     
    2526    private final PasswordEncoder passwordEncoder;
    2627
    27     public AuthServiceImpl(UsersRepository usersRepository, WorkersRepository workersRepository, SuppliersRepository suppliersRepository, ManagersRepository managersRepository, PasswordEncoder passwordEncoder) {
     28    public AuthServiceImpl( UsersRepository usersRepository, WorkersRepository workersRepository, SuppliersRepository suppliersRepository, ManagersRepository managersRepository, PasswordEncoder passwordEncoder) {
    2829        this.usersRepository = usersRepository;
    2930        this.workersRepository = workersRepository;
     
    3536    @Override
    3637    public User login(String username, String password) {
    37         if (username==null || username.isEmpty() || password==null || password.isEmpty()) {
     38        if (username == null || username.isEmpty() || password == null || password.isEmpty()) {
    3839            throw new InvalidArgumentsException();
    3940        }
    40         User user=usersRepository.findByUsername(username).orElseThrow(()->new UserNotFoundException(username));
    41         if(passwordEncoder.matches(password, user.getUserpassword()))
    42         {
     41        User user = usersRepository.findByUsername(username).orElseThrow(() -> new UserNotFoundException(username));
     42        if (passwordEncoder.matches(password, user.getUserpassword())) {
    4343            return user;
    44         }
    45         else
    46         {
     44        } else {
    4745            throw new InvalidUserCredentialsException();
    4846        }
     
    6260
    6361    private void RegParamsCheck(String firstname, String lastname, String username, String email, String password) {
    64         if(firstname==null || firstname.isEmpty() || lastname==null || lastname.isEmpty() || username==null || username.isEmpty() || email==null || email.isEmpty() || password==null || password.isEmpty())
    65         {
     62        if (firstname == null || firstname.isEmpty() || lastname == null || lastname.isEmpty() || username == null || username.isEmpty() || email == null || email.isEmpty() || password == null || password.isEmpty()) {
    6663            throw new InvalidArgumentsException();
    6764        }
    68         if(usersRepository.findByUsername(username).isPresent())
    69         {
     65        if (usersRepository.findByUsername(username).isPresent()) {
    7066            throw new UsernameAlreadyExistsException(username);
    7167        }
     
    7470    @Override
    7571    public Suppliers registerSupplier(String firstname, String lastname, String username, String email, String password, String supplierInfo, String phone, String street, int streetNumber, String city) {
    76         if(firstname==null || firstname.isEmpty() || lastname==null || lastname.isEmpty() || username==null || username.isEmpty() || email==null || email.isEmpty() || password==null || password.isEmpty())
    77         {
     72        if (firstname == null || firstname.isEmpty() || lastname == null || lastname.isEmpty() || username == null || username.isEmpty() || email == null || email.isEmpty() || password == null || password.isEmpty()) {
    7873            throw new InvalidArgumentsException();
    7974        }
    80         if(supplierInfo==null || supplierInfo.isEmpty() || phone==null || phone.isEmpty() || street==null || street.isEmpty() || city==null || city.isEmpty())
    81         {
     75        if (supplierInfo == null || supplierInfo.isEmpty() || phone == null || phone.isEmpty() || street == null || street.isEmpty() || city == null || city.isEmpty()) {
    8276            throw new InvalidArgumentsException();
    8377        }
    84         if(usersRepository.findByUsername(username).isPresent())
    85         {
     78        if (usersRepository.findByUsername(username).isPresent()) {
    8679            throw new UsernameAlreadyExistsException(username);
    8780        }
    8881        return suppliersRepository.save(new Suppliers(firstname, lastname, username, email, passwordEncoder.encode(password), supplierInfo, phone, street, streetNumber, city));
    8982    }
     83
     84    @Override
     85    public UserDetails loadUserByUsername(String username) throws UserNotFoundException {
     86        return usersRepository.findByUsername(username).orElseThrow(() -> new UserNotFoundException(username));
     87
     88    }
    9089}
  • FullyStocked/src/main/resources/templates/login.html

    rf11774e r6489bb9  
    1 <!DOCTYPE html>
    2 <html lang="en">
    3 <head>
    4     <meta charset="UTF-8">
    5     <title>Title</title>
    6 </head>
    7 <body>
     1<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
     2<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
     3<meta charset="UTF-8">
     4<th:block xmlns="http://www.w3.org/1999/xhtml">
     5<section class="vh-100">
     6    <div class="container py-5 h-100">
     7        <div class="row d-flex align-items-center justify-content-center h-100">
     8            <div class="col-md-8 col-lg-7 col-xl-6">
     9                <img src="https://img.freepik.com/free-vector/warehouse-isometric-template_1284-15709.jpg?w=2000"
     10                     class="img-fluid" alt="Phone image">
     11            </div>
     12            <div class="col-md-7 col-lg-5 col-xl-5 offset-xl-1">
     13                <h2>Најави се</h2>
     14                <form method="POST" action="/login">
     15                    <div class="form-outline mb-4">
     16                        <input name="username" type="text" id="username" class="form-control form-control-lg" />
     17                        <label class="form-label" for="username">Username</label>
     18                    </div>
    819
    9 </body>
    10 </html>
     20                    <div class="form-outline mb-4">
     21                        <input name="password" type="password" id="password" class="form-control form-control-lg" />
     22                        <label class="form-label" for="password">Password</label>
     23                    </div>
     24
     25                    <button type="submit" class="btn btn-primary btn-lg btn-block">Најави се</button>
     26
     27                </form>
     28            </div>
     29        </div>
     30    </div>
     31</section>
     32</th:block>
  • FullyStocked/src/main/resources/templates/register.html

    rf11774e r6489bb9  
    33<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
    44<meta charset="UTF-8">
     5<th:block xmlns="http://www.thymeleaf.org">
    56<section class="vh-230" style="background-color: #eee;">
    67  <div class="container h-150">
     
    1415                <p class="text-center h1 fw-bold mb-5 mx-1 mx-md-4 mt-4">Регистрација</p>
    1516
    16                 <form class="mx-1 mx-md-4">
     17                <form class="mx-1 mx-md-4" method="POST" action="/register">
    1718
    1819                  <div class="d-flex flex-row align-items-center mb-4">
    1920                    <i class="fas fa-user fa-lg me-3 fa-fw"></i>
    2021                    <div class="form-outline flex-fill mb-0">
    21                       <input type="text" id="ime" class="form-control" />
     22                      <input name="ime" for="ime" type="text" id="ime" class="form-control" />
    2223                      <label class="form-label" for="ime">Име</label>
    2324                    </div>
     
    2627                    <i class="fas fa-user fa-lg me-3 fa-fw"></i>
    2728                    <div class="form-outline flex-fill mb-0">
    28                       <input type="text" id="prezime" class="form-control" />
     29                      <input name="prezime" for="prezime" type="text" id="prezime" class="form-control" />
    2930                      <label class="form-label" for="prezime">Презиме</label>
    3031                    </div>
     
    3334                    <i class="fas fa-user fa-lg me-3 fa-fw"></i>
    3435                    <div class="form-outline flex-fill mb-0">
    35                       <input type="text" id="username" class="form-control" />
     36                      <input name="username" for="username" type="text" id="username" class="form-control" />
    3637                      <label class="form-label" for="username">Username</label>
    3738                    </div>
     
    4142                    <i class="fas fa-envelope fa-lg me-3 fa-fw"></i>
    4243                    <div class="form-outline flex-fill mb-0">
    43                       <input type="email" id="email" class="form-control" />
     44                      <input name="email" for="email" type="email" id="email" class="form-control" />
    4445                      <label class="form-label" for="email">Email</label>
    4546                    </div>
     
    4950                    <i class="fas fa-lock fa-lg me-3 fa-fw"></i>
    5051                    <div class="form-outline flex-fill mb-0">
    51                       <input type="password" id="password" class="form-control" />
     52                      <input name="password" for="password" type="password" id="password" class="form-control"/>
    5253                      <label class="form-label" for="password">Password</label>
    5354                    </div>
     
    5657                      <i class="fas fa-lock fa-lg me-3 fa-fw"></i>
    5758                      <div class="form-outline flex-fill mb-0">
    58                         <select class="form-select" id="tip">
     59                        <select name="role" for="role" class="form-select" id="tip">
    5960                          <option value="1" disabled>Избери тип на профил</option>
    60                           <option value="2">Магационер</option>
    61                           <option value="3">Добавувач</option>
    62                           <option value="4">Магационер</option>
     61                          <option value="magacioner">Магационер</option>
     62                          <option value="dobavuvac">Добавувач</option>
     63                          <option value="menadzer">Менаџер</option>
    6364                        </select>
    6465                        <label class="form-label" for="tip">Тип</label>
     
    6667                  </div>
    6768                  <div class="d-flex justify-content-center mx-4 mb-3 mb-lg-4">
    68                     <button type="button" class="btn btn-primary btn-lg">Регистрирај се</button>
     69                    <button type="submit" class="btn btn-primary btn-lg">Регистрирај се</button>
    6970                  </div>
    7071
     
    8586  </div>
    8687</section>
     88</th:block>
Note: See TracChangeset for help on using the changeset viewer.