Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ipk 20 basic spring jwt authentication #12

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@
<properties>
<java.version>21</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-security</artifactId>-->
<!-- </dependency>-->

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand All @@ -35,23 +38,39 @@
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
@SpringBootApplication
//@EnableConfigurationProperties
//@EntityScan(basePackages = {"com.MeetMate.user"}) //force scan the packages
public class Main {
public class MeetMateApplication {

public static void main(String[] args) {
SpringApplication.run(Main.class, args);
SpringApplication.run(MeetMateApplication.class, args);
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/MeetMate/roles/Role.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.MeetMate.roles;

public enum Role {

ADMIN,
COMPANY,
CLIENT;
}
8 changes: 0 additions & 8 deletions src/main/java/com/MeetMate/roles/Roles.java

This file was deleted.

68 changes: 7 additions & 61 deletions src/main/java/com/MeetMate/user/User.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.MeetMate.user;

import com.MeetMate.roles.Role;
import jakarta.persistence.*;
import lombok.*;

import java.time.LocalDate;
import java.time.Month;
import java.time.Period;

@Entity
@Table(name = "users")
@Data
public class User {
@Id
@SequenceGenerator(
Expand All @@ -22,16 +25,17 @@ public class User {
private Long id;
private String name;
private LocalDate birthday;
@Setter(AccessLevel.NONE)
private LocalDate createdAt;
private String email;
private String password;
//enum Rolle
private Role role;
//Last login
//(refresh token)
//bool verified

//No need for column in database
@Transient
@Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE)
private int age;

public User() {
Expand Down Expand Up @@ -62,66 +66,8 @@ public User(String email, String password) {
this.password = password;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public LocalDate getBirthday() {
if (this.birthday == null) birthday = LocalDate.EPOCH;
return birthday;
}

public void setBirthday(LocalDate birthday) {
this.birthday = birthday;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public int getAge() {
return Period.between(getBirthday(), LocalDate.now()).getYears();
}

public LocalDate getCreatedAt() {
return createdAt;
}

@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", birthday=" + birthday +
", createdAt=" + createdAt +
", email='" + email + '\'' +
", password='" + password + '\'' +
", age=" + age +
'}';
}

}
11 changes: 5 additions & 6 deletions src/main/java/com/MeetMate/user/UserController.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.MeetMate.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.StreamingHttpOutputMessage;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.service.annotation.PutExchange;

import java.util.List;

Expand All @@ -21,22 +18,24 @@ public UserController(UserService userService) {
}

@GetMapping(path = "get")
@ResponseBody
public User getUser(@RequestParam(name = "id") Long userId) {
return userService.getUser(userId);
}

@GetMapping(path = "getAll")
@ResponseBody
public List<User> getAllUsers() {
return userService.getAllUsers();
}

@PostMapping(path = "post")
public void registerNewUser(@RequestBody MultiValueMap<String, String> formData) {
public void registerNewUser(@RequestParam MultiValueMap<String, String> formData) {
userService.addNewUser(formData);
}

@PostMapping(path = "put")
public void updateUser(@RequestBody MultiValueMap<String, String> formData) {
@PutMapping(path = "put")
public void updateUser(@RequestParam MultiValueMap<String, String> formData) {
System.out.println(formData);
userService.updateUser(formData);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/MeetMate/user/UserService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.MeetMate.user;

import jakarta.transaction.Transactional;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
Expand All @@ -21,6 +20,7 @@ public UserService(UserRepository userRepository) {

public User getUser(Long userId) {
Optional<User> userOptional = userRepository.findUserById(userId);
// ???
userRepository.findUserById(userId);
if (userOptional.isPresent()) {
return userOptional.get();
Expand Down Expand Up @@ -70,7 +70,7 @@ public void updateUser(MultiValueMap<String, String> data) {

if (userRepository.findUserByEmail(email).isEmpty()) {
user.setEmail(email);
}
} // throw error
user.setPassword(password);
}

Expand Down
17 changes: 14 additions & 3 deletions src/test/java/com/MeetMate/MeetMateApplicationTests.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
package com.MeetMate;

import com.MeetMate.user.User;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.LocalDate;
import java.time.Month;

@SpringBootTest
class MeetMateApplicationTests {

@Test
void contextLoads() {
}
@Test
void contextLoads() {
User test = new User(
"Carl A",
LocalDate.of(2000, Month.JANUARY, 21),
"[email protected]",
"carl123");
System.out.println(test.getAge());
System.out.println(test.getName());
}

}
Loading