Skip to content

Commit 8f880e8

Browse files
Konstantin PankratovKonstantin Pankratov
authored andcommitted
Exclude swagger and h2 console from required authorization. Provide a dev profile with disabled security. Provide a starter script.
1 parent 7fb2bad commit 8f880e8

File tree

6 files changed

+103
-49
lines changed

6 files changed

+103
-49
lines changed

docker-compose.yml

Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
1-
networks:
2-
virt_container_net:
3-
driver: bridge
4-
enable_ipv6: true
5-
61
services:
7-
keycloak_web:
2+
keycloak_web1:
83
image: keycloak/keycloak:latest
9-
container_name: kc-web
4+
container_name: kc-web1
105
environment:
116
KC_DB: postgres
12-
KC_DB_URL: jdbc:postgresql://postgres:5432/keycloak
7+
KC_DB_URL: jdbc:postgresql://keycloakdb1:5432/keycloak
138
KC_DB_USERNAME: ${KC_DB_USERNAME}
149
KC_DB_PASSWORD: ${KC_DB_PASSWORD}
1510

1611
KEYCLOAK_ADMIN: ${KC_DB_USERNAME}
17-
KEYCLOAK_ADMIN_PASSWORD: ${KC_DB_PASSWORD}
12+
KEYCLOAK_ADMIN_PASSWORD: pass
13+
1814

1915
KC_HOSTNAME: localhost
20-
KC_HOSTNAME_PORT: 1314
16+
KC_HOSTNAME_PORT: 8080
2117
KC_HOSTNAME_STRICT: 'false'
2218
KC_HOSTNAME_STRICT_HTTPS: 'false'
2319
KC_LOG_LEVEL: debug
@@ -26,48 +22,32 @@ services:
2622
KC_HEALTH_ENABLED: 'true'
2723
KC_PROXY: edge
2824
KC_PROXY_HEADERS: forwarded
29-
KC_HTTP_RELATIVE_PATH: "/auth"
3025
command: start-dev
3126
depends_on:
32-
- postgres
27+
- keycloakdb1
3328
ports:
34-
- '7070:1314'
35-
networks:
36-
- virt_container_net
37-
######################################################
38-
postgres:
29+
- '7070:8080'
30+
######################################################
31+
keycloakdb1:
3932
image: postgres:15
4033
volumes:
41-
- ./postgres_data:/var/lib/postgresql/data
42-
- ./init-db:/docker-entrypoint-initdb.d
34+
- postgres_data:/var/lib/postgresql/data
4335
environment:
4436
POSTGRES_DB: keycloak
4537
POSTGRES_USER: ${KC_DB_USERNAME}
4638
POSTGRES_PASSWORD: ${KC_DB_PASSWORD}
47-
networks:
48-
- virt_container_net
49-
######################################################
50-
spring:
51-
build:
52-
context: .
53-
dockerfile: Dockerfile
54-
# image: panderu/study-buddies-backend:latest
55-
container_name: spring-backend
56-
ports:
57-
- "1516:8080"
58-
depends_on:
59-
- keycloak_web
60-
networks:
61-
- virt_container_net
62-
######################################################
63-
nginx:
64-
image: nginx:latest
65-
network_mode: host
6639
ports:
67-
- 80:80
68-
- 443:443
69-
restart: always
70-
depends_on:
71-
- spring
72-
volumes:
73-
- ./nginx/conf/:/etc/nginx/conf.d/:ro
40+
- '5432:5432'
41+
#spring:
42+
# build:
43+
# context: .
44+
# dockerfile: Dockerfile
45+
# # image: panderu/study-buddies-backend:latest
46+
# container_name: spring-backend
47+
# ports:
48+
# - "1516:8080"
49+
# depends_on:
50+
# - keycloak_web
51+
######################################################
52+
volumes:
53+
postgres_data:
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.studybuddies.server.configuration;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.context.annotation.Profile;
5+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
6+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
7+
import org.springframework.security.config.annotation.web.configurers.CorsConfigurer;
8+
import org.springframework.security.config.annotation.web.configurers.CsrfConfigurer;
9+
import org.springframework.security.web.SecurityFilterChain;
10+
11+
@Profile("dev")
12+
@Configuration
13+
@EnableWebSecurity
14+
public class DevConfig {
15+
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
16+
http.authorizeHttpRequests(auth -> auth.anyRequest().permitAll());
17+
http.csrf(CsrfConfigurer::disable);
18+
http.cors(CorsConfigurer::disable);
19+
return http.build();
20+
}
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.studybuddies.server.configuration;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.boot.context.event.ApplicationReadyEvent;
5+
import org.springframework.context.ApplicationListener;
6+
import org.springframework.context.annotation.Profile;
7+
import org.springframework.stereotype.Component;
8+
9+
@Component
10+
@Slf4j
11+
@Profile("dev")
12+
public class DevProfileListener implements ApplicationListener<ApplicationReadyEvent> {
13+
14+
@Override
15+
public void onApplicationEvent(ApplicationReadyEvent e) {
16+
log.warn("WARNING: Dev profile is active. All authorization mechanisms will be disabled.");
17+
}
18+
}
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.studybuddies.server.configuration;
22

3-
import org.springframework.beans.factory.annotation.Autowired;
3+
import lombok.RequiredArgsConstructor;
44
import org.springframework.context.annotation.Bean;
55
import org.springframework.context.annotation.Configuration;
66
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
@@ -9,16 +9,22 @@
99

1010
@Configuration
1111
@EnableMethodSecurity
12+
@RequiredArgsConstructor
1213
public class SecurityConfig {
1314

14-
@Autowired
1515
JwtAuthConverter jwtAuthConverter;
1616

1717
@Bean
1818
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
1919
http.
20-
authorizeHttpRequests(auth -> auth.anyRequest().authenticated())
21-
.oauth2ResourceServer(oauth2 -> oauth2.jwt(jwt -> jwt.jwtAuthenticationConverter(jwtAuthConverter)));
20+
authorizeHttpRequests(auth ->
21+
auth.requestMatchers("/swagger-ui/**", "/h2-console/**", "/api-docs/**", "/v3/**").permitAll()
22+
)
23+
.oauth2ResourceServer(oauth2 ->
24+
oauth2.jwt(jwt ->
25+
jwt.jwtAuthenticationConverter(jwtAuthConverter)
26+
)
27+
);
2228
return http.build();
2329
}
2430
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
server:
2+
port: 8080
3+
spring:
4+
application:
5+
name: server
6+
datasource:
7+
url: jdbc:h2:file:./data/demo
8+
username: sa
9+
password: password
10+
driverClassName: org.h2.Driver
11+
jpa:
12+
database-platform: org.hibernate.dialect.H2Dialect
13+
show-sql: true
14+
hibernate:
15+
ddl-auto: update
16+
h2:
17+
console.enabled: true
18+
springdoc:
19+
swagger-ui.path: /api-docs

server/starter

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
if [ -z "$1" ]; then
4+
echo "No profile provided. Please enter the profile to use:"
5+
read profile
6+
else
7+
profile=$1
8+
fi
9+
10+
mvn spring-boot:run -Dspring-boot.run.profiles=$profile

0 commit comments

Comments
 (0)