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

Dev #37

Merged
merged 30 commits into from
Apr 1, 2024
Merged

Dev #37

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
9c16da2
Pom.xml production
bencodes07 Feb 3, 2024
6fc0503
Update pom.xml
bencodes07 Feb 3, 2024
fd5eb00
Update pom.xml
bencodes07 Feb 3, 2024
b1b7119
Update pom.xml
bencodes07 Feb 3, 2024
7ca61f5
Added a new refresh token thats being sent with a refresh request / M…
Gugi-Games Feb 11, 2024
51676ed
Added Docker Compose file and documentation into the readme
Gugi-Games Feb 21, 2024
556be05
Delete data directory
Gugi-Games Feb 21, 2024
428f207
style: format code with Google Java Format
deepsource-autofix[bot] Feb 21, 2024
becd9ce
Merge pull request #30 from Informatik-Projekt-Kurs/IPK-148-Backend-D…
Gugi-Games Feb 22, 2024
abc9dd5
Added internal server errors if a unknown error happens
Gugi-Games Feb 22, 2024
1c69a26
Fixed error codes not having a message and fixed a bug where the logi…
Gugi-Games Feb 22, 2024
c050afd
style: format code with Google Java Format
deepsource-autofix[bot] Feb 22, 2024
cf859ee
Merge pull request #31 from Informatik-Projekt-Kurs/IPK-154-Reformat-…
Gugi-Games Feb 25, 2024
3132963
Added company roles and removed Refresh token rotation
Gugi-Games Feb 29, 2024
b682063
style: format code with Google Java Format
deepsource-autofix[bot] Feb 29, 2024
a2798e9
Update docker-compose.yml
bencodes07 Feb 29, 2024
42611c1
Merge pull request #32 from Informatik-Projekt-Kurs/IPK-159-Add-Compa…
Gugi-Games Mar 16, 2024
9b8b4b0
Added global and IP specific rate limiter
Gugi-Games Mar 16, 2024
25a082b
resolving merge problem
Gugi-Games Mar 25, 2024
3c16940
Merge pull request #34 from Informatik-Projekt-Kurs/IPK-153-Rate-Limi…
Gugi-Games Mar 25, 2024
9dc0dba
Overhauled request log deletion
Gugi-Games Mar 27, 2024
f8d4512
working docker-compose file
Gugi-Games Mar 30, 2024
495206e
Merge remote-tracking branch 'origin/IPK-153-Rate-Limiting' into IPK-…
Gugi-Games Mar 30, 2024
9cc1720
Merge pull request #35 from Informatik-Projekt-Kurs/IPK-153-Rate-Limi…
Gugi-Games Apr 1, 2024
802b754
Split up api creation into a multistep Dockerfile to include the crea…
Gugi-Games Apr 1, 2024
7895215
Split up api creation into a multistep Dockerfile to include the crea…
Gugi-Games Apr 1, 2024
1869912
Changed host port of api from 8080 to 8081 for frontend integration
Gugi-Games Apr 1, 2024
9ffb325
Merge branch 'dev' into IPK-148-Backend-Deployment
Gugi-Games Apr 1, 2024
2320cec
Merge pull request #36 from Informatik-Projekt-Kurs/IPK-148-Backend-D…
Gugi-Games Apr 1, 2024
3a0785f
Merge branch 'prod' into dev
bencodes07 Apr 1, 2024
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ target/
!**/src/main/**/target/
!**/src/test/**/target/

### Docker Compose ###
/data/

### STS ###
.apt_generated
.classpath
Expand Down
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Build the application
FROM maven:3.8.5-openjdk-17 as builder
WORKDIR /app
COPY pom.xml .
RUN mvn dependency:go-offline
COPY src/ ./src/
RUN mvn clean package -DskipTests=true

# Run the application
FROM openjdk:17
WORKDIR /app
COPY --from=builder /app/target/MeetMate.jar /app/MeetMate.jar
CMD ["java", "-jar", "MeetMate.jar"]
27 changes: 27 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
version: '3'
services:

meet-mate:
build:
context: .
dockerfile: Dockerfile
ports:
- "8081:8080"
depends_on:
- postgres
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/test
SPRING_DATASOURCE_USERNAME: postgres
SPRING_DATASOURCE_PASSWORD: 1234

postgres:
image: postgres:16
restart: always
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: 1234
POSTGRES_DB: test
ports:
- "5432:5432"
volumes:
- ./data:/var/lib/postgresql/data
24 changes: 24 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.release>17</maven.compiler.release>
<maven.compiler.verbose>true</maven.compiler.verbose>
</properties>

<distributionManagement>
<repository>
<uniqueVersion>false</uniqueVersion>
<id>backend</id>
<name>MeetMate Backend</name>
<url>https://github.com/Informatik-Projekt-Kurs/backend</url>
<layout>default</layout>
</repository>
</distributionManagement>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -108,6 +123,7 @@
</dependencies>

