Index: trip-planner/pom.xml
===================================================================
--- trip-planner/pom.xml	(revision 8d391a12ef6baa3aefe9611a6f7c5b569977c946)
+++ trip-planner/pom.xml	(revision 1ad8e64a349fcc7c996155279b16efc2cfe001dd)
@@ -80,4 +80,23 @@
             <version>3.0.3</version>
         </dependency>
+        <!--SPRING SECURITY-->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-security</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.google.code.gson</groupId>
+            <artifactId>gson</artifactId>
+            <version>2.8.5</version>
+        </dependency>
+        <dependency>
+            <groupId>io.jsonwebtoken</groupId>
+            <artifactId>jjwt</artifactId>
+            <version>0.9.0</version>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-validation</artifactId>
+        </dependency>
     </dependencies>
 
Index: trip-planner/src/main/java/finki/diplomska/tripplanner/TripPlannerApplication.java
===================================================================
--- trip-planner/src/main/java/finki/diplomska/tripplanner/TripPlannerApplication.java	(revision 8d391a12ef6baa3aefe9611a6f7c5b569977c946)
+++ trip-planner/src/main/java/finki/diplomska/tripplanner/TripPlannerApplication.java	(revision 1ad8e64a349fcc7c996155279b16efc2cfe001dd)
@@ -4,4 +4,6 @@
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.Bean;
+import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
 
 @SpringBootApplication
