Skip to content

Commit

Permalink
Merge pull request #28 from Informatik-Projekt-Kurs/dev
Browse files Browse the repository at this point in the history
Full User System
  • Loading branch information
bencodes07 authored Feb 3, 2024
2 parents b37dfb9 + 6ec1334 commit 147d65d
Show file tree
Hide file tree
Showing 19 changed files with 704 additions and 269 deletions.
10 changes: 10 additions & 0 deletions .deepsource.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version = 1

[[analyzers]]
name = "java"

[analyzers.meta]
runtime_version = "19"

[[transformers]]
name = "google-java-format"
50 changes: 44 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
<version>3.2.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com</groupId>
Expand All @@ -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,58 @@
<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>

<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.11.5</version>
</dependency>

<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.11.5</version>
</dependency>

<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>0.11.5</version>
</dependency>

</dependencies>

<build>
<plugins>
Expand Down
16 changes: 0 additions & 16 deletions src/main/java/com/MeetMate/Main.java

This file was deleted.

14 changes: 14 additions & 0 deletions src/main/java/com/MeetMate/MeetMateApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.MeetMate;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
// @EnableConfigurationProperties
// @EntityScan(basePackages = {"com.MeetMate.user"}) //force scan the packages
public class MeetMateApplication {

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

public @interface Experimentational {}
15 changes: 15 additions & 0 deletions src/main/java/com/MeetMate/response/AuthenticationResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.MeetMate.response;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

@Data
@AllArgsConstructor
@Builder
public class AuthenticationResponse {

String access_Token;
long expires_at;
String refresh_token;
}
19 changes: 19 additions & 0 deletions src/main/java/com/MeetMate/response/GetResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.MeetMate.response;

import com.MeetMate.roles.Role;
import java.time.LocalDate;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

@Data
@AllArgsConstructor
@Builder
public class GetResponse {

long id;
String name;
LocalDate created_at;
String email;
Role role;
}
14 changes: 14 additions & 0 deletions src/main/java/com/MeetMate/response/RefreshResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.MeetMate.response;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

@Data
@AllArgsConstructor
@Builder
public class RefreshResponse {

String access_Token;
long expires_at;
}
7 changes: 7 additions & 0 deletions src/main/java/com/MeetMate/roles/Role.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
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.

48 changes: 48 additions & 0 deletions src/main/java/com/MeetMate/security/ApplicationConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.MeetMate.security;

import com.MeetMate.user.UserRepository;
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
@RequiredArgsConstructor
public class ApplicationConfig {

private final UserRepository userRepository;

@Bean
public UserDetailsService userDetailsService() {
return username ->
userRepository
.findUserByEmail(username)
.orElseThrow(() -> new EntityNotFoundException("User not found"));
}

@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration configuration)
throws Exception {
return configuration.getAuthenticationManager();
}

@Bean
public AuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
daoAuthenticationProvider.setUserDetailsService(userDetailsService());
daoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
return daoAuthenticationProvider;
}

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
60 changes: 60 additions & 0 deletions src/main/java/com/MeetMate/security/JwtAuthenticationFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.MeetMate.security;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

@Component
@RequiredArgsConstructor
public class JwtAuthenticationFilter extends OncePerRequestFilter {

private final JwtService jwtService;
private final UserDetailsService userDetailsService;

@Override
protected void doFilterInternal(
@NotNull HttpServletRequest request,
@NotNull HttpServletResponse response,
@NotNull FilterChain filterChain)
throws ServletException, IOException {

final String authHeader = request.getHeader("Authorization");
final String jwt;
final String userEmail;
if (authHeader == null || !authHeader.startsWith("Bearer ")) {
filterChain.doFilter(request, response);
return;
}
// extract jwt token
// beginIndex is 7 bc "Bearer " is 7
jwt = authHeader.substring(7);
userEmail = jwtService.extractUserEmail(jwt);

if (userEmail != null
&& SecurityContextHolder.getContext().getAuthentication()
== null) { // check f if user is already authenticated
UserDetails userDetails = userDetailsService.loadUserByUsername(userEmail);

if (jwtService.isTokenValid(jwt, userDetails)) {
UsernamePasswordAuthenticationToken authToken =
new UsernamePasswordAuthenticationToken(
userDetails, null, userDetails.getAuthorities());

authToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
SecurityContextHolder.getContext().setAuthentication(authToken);
}
}
filterChain.doFilter(request, response);
}
}
Loading

0 comments on commit 147d65d

Please sign in to comment.