Skip to content

Commit f6687cb

Browse files
committed
feat: added volume to java api
1 parent 49bcb8f commit f6687cb

File tree

6 files changed

+35
-6
lines changed

6 files changed

+35
-6
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ RUN mvn clean install
99

1010
FROM openjdk:21-jdk-slim
1111
EXPOSE 8080
12+
RUN mkdir -p /app/uploads
1213
COPY --from=build /target/restful-api-users-java-0.0.1-SNAPSHOT.jar app.jar
1314

1415
ENTRYPOINT [ "java", "-jar", "app.jar" ]

docker-compose.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
version: '3.8'
2-
31
services:
42
postgres:
53
container_name: restful-api-users-java-pg
@@ -10,6 +8,8 @@ services:
108
- POSTGRES_USER=admin
119
- POSTGRES_PASSWORD=admin
1210
- POSTGRES_DB=restful_users
11+
volumes:
12+
- pgdata:/var/lib/postgresql/data
1313
networks:
1414
- backend
1515

@@ -24,8 +24,13 @@ services:
2424
- 8080:8080
2525
env_file:
2626
- ./src/main/resources/application-dev.properties
27+
volumes:
28+
- ./src/main/resources/public:/app/uploads
2729
networks:
2830
- backend
2931

3032
networks:
31-
backend:
33+
backend:
34+
35+
volumes:
36+
pgdata:

src/main/java/com/joaov1ct0r/restful_api_users_java/modules/domain/services/FileStorageService.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212

1313
@Service
1414
public class FileStorageService {
15-
private final Path fileStorageLocation = Paths.get("uploads").toAbsolutePath().normalize();
15+
// private final Path fileStorageLocation = Paths.get("C:\\Users\\jov90\\projects\\java-spring-boot-users\\src\\main\\resources\\public").toAbsolutePath().normalize();
16+
private final Path fileStorageLocation = Paths.get("/app/uploads").toAbsolutePath().normalize();
1617

1718
public FileStorageService() throws IOException {
1819
Files.createDirectories(this.fileStorageLocation);
1920
}
2021

2122
public String storeFile(MultipartFile file) throws IOException {
23+
System.out.println("path: " + this.fileStorageLocation);
2224
String fileName = StringUtils.cleanPath(file.getOriginalFilename());
2325

2426
if (fileName.contains("..")) {
@@ -30,6 +32,6 @@ public String storeFile(MultipartFile file) throws IOException {
3032
Path targetLocation = this.fileStorageLocation.resolve(newFileName);
3133
Files.copy(file.getInputStream(), targetLocation, StandardCopyOption.REPLACE_EXISTING);
3234

33-
return "http://localhost:8080/uploads/" + newFileName;
35+
return "http://localhost:8080/" + newFileName;
3436
}
3537
}

src/main/java/com/joaov1ct0r/restful_api_users_java/security/SecurityConfig.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
99
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
1010
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
11+
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
1112
import org.springframework.security.config.http.SessionCreationPolicy;
1213
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
1314
import org.springframework.security.crypto.password.PasswordEncoder;
@@ -27,11 +28,20 @@ public class SecurityConfig {
2728
"/actuator/**",
2829
};
2930

31+
@Bean
32+
public WebSecurityCustomizer webSecurityCustomizer() {
33+
return (web -> web.ignoring()
34+
.requestMatchers("/**.jpg", "/**.png", "/**.jpeg")
35+
.requestMatchers("/public/**")
36+
);
37+
}
38+
3039
@Bean
3140
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
3241
return http.csrf(csrf -> csrf.disable())
3342
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
3443
.authorizeHttpRequests(auth -> auth
44+
.requestMatchers("/public/**").permitAll()
3545
.requestMatchers(HttpMethod.POST, "/signin/").permitAll()
3646
.requestMatchers(HttpMethod.POST, "/signup/").permitAll()
3747
.requestMatchers(HttpMethod.PUT, "/reset_password/").permitAll()

src/main/java/com/joaov1ct0r/restful_api_users_java/security/SecurityFilter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ private Optional<String> getToken(HttpServletRequest request) {
7474
protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException {
7575
String path = request.getServletPath();
7676

77+
if (path.endsWith(".jpg")) return true;
78+
if (path.endsWith(".jpeg")) return true;
79+
if (path.endsWith(".png")) return true;
80+
7781
if (path.equals("/signin/")) return true;
7882

7983
if (path.equals("/signup/")) return true;

src/main/java/com/joaov1ct0r/restful_api_users_java/security/WebConfig.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
import org.springframework.context.annotation.Configuration;
55
import org.springframework.context.annotation.Bean;
66
import org.springframework.web.servlet.config.annotation.CorsRegistry;
7+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
78
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
89

910
@Configuration
10-
public class WebConfig {
11+
public class WebConfig implements WebMvcConfigurer {
1112
@Value("${SPRING_PROFILES_ACTIVE:prod}")
1213
private String env;
1314

@@ -23,5 +24,11 @@ public void addCorsMappings(CorsRegistry registry) {
2324
}
2425
};
2526
}
27+
28+
@Override
29+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
30+
registry.addResourceHandler("/uploads/**")
31+
.addResourceLocations("file:" + "/app/uploads" + "/");
32+
}
2633
}
2734

0 commit comments

Comments
 (0)