SPRING SECURITY / 01

Setup and startup

From a project copy to your first authenticated request.

Full startup with Docker

The project has one compose.yml at the root. It builds and starts MySQL, backend and frontend with Nginx. You need only Docker with Compose and a prepared .env file.

shell
# Desde la raíz del proyecto, con Docker abierto.
# Conserva el .env existente; para una instalación nueva:
if [ ! -f .env ]; then cp .env.example .env; fi

# Configura JWT_SECRET y las contraseñas en .env antes de arrancar.
docker compose up -d --build --wait
docker compose ps

Open http://localhost:8088. All three services must show status healthy. You need not start Maven or pnpm in another terminal: the full application runs in Docker.

ServiceAddressAccess
Frontend / Nginxhttp://localhost:8088Browser; also proxies /api to the backend.
Backend / Spring Bootbackend:7070Docker-internal; public API through http://localhost:8088/api.
MySQLmysql:3306Docker-internal; no published host port.
shell
# Consultar logs.
docker compose logs --tail=100 backend frontend mysql

# Reconstruir después de cambiar código o configuración.
docker compose up -d --build --wait

# Detener los servicios conservando la base de datos.
docker compose down

MySQL persists in security-lab-full_mysql_data. Detailed instructions for variables, initial accounts, backups and server deployment are in the Docker guide. The following sections are the alternative for development with Maven and pnpm on the host.

1. Local development requirements

Install Java 21, Maven 3.9+, Node 20.19+ or 22.12+, pnpm 10 and Docker with Compose v2. Java uses Maven; all JavaScript package operations use pnpm. Run blocks from the root unless another directory is specified.

shell
java -version
mvn -version
node --version
pnpm --version
docker compose version

On macOS, if Maven uses another JDK:

shell
export JAVA_HOME=$(/usr/libexec/java_home -v 21)

2. Prepare .env without losing configuration

If .envalready exists, preserve it. For a new installation:

shell
# Desde la raíz: copia solo si todavía no existe.
if [ ! -f .env ]; then cp .env.example .env; fi
openssl rand -hex 32

OpenSSL generates 32 random bytes represented as 64 hexadecimal characters. Copy the complete result into JWT_SECRET inside .env. Java uses that text's UTF-8 bytes: this application does not decode it as hexadecimal or Base64. No RSA key pair, certificate or PEM file is needed.

dotenv
# Fragmento ilustrativo: sustituye los marcadores antes de arrancar.
DB_PASSWORD=TU_PASSWORD_MYSQL
MYSQL_ROOT_PASSWORD=OTRA_PASSWORD_PARA_ROOT
JWT_SECRET=PEGA_AQUI_EL_RESULTADO_DE_OPENSSL
JWT_EXPIRATION_SECONDS=900
DEMO_ENABLED=true
DEMO_ADMIN_PASSWORD=TU_PASSWORD_ADMIN_DE_AL_MENOS_8_CARACTERES
DEMO_USER_PASSWORD=TU_PASSWORD_USER_DE_AL_MENOS_8_CARACTERES

Which key you are generating

HS256 is symmetric signing: the same secret signs and verifies JWTs. BCrypt automatically generates password salts; it does not use JWT_SECRET. Changing the JWT key and restarting backend invalidates tokens signed with the previous key. Preserve the same secure key across restarts to keep unexpired tokens valid.

Variables and effective values

VariableValue / exampleUsage
DB_URLjdbc:mysql://localhost:3307/security_lab?serverTimezone=UTCLocal JDBC connection. Docker uses mysql:3306.
DB_USERsecurity_appApplication user; does not use root.
DB_PASSWORDSet your own valueJDBC password and initial MySQL user password.
MYSQL_ROOT_PASSWORDSet a different valueMySQL administrative password, container-only.
JWT_SECRETGenerate with OpenSSLHS256 secret of at least 32 UTF-8 bytes. No default in Java.
JWT_EXPIRATION_SECONDS900Positive duration in seconds; 900 equals 15 minutes.
FRONTEND_URLhttp://localhost:5173,http://127.0.0.1:5173Exact CORS origins separated by commas, without paths.
DEMO_ENABLEDfalse in .env.example and by default in SpringEnables initial creation of the two demo accounts.
DEMO_ADMIN_PASSWORD / DEMO_USER_PASSWORDSet for demo modeAt least 8 characters. Used only when creating nonexistent accounts.
VITE_API_URL/api if undefinedBuild-time frontend variable; never contains secrets.

Spring imports ../.env and .env as properties files through spring.config.import. This works when running Maven from backend/. Environment variables can override that configuration. JWT_EXPIRATION is not a variable used by this project.

3. Prepare MySQL for local development

To use Maven and Vite on the host, prepare an accessible MySQL 8.4 instance, create database security_lab and a user with permissions on it. Set its JDBC URL and credentials in .env DB_URL, DB_USER and DB_PASSWORD. The example URL uses localhost:3307; adapt it to your instance.

The single compose.yml deploys the full application and keeps MySQL private on its network. To use this mode, run docker compose up -d --build --wait and access localhost:8088 without starting Maven or Vite.

4. Start Spring

shell
# Terminal 1, desde la raíz
cd backend
mvn spring-boot:run

Spring reads the root .env and connects directly to the database configured by DB_URL, DB_USER and DB_PASSWORD. Maven does not start Docker. The API listens at http://localhost:7070. Flyway creates the schema before Hibernate validates it. HikariCP maintains between one and five connections.

With DEMO_ENABLED=true, DemoData creates admin@example.com with ADMIN and user@example.com with USER if those emails do not exist. Use your configured passwords. Changing demo variables changes neither passwords nor roles of existing accounts. Example configuration contains known passwords: replace them for your installation.

5. Start React

shell
# Terminal 2, desde la raíz
cd frontend
pnpm install --frozen-lockfile
pnpm dev

Open localhost:5173. Vite listens on 127.0.0.1:5173 and redirects /api to Spring on 7070. Sign in with a demo account or register. If you created frontend/.env.local, check that VITE_API_URL matches this environment.

6. Check and stop

shell
# Desde la raíz: sin token debe responder 401; confirma que la API contesta.
curl -i http://localhost:7070/api/me

# Pruebas de integración, desde backend/
mvn test

# Build del frontend, desde frontend/
pnpm build

Tests use H2 in MySQL mode and real JWTs with MockMvc; they require neither MySQL nor Docker. They check credentials, validation, permissions, role changes, disabling, deletion and CORS. H2 does not test every difference in real MySQL.

Stop React and Spring with Ctrl+C in each terminal. To stop full deployment from the root, docker compose stop stops the three services; docker compose down removes containers and network while preserving the volume unless deletion options are added.

Build without containers

shell
# Desde backend/
mvn clean verify
java -jar target/security-lab-1.0.0.jar

# En otra terminal, desde frontend/
pnpm build
pnpm preview --port 5173

Vite's /api proxy is configured only for development. To preview with local Spring, set VITE_API_URL=http://localhost:7070/api in frontend/.env.local and rebuild before preview. Preview checks the build; deployment uses a static server and proxy such as the Docker example. VITE_* variables are included in the build and visible in the browser: official Vite documentation.

CV & portfolio · ES / EN
EspañolEnglish