@@ -9,4 +11,8 @@
 public class TripPlannerApplication {
 
+    @Bean
+    BCryptPasswordEncoder bCryptPasswordEncoder(){
+        return new BCryptPasswordEncoder();
+    }
     public static void main(String[] args) {
         SpringApplication.run(TripPlannerApplication.class, args);
Index: trip-planner/src/main/java/finki/diplomska/tripplanner/models/User.java
===================================================================
--- trip-planner/src/main/java/finki/diplomska/tripplanner/models/User.java	(revision 1ad8e64a349fcc7c996155279b16efc2cfe001dd)
+++ trip-planner/src/main/java/finki/diplomska/tripplanner/models/User.java	(revision 1ad8e64a349fcc7c996155279b16efc2cfe001dd)
@@ -0,0 +1,84 @@
+package finki.diplomska.tripplanner.models;
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.Setter;
+import org.springframework.security.core.GrantedAuthority;
+import org.springframework.security.core.userdetails.UserDetails;
+
+import javax.persistence.*;
+import javax.validation.constraints.Email;
+import javax.validation.constraints.NotBlank;
+import java.util.Collection;
+import java.util.Date;
+
+@Entity
+@Table(name = "users")
+@AllArgsConstructor
+@Getter
+@Setter
+public class User implements UserDetails {
+
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    private Long id;
+
+    @Email(message = "Username needs to be an email")
+    @NotBlank(message = "username is required")
+    @Column(unique = true)
+    private String username;
+    @NotBlank(message = "Please enter your full name")
+    private String fullName;
+    @NotBlank(message = "Password field is required")
+    private String password;
+    @Transient
+    private String confirmPassword;
+    private Date create_At;
+    private Date update_At;
+
+    //OneToMany with Project
+
+    public User() {
+    }
+
+
+    @PrePersist
+    protected void onCreate(){
+        this.create_At = new Date();
+    }
+    @PreUpdate
+    protected void onUpdate(){
+        this.update_At = new Date();
+    }
+
+    @Override
+    @JsonIgnore
+    public Collection<? extends GrantedAuthority> getAuthorities() {
+        return null;
+    }
+
+    @Override
+    @JsonIgnore
+    public boolean isAccountNonExpired() {
+        return true;
+    }
+
+    @Override
+    @JsonIgnore
+    public boolean isAccountNonLocked() {
+        return true;
+    }
+
+    @Override
+    @JsonIgnore
+    public boolean isCredentialsNonExpired() {
+        return true;
+    }
+
+    @Override
+    @JsonIgnore
+    public boolean isEnabled() {
+        return true;
+    }
+}
Index: trip-planner/src/main/java/finki/diplomska/tripplanner/models/dto/PlannerDto.java
===================================================================
--- trip-planner/src/main/java/finki/diplomska/tripplanner/models/dto/PlannerDto.java	(revision 8d391a12ef6baa3aefe9611a6f7c5b569977c946)
+++ trip-planner/src/main/java/finki/diplomska/tripplanner/models/dto/PlannerDto.java	(revision 1ad8e64a349fcc7c996155279b16efc2cfe001dd)
@@ -3,5 +3,4 @@
 import lombok.Data;
 
-import java.util.List;
 
 @Data
Index: trip-planner/src/main/java/finki/diplomska/tripplanner/models/dto/UserDto.java
===================================================================
--- trip-planner/src/main/java/finki/diplomska/tripplanner/models/dto/UserDto.java	(revision 1ad8e64a349fcc7c996155279b16efc2cfe001dd)
+++ trip-planner/src/main/java/finki/diplomska/tripplanner/models/dto/UserDto.java	(revision 1ad8e64a349fcc7c996155279b16efc2cfe001dd)
@@ -0,0 +1,30 @@
+package finki.diplomska.tripplanner.models.dto;
+
+
+import lombok.Data;
+
+import java.util.Date;
+
+@Data
+public class UserDto {
+    private String username;
+
+    private String fullName;
+
+    private String password;
+
+    private String confirmPassword;
+
+    private Date create_At;
+
+    private Date update_At;
+
+    public UserDto(String username, String fullName, String password, String confirmPassword, Date create_At, Date update_At) {
+        this.username = username;
+        this.fullName = fullName;
+        this.password = password;
+        this.confirmPassword = confirmPassword;
+        this.create_At = create_At;
+        this.update_At = update_At;
+    }
+}
Index: trip-planner/src/main/java/finki/diplomska/tripplanner/models/exceptions/CustomResponseEntityExceptionHandler.java
===================================================================
--- trip-planner/src/main/java/finki/diplomska/tripplanner/models/exceptions/CustomResponseEntityExceptionHandler.java	(revision 1ad8e64a349fcc7c996155279b16efc2cfe001dd)
+++ trip-planner/src/main/java/finki/diplomska/tripplanner/models/exceptions/CustomResponseEntityExceptionHandler.java	(revision 1ad8e64a349fcc7c996155279b16efc2cfe001dd)
@@ -0,0 +1,18 @@
+package finki.diplomska.tripplanner.models.exceptions;
+
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.context.request.WebRequest;
+
+@ControllerAdvice
+@RestController
+public class CustomResponseEntityExceptionHandler {
+    @ExceptionHandler
+    public final ResponseEntity<Object> handleUsernameAlreadyExists(UsernameAlreadyExistsException ex, WebRequest request){
+        UsernameAlreadyExistsResponse exceptionResponse = new UsernameAlreadyExistsResponse(ex.getMessage());
+        return new ResponseEntity(exceptionResponse, HttpStatus.BAD_REQUEST);
+    }
+}
Index: trip-planner/src/main/java/finki/diplomska/tripplanner/models/exceptions/InvalidLoginResponse.java
===================================================================
--- trip-planner/src/main/java/finki/diplomska/tripplanner/models/exceptions/InvalidLoginResponse.java	(revision 1ad8e64a349fcc7c996155279b16efc2cfe001dd)
+++ trip-planner/src/main/java/finki/diplomska/tripplanner/models/exceptions/InvalidLoginResponse.java	(revision 1ad8e64a349fcc7c996155279b16efc2cfe001dd)
@@ -0,0 +1,27 @@
+package finki.diplomska.tripplanner.models.exceptions;
+
+public class InvalidLoginResponse {
+    private String username;
+    private String password;
+
+    public InvalidLoginResponse() {
+        this.username = "Invalid username";
+        this.password = "Invalid password";
+    }
+
+    public String getUsername() {
+        return username;
+    }
+
+    public void setUsername(String username) {
+        this.username = username;
+    }
+
+    public String getPassword() {
+        return password;
+    }
+
+    public void setPassword(String password) {
+        this.password = password;
+    }
+}
Index: trip-planner/src/main/java/finki/diplomska/tripplanner/models/exceptions/UsernameAlreadyExistsException.java
===================================================================
--- trip-planner/src/main/java/finki/diplomska/tripplanner/models/exceptions/UsernameAlreadyExistsException.java	(revision 1ad8e64a349fcc7c996155279b16efc2cfe001dd)
+++ trip-planner/src/main/java/finki/diplomska/tripplanner/models/exceptions/UsernameAlreadyExistsException.java	(revision 1ad8e64a349fcc7c996155279b16efc2cfe001dd)
@@ -0,0 +1,12 @@
+package finki.diplomska.tripplanner.models.exceptions;
+
+import org.springframework.http.HttpStatus;
+import org.springframework.web.bind.annotation.ResponseStatus;
+
+@ResponseStatus(HttpStatus.BAD_REQUEST)
+public class UsernameAlreadyExistsException extends RuntimeException{
+
+    public UsernameAlreadyExistsException (String message){
+        super(message);
+    }
+}
Index: trip-planner/src/main/java/finki/diplomska/tripplanner/models/exceptions/UsernameAlreadyExistsResponse.java
===================================================================
--- trip-planner/src/main/java/finki/diplomska/tripplanner/models/exceptions/UsernameAlreadyExistsResponse.java	(revision 1ad8e64a349fcc7c996155279b16efc2cfe001dd)
+++ trip-planner/src/main/java/finki/diplomska/tripplanner/models/exceptions/UsernameAlreadyExistsResponse.java	(revision 1ad8e64a349fcc7c996155279b16efc2cfe001dd)
@@ -0,0 +1,17 @@
+package finki.diplomska.tripplanner.models.exceptions;
+
+public class UsernameAlreadyExistsResponse {
+    private String username;
+
+    public UsernameAlreadyExistsResponse(String username) {
+        this.username = username;
+    }
+
+    public String getUsername() {
+        return username;
+    }
+
+    public void setUsername(String username) {
+        this.username = username;
+    }
+}
Index: trip-planner/src/main/java/finki/diplomska/tripplanner/repository/jpa/JpaUserRepository.java
===================================================================
--- trip-planner/src/main/java/finki/diplomska/tripplanner/repository/jpa/JpaUserRepository.java	(revision 1ad8e64a349fcc7c996155279b16efc2cfe001dd)
+++ trip-planner/src/main/java/finki/diplomska/tripplanner/repository/jpa/JpaUserRepository.java	(revision 1ad8e64a349fcc7c996155279b16efc2cfe001dd)
@@ -0,0 +1,9 @@
+package finki.diplomska.tripplanner.repository.jpa;
+
+import finki.diplomska.tripplanner.models.User;
+import org.springframework.data.repository.CrudRepository;
+import org.springframework.stereotype.Repository;
+
+@Repository
+public interface JpaUserRepository extends CrudRepository<User, Long> {
+}
Index: trip-planner/src/main/java/finki/diplomska/tripplanner/security/JwtAuthenticationEntryPoint.java
===================================================================
--- trip-planner/src/main/java/finki/diplomska/tripplanner/security/JwtAuthenticationEntryPoint.java	(revision 1ad8e64a349fcc7c996155279b16efc2cfe001dd)
+++ trip-planner/src/main/java/finki/diplomska/tripplanner/security/JwtAuthenticationEntryPoint.java	(revision 1ad8e64a349fcc7c996155279b16efc2cfe001dd)
@@ -0,0 +1,28 @@
+package finki.diplomska.tripplanner.security;
+
+import com.google.gson.Gson;
+import finki.diplomska.tripplanner.models.exceptions.InvalidLoginResponse;
+import org.springframework.security.core.AuthenticationException;
+import org.springframework.security.web.AuthenticationEntryPoint;
+import org.springframework.stereotype.Component;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+@Component
+public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {
+    @Override
+    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
+                         AuthenticationException e) throws IOException, ServletException {
+
+        InvalidLoginResponse loginResponse = new InvalidLoginResponse();
+        String jsonLoginResponse = new Gson().toJson(loginResponse);
+
+
+        httpServletResponse.setContentType("application/json");
+        httpServletResponse.setStatus(401);
+        httpServletResponse.getWriter().print(jsonLoginResponse);
+    }
+}
Index: trip-planner/src/main/java/finki/diplomska/tripplanner/security/SecurityConfig.java
===================================================================
--- trip-planner/src/main/java/finki/diplomska/tripplanner/security/SecurityConfig.java	(revision 1ad8e64a349fcc7c996155279b16efc2cfe001dd)
+++ trip-planner/src/main/java/finki/diplomska/tripplanner/security/SecurityConfig.java	(revision 1ad8e64a349fcc7c996155279b16efc2cfe001dd)
@@ -0,0 +1,48 @@
+package finki.diplomska.tripplanner.security;
+
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
+import org.springframework.security.config.annotation.web.builders.HttpSecurity;
+import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
+import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
+import org.springframework.security.config.http.SessionCreationPolicy;
+
+@Configuration
+@EnableWebSecurity
+@EnableGlobalMethodSecurity(
+        securedEnabled = true,
+        jsr250Enabled = true,
+        prePostEnabled = true
+)
+public class SecurityConfig extends WebSecurityConfigurerAdapter {
+
+    @Autowired
+    private JwtAuthenticationEntryPoint unauthorizedHandler;
+
+    @Override
+    protected void configure(HttpSecurity http) throws Exception {
+        http.cors().and().csrf().disable()
+                .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
+                .sessionManagement()
+                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
+                .and()
+                .headers().frameOptions().sameOrigin() //To enable H2 Database
+                .and()
+                .authorizeRequests()
+                .antMatchers(
+                        "/",
+                        "/favicon.ico",
+                        "/**/*.png",
+                        "/**/*.gif",
+                        "/**/*.svg",
+                        "/**/*.jpg",
+                        "/**/*.html",
+                        "/**/*.css",
+                        "/**/*.js"
+                ).permitAll()
+                .antMatchers("/api/users/**").permitAll()
+                .anyRequest().authenticated();
+    }
+}
Index: trip-planner/src/main/java/finki/diplomska/tripplanner/service/UserService.java
===================================================================
--- trip-planner/src/main/java/finki/diplomska/tripplanner/service/UserService.java	(revision 1ad8e64a349fcc7c996155279b16efc2cfe001dd)
+++ trip-planner/src/main/java/finki/diplomska/tripplanner/service/UserService.java	(revision 1ad8e64a349fcc7c996155279b16efc2cfe001dd)
@@ -0,0 +1,11 @@
+package finki.diplomska.tripplanner.service;
+
+import finki.diplomska.tripplanner.models.User;
+
+
+
+public interface UserService {
+
+    User saveUser (User user);
+
+}
Index: trip-planner/src/main/java/finki/diplomska/tripplanner/service/impl/MapValidationErrorService.java
===================================================================
--- trip-planner/src/main/java/finki/diplomska/tripplanner/service/impl/MapValidationErrorService.java	(revision 1ad8e64a349fcc7c996155279b16efc2cfe001dd)
+++ trip-planner/src/main/java/finki/diplomska/tripplanner/service/impl/MapValidationErrorService.java	(revision 1ad8e64a349fcc7c996155279b16efc2cfe001dd)
@@ -0,0 +1,28 @@
+package finki.diplomska.tripplanner.service.impl;
+
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.stereotype.Service;
+import org.springframework.validation.BindingResult;
+import org.springframework.validation.FieldError;
+
+import java.util.HashMap;
+import java.util.Map;
+
+@Service
+public class MapValidationErrorService {
+    public ResponseEntity<?> MapValidationService(BindingResult result){
+
+        if(result.hasErrors()){
+            Map<String, String> errorMap = new HashMap<>();
+
+            for(FieldError error: result.getFieldErrors()){
+                errorMap.put(error.getField(), error.getDefaultMessage());
+            }
+            return new ResponseEntity<Map<String, String>>(errorMap, HttpStatus.BAD_REQUEST);
+        }
+
+        return null;
+
+    }
+}
Index: trip-planner/src/main/java/finki/diplomska/tripplanner/service/impl/UserServiceImpl.java
===================================================================
--- trip-planner/src/main/java/finki/diplomska/tripplanner/service/impl/UserServiceImpl.java	(revision 1ad8e64a349fcc7c996155279b16efc2cfe001dd)
+++ trip-planner/src/main/java/finki/diplomska/tripplanner/service/impl/UserServiceImpl.java	(revision 1ad8e64a349fcc7c996155279b16efc2cfe001dd)
@@ -0,0 +1,37 @@
+package finki.diplomska.tripplanner.service.impl;
+
+import finki.diplomska.tripplanner.models.User;
+import finki.diplomska.tripplanner.models.dto.UserDto;
+import finki.diplomska.tripplanner.models.exceptions.UsernameAlreadyExistsException;
+import finki.diplomska.tripplanner.repository.jpa.JpaUserRepository;
+import finki.diplomska.tripplanner.service.UserService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
+import org.springframework.stereotype.Service;
+
+
+@Service
+public class UserServiceImpl  implements UserService {
+
+    @Autowired
+    private JpaUserRepository userRepository;
+
+    @Autowired
+    private BCryptPasswordEncoder bCryptPasswordEncoder;
+
+    public User saveUser (User newUser){
+        try{
+            newUser.setPassword(bCryptPasswordEncoder.encode(newUser.getPassword()));
+            //Username has to be unique (exception)
+            newUser.setUsername(newUser.getUsername());
+            // Make sure that password and confirmPassword match
+            // We don't persist or show the confirmPassword
+            newUser.setConfirmPassword("");
+            return this.userRepository.save(newUser);
+        }catch(Exception e){
+            throw new UsernameAlreadyExistsException("Username '"+newUser.getUsername()+ "' already exists");
+        }
+
+    }
+
+}
Index: trip-planner/src/main/java/finki/diplomska/tripplanner/validator/UserValidator.java
===================================================================
--- trip-planner/src/main/java/finki/diplomska/tripplanner/validator/UserValidator.java	(revision 1ad8e64a349fcc7c996155279b16efc2cfe001dd)
+++ trip-planner/src/main/java/finki/diplomska/tripplanner/validator/UserValidator.java	(revision 1ad8e64a349fcc7c996155279b16efc2cfe001dd)
@@ -0,0 +1,26 @@
+package finki.diplomska.tripplanner.validator;
+
+import finki.diplomska.tripplanner.models.User;
+import org.springframework.stereotype.Component;
+import org.springframework.validation.Errors;
+import org.springframework.validation.Validator;
+
+
+@Component
+public class UserValidator implements Validator {
+    @Override
+    public boolean supports(Class<?> aClass) {
+        return User.class.equals(aClass);
+    }
+
+    @Override
+    public void validate(Object object, Errors errors) {
+        User user = (User) object;
+        if(user.getPassword().length() < 6){
+            errors.rejectValue("password","Length", "Password must be at least 6 characters" );
+        }
+        if(!user.getPassword().equals(user.getConfirmPassword())){
+           errors.rejectValue("confirmPassword", "Match", "Password must match");
+        }
+    }
+}
Index: trip-planner/src/main/java/finki/diplomska/tripplanner/web/rest/UserController.java
===================================================================
--- trip-planner/src/main/java/finki/diplomska/tripplanner/web/rest/UserController.java	(revision 1ad8e64a349fcc7c996155279b16efc2cfe001dd)
+++ trip-planner/src/main/java/finki/diplomska/tripplanner/web/rest/UserController.java	(revision 1ad8e64a349fcc7c996155279b16efc2cfe001dd)
@@ -0,0 +1,41 @@
+package finki.diplomska.tripplanner.web.rest;
+
+import finki.diplomska.tripplanner.models.User;
+import finki.diplomska.tripplanner.models.dto.UserDto;
+import finki.diplomska.tripplanner.service.UserService;
+import finki.diplomska.tripplanner.service.impl.MapValidationErrorService;
+import finki.diplomska.tripplanner.validator.UserValidator;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.validation.BindingResult;
+import org.springframework.web.bind.annotation.*;
+
+import javax.validation.Valid;
+
+@RestController
+@CrossOrigin(origins = "http://localhost:4200")
+@RequestMapping(value = "/api/users")
+public class UserController {
+
+    @Autowired
+    private MapValidationErrorService mapValidationErrorService;
+
+    @Autowired
+    private UserService userService;
+
+    @Autowired
+    private UserValidator userValidator;
+
+    @PostMapping("/register")
+    public ResponseEntity<?> registerUser(@Valid @RequestBody User user, BindingResult result){
+        // Validate passwords match
+        userValidator.validate(user, result);
+        ResponseEntity<?> errorMap = mapValidationErrorService.MapValidationService(result);
+        if(errorMap != null)return errorMap;
+
+        User newUser = userService.saveUser(user);
+
+        return  new ResponseEntity<User>(newUser, HttpStatus.CREATED);
+    }
+}