<build>
<finalName>MeetMate</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
Expand All @@ -129,6 +145,14 @@
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down
21 changes: 19 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
# Backend Repo
# Backend Repository of MeetMate

Das Reich von Tim :D
## Setting up the application

All the following commands are to be executed in the root directory of the project.

### Creating the Docker image
Make the jar file with<br>
> mvn clean package
---
### Running the application with Docker Compose
Start the application using
>docker-compose up
---
### Stopping the application
To stop the running application use either `Ctrl + C` or
>docker-compose stop

or delete the created containers with
>docker-compose down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.MeetMate.experiments;

public @interface AuthenticationHeader {}
4 changes: 4 additions & 0 deletions src/main/java/com/MeetMate/experiments/Experimentational.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
package com.MeetMate.experiments;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.SOURCE)
public @interface Experimentational {}
17 changes: 17 additions & 0 deletions src/main/java/com/MeetMate/experiments/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.MeetMate.experiments;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(path = "test")
public class Test {
//Link in SecurityConfig.java
@PostMapping(path = "test")
public String getUser(){
return "asdasdasd";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ public class AuthenticationResponse {

String access_Token;
long expires_at;
String refresh_token;
String refresh_Token;
}
5 changes: 3 additions & 2 deletions src/main/java/com/MeetMate/roles/Role.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

public enum Role {
ADMIN,
COMPANY,
CLIENT;
CLIENT,
COMPANY_OWNER,
COMPANY_MEMBER;
}
2 changes: 2 additions & 0 deletions src/main/java/com/MeetMate/security/ApplicationConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.MeetMate.user.UserRepository;
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
Expand All @@ -18,6 +19,7 @@
public class ApplicationConfig {

private final UserRepository userRepository;
private final JwtService jwtService;

@Bean
public UserDetailsService userDetailsService() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
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.core.Ordered;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
Expand All @@ -15,6 +15,8 @@
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import java.io.IOException;

@Component
@RequiredArgsConstructor
public class JwtAuthenticationFilter extends OncePerRequestFilter {
Expand Down Expand Up @@ -57,4 +59,5 @@ protected void doFilterInternal(
}
filterChain.doFilter(request, response);
}

}
2 changes: 1 addition & 1 deletion src/main/java/com/MeetMate/security/JwtService.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public String generateRefreshToken(User user) throws EntityNotFoundException {

// Claims::getSubject
public String extractUserEmail(String token) {
return extractClaim(token, Claims -> Claims.getSubject());
return extractClaim(token, Claims::getSubject);
}

@Experimentational
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/MeetMate/security/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws
.authorizeHttpRequests(
authorizeRequests ->
authorizeRequests
.requestMatchers("/api/user/login", "/api/user/signup")
.requestMatchers("/api/user/login", "/api/user/signup", "/test/test")
// .requestMatchers("/**")
.permitAll() // Whitelist
.anyRequest()
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/com/MeetMate/throttle/GlobalRateLimiter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.MeetMate.throttle;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import java.io.IOException;
import java.util.LinkedList;

@Component
@RequiredArgsConstructor
public class GlobalRateLimiter extends OncePerRequestFilter {

private final LinkedList<Long> requests = new LinkedList<>();
private final int maxRequests = 500;
private final long refreshTime = 1000 * 1; // 1 second

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

requests.addLast(System.currentTimeMillis());

clearRequests();

if (requests.size() > maxRequests) {
response.setStatus(429);
response.getWriter().write("Too many requests");
return;
}

filterChain.doFilter(request, response);
}

private void clearRequests() {
while (!requests.isEmpty()
&& System.currentTimeMillis() - requests.getFirst() > refreshTime)
requests.remove();

}

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

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;

@Component
@RequiredArgsConstructor
public class IPRateLimiter extends OncePerRequestFilter {

private final HashMap<String, LinkedList<Long>> requests = new HashMap<>();
private final int maxRequests = 2;
private final long refreshTime = 1000 * 10; // 10 seconds

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

String ip = request.getRemoteAddr();

if (requests.containsKey(ip))
requests.get(ip).addLast(System.currentTimeMillis());
else
requests.put(ip, new LinkedList<Long>(Collections.singleton(System.currentTimeMillis())));

clearRequests(ip);

if (requests.get(ip).size() > maxRequests) {
response.setStatus(429);
response.getWriter().write("Too many requests");
return;
}

filterChain.doFilter(request, response);
}

private void clearRequests(String ip) {
while (!requests.isEmpty()
&& System.currentTimeMillis() - requests.get(ip).getFirst() > refreshTime)
requests.get(ip).remove();

}

}
Loading
Loading