Skip to content

Commit

Permalink
Merge pull request #37 from Informatik-Projekt-Kurs/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
bencodes07 authored Apr 1, 2024
2 parents 19c909c + 3a0785f commit 296854d
Show file tree
Hide file tree
Showing 20 changed files with 335 additions and 77 deletions.
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

0 comments on commit 296854d

Please sign in to comment.