File tree Expand file tree Collapse file tree 6 files changed +35
-6
lines changed
src/main/java/com/joaov1ct0r/restful_api_users_java Expand file tree Collapse file tree 6 files changed +35
-6
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ RUN mvn clean install
9
9
10
10
FROM openjdk:21-jdk-slim
11
11
EXPOSE 8080
12
+ RUN mkdir -p /app/uploads
12
13
COPY --from=build /target/restful-api-users-java-0.0.1-SNAPSHOT.jar app.jar
13
14
14
15
ENTRYPOINT [ "java" , "-jar" , "app.jar" ]
Original file line number Diff line number Diff line change 1
- version : ' 3.8'
2
-
3
1
services :
4
2
postgres :
5
3
container_name : restful-api-users-java-pg
@@ -10,6 +8,8 @@ services:
10
8
- POSTGRES_USER=admin
11
9
- POSTGRES_PASSWORD=admin
12
10
- POSTGRES_DB=restful_users
11
+ volumes :
12
+ - pgdata:/var/lib/postgresql/data
13
13
networks :
14
14
- backend
15
15
@@ -24,8 +24,13 @@ services:
24
24
- 8080:8080
25
25
env_file :
26
26
- ./src/main/resources/application-dev.properties
27
+ volumes :
28
+ - ./src/main/resources/public:/app/uploads
27
29
networks :
28
30
- backend
29
31
30
32
networks :
31
- backend:
33
+ backend :
34
+
35
+ volumes :
36
+ pgdata:
Original file line number Diff line number Diff line change 12
12
13
13
@ Service
14
14
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 ();
16
17
17
18
public FileStorageService () throws IOException {
18
19
Files .createDirectories (this .fileStorageLocation );
19
20
}
20
21
21
22
public String storeFile (MultipartFile file ) throws IOException {
23
+ System .out .println ("path: " + this .fileStorageLocation );
22
24
String fileName = StringUtils .cleanPath (file .getOriginalFilename ());
23
25
24
26
if (fileName .contains (".." )) {
@@ -30,6 +32,6 @@ public String storeFile(MultipartFile file) throws IOException {
30
32
Path targetLocation = this .fileStorageLocation .resolve (newFileName );
31
33
Files .copy (file .getInputStream (), targetLocation , StandardCopyOption .REPLACE_EXISTING );
32
34
33
- return "http://localhost:8080/uploads/ " + newFileName ;
35
+ return "http://localhost:8080/" + newFileName ;
34
36
}
35
37
}
Original file line number Diff line number Diff line change 8
8
import org .springframework .security .config .annotation .authentication .configuration .AuthenticationConfiguration ;
9
9
import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
10
10
import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
11
+ import org .springframework .security .config .annotation .web .configuration .WebSecurityCustomizer ;
11
12
import org .springframework .security .config .http .SessionCreationPolicy ;
12
13
import org .springframework .security .crypto .bcrypt .BCryptPasswordEncoder ;
13
14
import org .springframework .security .crypto .password .PasswordEncoder ;
@@ -27,11 +28,20 @@ public class SecurityConfig {
27
28
"/actuator/**" ,
28
29
};
29
30
31
+ @ Bean
32
+ public WebSecurityCustomizer webSecurityCustomizer () {
33
+ return (web -> web .ignoring ()
34
+ .requestMatchers ("/**.jpg" , "/**.png" , "/**.jpeg" )
35
+ .requestMatchers ("/public/**" )
36
+ );
37
+ }
38
+
30
39
@ Bean
31
40
SecurityFilterChain securityFilterChain (HttpSecurity http ) throws Exception {
32
41
return http .csrf (csrf -> csrf .disable ())
33
42
.sessionManagement (session -> session .sessionCreationPolicy (SessionCreationPolicy .STATELESS ))
34
43
.authorizeHttpRequests (auth -> auth
44
+ .requestMatchers ("/public/**" ).permitAll ()
35
45
.requestMatchers (HttpMethod .POST , "/signin/" ).permitAll ()
36
46
.requestMatchers (HttpMethod .POST , "/signup/" ).permitAll ()
37
47
.requestMatchers (HttpMethod .PUT , "/reset_password/" ).permitAll ()
Original file line number Diff line number Diff line change @@ -74,6 +74,10 @@ private Optional<String> getToken(HttpServletRequest request) {
74
74
protected boolean shouldNotFilter (HttpServletRequest request ) throws ServletException {
75
75
String path = request .getServletPath ();
76
76
77
+ if (path .endsWith (".jpg" )) return true ;
78
+ if (path .endsWith (".jpeg" )) return true ;
79
+ if (path .endsWith (".png" )) return true ;
80
+
77
81
if (path .equals ("/signin/" )) return true ;
78
82
79
83
if (path .equals ("/signup/" )) return true ;
Original file line number Diff line number Diff line change 4
4
import org .springframework .context .annotation .Configuration ;
5
5
import org .springframework .context .annotation .Bean ;
6
6
import org .springframework .web .servlet .config .annotation .CorsRegistry ;
7
+ import org .springframework .web .servlet .config .annotation .ResourceHandlerRegistry ;
7
8
import org .springframework .web .servlet .config .annotation .WebMvcConfigurer ;
8
9
9
10
@ Configuration
10
- public class WebConfig {
11
+ public class WebConfig implements WebMvcConfigurer {
11
12
@ Value ("${SPRING_PROFILES_ACTIVE:prod}" )
12
13
private String env ;
13
14
@@ -23,5 +24,11 @@ public void addCorsMappings(CorsRegistry registry) {
23
24
}
24
25
};
25
26
}
27
+
28
+ @ Override
29
+ public void addResourceHandlers (ResourceHandlerRegistry registry ) {
30
+ registry .addResourceHandler ("/uploads/**" )
31
+ .addResourceLocations ("file:" + "/app/uploads" + "/" );
32
+ }
26
33
}
27
34
You can’t perform that action at this time.
0 commit comments