2
.gitattributes
vendido
Archivo normal
2
.gitattributes
vendido
Archivo normal
@@ -0,0 +1,2 @@
|
||||
/mvnw text eol=lf
|
||||
*.cmd text eol=crlf
|
||||
33
.gitignore
vendido
Archivo normal
33
.gitignore
vendido
Archivo normal
@@ -0,0 +1,33 @@
|
||||
HELP.md
|
||||
target/
|
||||
.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**/target/
|
||||
!**/src/test/**/target/
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
build/
|
||||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
3
.mvn/wrapper/maven-wrapper.properties
vendido
Archivo normal
3
.mvn/wrapper/maven-wrapper.properties
vendido
Archivo normal
@@ -0,0 +1,3 @@
|
||||
wrapperVersion=3.3.4
|
||||
distributionType=only-script
|
||||
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.12/apache-maven-3.9.12-bin.zip
|
||||
259
IMPLEMENTATION_SUMMARY.md
Archivo normal
259
IMPLEMENTATION_SUMMARY.md
Archivo normal
@@ -0,0 +1,259 @@
|
||||
# API Sessions - Implementation Summary
|
||||
|
||||
## ✅ Project Successfully Implemented
|
||||
|
||||
A complete CRUD REST API with JWT authentication has been successfully implemented and tested.
|
||||
|
||||
## 🎯 Implemented Features
|
||||
|
||||
### Backend Components
|
||||
|
||||
1. **Entities**
|
||||
- ✅ User entity with authentication fields
|
||||
- ✅ Product entity for CRUD operations
|
||||
- ✅ JPA annotations and automatic timestamps
|
||||
|
||||
2. **Repositories**
|
||||
- ✅ UserRepository with custom queries
|
||||
- ✅ ProductRepository with search capabilities
|
||||
|
||||
3. **Security Layer**
|
||||
- ✅ JWT token generation and validation (JJWT 0.12.6)
|
||||
- ✅ JwtRequestFilter for request interception
|
||||
- ✅ Spring Security integration
|
||||
- ✅ BCrypt password encryption
|
||||
- ✅ Stateless session management
|
||||
|
||||
4. **Services**
|
||||
- ✅ CustomUserDetailsService for authentication
|
||||
- ✅ ProductService with full CRUD operations
|
||||
|
||||
5. **REST Controllers**
|
||||
- ✅ AuthController (login, status check)
|
||||
- ✅ ProductController (full CRUD + search + filter)
|
||||
|
||||
6. **Database**
|
||||
- ✅ H2 in-memory database
|
||||
- ✅ Automatic schema generation
|
||||
- ✅ Data initialization on startup
|
||||
- ✅ 3 users (admin, user, john)
|
||||
- ✅ 8 sample products
|
||||
|
||||
### Testing Infrastructure
|
||||
|
||||
1. **Test Scripts**
|
||||
- ✅ `test-api.sh` - Comprehensive test suite (14 tests)
|
||||
- ✅ `quick-test.sh` - Quick validation script
|
||||
- ✅ Colored output for better readability
|
||||
- ✅ JSON formatting
|
||||
|
||||
2. **Documentation**
|
||||
- ✅ `README.md` - Complete project documentation
|
||||
- ✅ API endpoint documentation
|
||||
- ✅ Setup instructions
|
||||
- ✅ Usage examples
|
||||
|
||||
## 🧪 Test Results
|
||||
|
||||
All 14 tests passed successfully:
|
||||
|
||||
### Authentication Tests
|
||||
- ✅ **TEST 1**: Login and JWT token generation
|
||||
- ✅ **TEST 2**: Authentication status check
|
||||
- ✅ **TEST 14**: Unauthorized access prevention
|
||||
|
||||
### READ Operations
|
||||
- ✅ **TEST 3**: Get all products
|
||||
- ✅ **TEST 4**: Get product by ID
|
||||
- ✅ **TEST 5**: Search products by name
|
||||
- ✅ **TEST 6**: Filter products by category
|
||||
|
||||
### CREATE Operations
|
||||
- ✅ **TEST 7**: Create new product (Gaming Console PlayStation 5)
|
||||
|
||||
### UPDATE Operations
|
||||
- ✅ **TEST 8**: Update existing product (Laptop Dell XPS 15)
|
||||
- ✅ **TEST 9**: Verify product update
|
||||
|
||||
### DELETE Operations
|
||||
- ✅ **TEST 11**: Delete product
|
||||
- ✅ **TEST 12**: Verify deletion (404 response)
|
||||
|
||||
### Data Verification
|
||||
- ✅ **TEST 10**: Verify product list after creation
|
||||
- ✅ **TEST 13**: Verify final product list after deletion
|
||||
|
||||
## 📊 Initial Data
|
||||
|
||||
### Users
|
||||
| Username | Password | Role | Email |
|
||||
|----------|-----------|------------|--------------------|
|
||||
| admin | admin123 | ROLE_ADMIN | admin@example.com |
|
||||
| user | user123 | ROLE_USER | user@example.com |
|
||||
| john | john123 | ROLE_USER | john@example.com |
|
||||
|
||||
### Products (8 items initialized)
|
||||
1. Laptop Dell XPS 15 - $1,299.99
|
||||
2. iPhone 15 Pro - $999.99
|
||||
3. Wireless Mouse Logitech MX Master 3 - $99.99
|
||||
4. Mechanical Keyboard - $149.99
|
||||
5. 27-inch 4K Monitor - $449.99
|
||||
6. USB-C Hub - $49.99
|
||||
7. Wireless Headphones Sony WH-1000XM5 - $349.99
|
||||
8. Webcam Logitech C920 - $79.99
|
||||
|
||||
## 🔧 Technology Stack
|
||||
|
||||
- **Spring Boot**: 4.1.0-M1
|
||||
- **Java**: 21
|
||||
- **Spring Security**: JWT-based authentication
|
||||
- **JWT Library**: JJWT 0.12.6
|
||||
- **Database**: H2 (in-memory)
|
||||
- **ORM**: Spring Data JPA / Hibernate
|
||||
- **Build Tool**: Maven
|
||||
- **Utilities**: Lombok
|
||||
|
||||
## 📝 API Endpoints
|
||||
|
||||
### Authentication
|
||||
- `POST /api/auth/login` - User login
|
||||
- `GET /api/auth/status` - Check authentication status
|
||||
|
||||
### Products (All require JWT token)
|
||||
- `GET /api/products` - Get all products
|
||||
- `GET /api/products/{id}` - Get product by ID
|
||||
- `GET /api/products/search?name={name}` - Search products
|
||||
- `GET /api/products/category/{category}` - Filter by category
|
||||
- `POST /api/products` - Create new product
|
||||
- `PUT /api/products/{id}` - Update product
|
||||
- `DELETE /api/products/{id}` - Delete product
|
||||
|
||||
### Console Access
|
||||
- `GET /h2-console` - H2 database console (no authentication required)
|
||||
|
||||
## 🚀 How to Run
|
||||
|
||||
1. **Start the application:**
|
||||
```bash
|
||||
./mvnw spring-boot:run
|
||||
```
|
||||
|
||||
2. **Run comprehensive tests:**
|
||||
```bash
|
||||
./test-api.sh
|
||||
```
|
||||
|
||||
3. **Run quick tests:**
|
||||
```bash
|
||||
./quick-test.sh
|
||||
```
|
||||
|
||||
4. **Access H2 Console:**
|
||||
- URL: http://localhost:8080/h2-console
|
||||
- JDBC URL: jdbc:h2:mem:testdb
|
||||
- Username: sa
|
||||
- Password: (empty)
|
||||
|
||||
## 🔒 Security Features
|
||||
|
||||
- ✅ JWT token-based authentication
|
||||
- ✅ Stateless session management
|
||||
- ✅ Password encryption with BCrypt
|
||||
- ✅ Token validation on each request
|
||||
- ✅ Protected endpoints (403 without token)
|
||||
- ✅ Token expiration (24 hours)
|
||||
- ✅ CSRF protection disabled for REST API
|
||||
|
||||
## 📦 Project Structure
|
||||
|
||||
```
|
||||
src/main/java/com/manalejandro/api_sessions/
|
||||
├── config/
|
||||
│ ├── SecurityConfig.java # Spring Security configuration
|
||||
│ └── DataInitializer.java # Database initialization
|
||||
├── controller/
|
||||
│ ├── AuthController.java # Authentication endpoints
|
||||
│ └── ProductController.java # Product CRUD endpoints
|
||||
├── dto/
|
||||
│ ├── AuthRequest.java # Login request DTO
|
||||
│ ├── AuthResponse.java # Login response DTO
|
||||
│ └── ProductDto.java # Product DTO
|
||||
├── entity/
|
||||
│ ├── User.java # User entity
|
||||
│ └── Product.java # Product entity
|
||||
├── repository/
|
||||
│ ├── UserRepository.java # User data access
|
||||
│ └── ProductRepository.java # Product data access
|
||||
├── security/
|
||||
│ ├── JwtTokenUtil.java # JWT utility functions
|
||||
│ └── JwtRequestFilter.java # JWT filter for requests
|
||||
└── service/
|
||||
├── CustomUserDetailsService.java # User authentication service
|
||||
└── ProductService.java # Product business logic
|
||||
```
|
||||
|
||||
## ✨ Key Implementation Highlights
|
||||
|
||||
1. **JWT Integration with Spring Security**
|
||||
- JWT tokens are validated and integrated with Spring Security's authentication context
|
||||
- Each request with a valid token sets up the SecurityContext
|
||||
- Seamless integration between JWT and Spring Security sessions
|
||||
|
||||
2. **Comprehensive CRUD Operations**
|
||||
- Full Create, Read, Update, Delete functionality
|
||||
- Additional search and filter capabilities
|
||||
- Proper HTTP status codes (200, 201, 404, 403)
|
||||
|
||||
3. **Database Initialization**
|
||||
- Automatic database setup on application start
|
||||
- Pre-populated with test data
|
||||
- Password encryption for all users
|
||||
|
||||
4. **Test Automation**
|
||||
- Shell scripts for complete API testing
|
||||
- Colored output for easy result visualization
|
||||
- JSON formatting for readable responses
|
||||
- All CRUD operations validated
|
||||
|
||||
5. **Production-Ready Code**
|
||||
- Comprehensive documentation
|
||||
- Proper error handling
|
||||
- Clean code structure
|
||||
- Following Spring Boot best practices
|
||||
|
||||
## 🎓 Learning Points
|
||||
|
||||
This implementation demonstrates:
|
||||
- Modern Spring Boot REST API development
|
||||
- JWT authentication and authorization
|
||||
- Spring Security configuration
|
||||
- JPA/Hibernate entity relationships
|
||||
- Repository pattern
|
||||
- Service layer architecture
|
||||
- DTO pattern for data transfer
|
||||
- Shell script automation
|
||||
- API testing strategies
|
||||
|
||||
## 📌 Notes
|
||||
|
||||
- All code is in English (including comments and documentation)
|
||||
- The application uses an in-memory H2 database (data resets on restart)
|
||||
- JWT secret key should be changed for production use
|
||||
- Token expiration is set to 24 hours
|
||||
- CORS is not configured (add if needed for frontend integration)
|
||||
|
||||
## ✅ Implementation Complete
|
||||
|
||||
The project is fully functional and ready for use. All requirements have been met:
|
||||
- ✅ Complete CRUD API implementation
|
||||
- ✅ JWT authentication integrated with Spring Security
|
||||
- ✅ H2 database with automatic initialization
|
||||
- ✅ Test scripts with all CRUD operations
|
||||
- ✅ Comprehensive English documentation
|
||||
- ✅ All tests passing
|
||||
|
||||
---
|
||||
|
||||
**Status**: ✅ READY FOR USE
|
||||
**Last Updated**: 2026-01-23
|
||||
**Version**: 1.0.0
|
||||
255
QUICK_REFERENCE.md
Archivo normal
255
QUICK_REFERENCE.md
Archivo normal
@@ -0,0 +1,255 @@
|
||||
# API Sessions - Quick Reference
|
||||
|
||||
## 🚀 Quick Start
|
||||
|
||||
```bash
|
||||
# Start the application
|
||||
./mvnw spring-boot:run
|
||||
|
||||
# Or use management script
|
||||
./manage.sh start
|
||||
|
||||
# Run tests
|
||||
./test-api.sh
|
||||
```
|
||||
|
||||
## 🔑 Default Credentials
|
||||
|
||||
| Username | Password | Role |
|
||||
|----------|-----------|------------|
|
||||
| admin | admin123 | ROLE_ADMIN |
|
||||
| user | user123 | ROLE_USER |
|
||||
| john | john123 | ROLE_USER |
|
||||
|
||||
## 📡 API Endpoints
|
||||
|
||||
### Authentication (No token required)
|
||||
```bash
|
||||
# Login
|
||||
curl -X POST http://localhost:8080/api/auth/login \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"username":"admin","password":"admin123"}'
|
||||
```
|
||||
|
||||
### Products (Token required)
|
||||
|
||||
#### Get all products
|
||||
```bash
|
||||
curl -X GET http://localhost:8080/api/products \
|
||||
-H "Authorization: Bearer YOUR_TOKEN"
|
||||
```
|
||||
|
||||
#### Get product by ID
|
||||
```bash
|
||||
curl -X GET http://localhost:8080/api/products/1 \
|
||||
-H "Authorization: Bearer YOUR_TOKEN"
|
||||
```
|
||||
|
||||
#### Search products
|
||||
```bash
|
||||
curl -X GET "http://localhost:8080/api/products/search?name=laptop" \
|
||||
-H "Authorization: Bearer YOUR_TOKEN"
|
||||
```
|
||||
|
||||
#### Filter by category
|
||||
```bash
|
||||
curl -X GET http://localhost:8080/api/products/category/Electronics \
|
||||
-H "Authorization: Bearer YOUR_TOKEN"
|
||||
```
|
||||
|
||||
#### Create product
|
||||
```bash
|
||||
curl -X POST http://localhost:8080/api/products \
|
||||
-H "Authorization: Bearer YOUR_TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{
|
||||
"name": "New Product",
|
||||
"description": "Description",
|
||||
"price": 99.99,
|
||||
"stock": 10,
|
||||
"category": "Electronics"
|
||||
}'
|
||||
```
|
||||
|
||||
#### Update product
|
||||
```bash
|
||||
curl -X PUT http://localhost:8080/api/products/1 \
|
||||
-H "Authorization: Bearer YOUR_TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{
|
||||
"name": "Updated Product",
|
||||
"description": "Updated description",
|
||||
"price": 149.99,
|
||||
"stock": 15,
|
||||
"category": "Electronics"
|
||||
}'
|
||||
```
|
||||
|
||||
#### Delete product
|
||||
```bash
|
||||
curl -X DELETE http://localhost:8080/api/products/1 \
|
||||
-H "Authorization: Bearer YOUR_TOKEN"
|
||||
```
|
||||
|
||||
## 🛠️ Management Commands
|
||||
|
||||
```bash
|
||||
./manage.sh start # Start application
|
||||
./manage.sh stop # Stop application
|
||||
./manage.sh restart # Restart application
|
||||
./manage.sh test # Run full tests
|
||||
./manage.sh quick-test # Run quick tests
|
||||
./manage.sh status # Check status
|
||||
./manage.sh logs # View logs
|
||||
./manage.sh console # Show H2 console info
|
||||
./manage.sh help # Show all commands
|
||||
```
|
||||
|
||||
## 🗄️ H2 Console
|
||||
|
||||
- **URL**: http://localhost:8080/h2-console
|
||||
- **JDBC URL**: jdbc:h2:mem:testdb
|
||||
- **Username**: sa
|
||||
- **Password**: (empty)
|
||||
|
||||
## 📝 Complete Workflow Example
|
||||
|
||||
```bash
|
||||
# 1. Start the application
|
||||
./manage.sh start
|
||||
|
||||
# 2. Wait for startup (check status)
|
||||
./manage.sh status
|
||||
|
||||
# 3. Login and save token
|
||||
TOKEN=$(curl -s -X POST http://localhost:8080/api/auth/login \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"username":"admin","password":"admin123"}' | \
|
||||
grep -o '"token":"[^"]*' | cut -d'"' -f4)
|
||||
|
||||
# 4. Use the token to access API
|
||||
curl -X GET http://localhost:8080/api/products \
|
||||
-H "Authorization: Bearer $TOKEN"
|
||||
|
||||
# 5. Create a product
|
||||
curl -X POST http://localhost:8080/api/products \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{
|
||||
"name": "Test Product",
|
||||
"description": "Test Description",
|
||||
"price": 99.99,
|
||||
"stock": 10,
|
||||
"category": "Test"
|
||||
}'
|
||||
|
||||
# 6. View all products
|
||||
curl -X GET http://localhost:8080/api/products \
|
||||
-H "Authorization: Bearer $TOKEN" | python3 -m json.tool
|
||||
```
|
||||
|
||||
## 🧪 Testing
|
||||
|
||||
```bash
|
||||
# Run comprehensive tests (14 tests)
|
||||
./test-api.sh
|
||||
|
||||
# Run quick validation
|
||||
./quick-test.sh
|
||||
|
||||
# Manual test - get token
|
||||
./manage.sh start
|
||||
sleep 10
|
||||
curl -X POST http://localhost:8080/api/auth/login \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"username":"admin","password":"admin123"}'
|
||||
```
|
||||
|
||||
## 📊 Project Structure
|
||||
|
||||
```
|
||||
api-sessions/
|
||||
├── src/main/java/ # Source code
|
||||
├── src/main/resources/ # Configuration
|
||||
├── test-api.sh # Comprehensive tests
|
||||
├── quick-test.sh # Quick tests
|
||||
├── manage.sh # Management script
|
||||
├── README.md # Full documentation
|
||||
├── IMPLEMENTATION_SUMMARY.md # Implementation details
|
||||
└── QUICK_REFERENCE.md # This file
|
||||
```
|
||||
|
||||
## 🔍 Troubleshooting
|
||||
|
||||
### Application won't start
|
||||
```bash
|
||||
# Check if port 8080 is in use
|
||||
lsof -i :8080
|
||||
|
||||
# Clean and rebuild
|
||||
./manage.sh clean
|
||||
./manage.sh build
|
||||
./manage.sh start
|
||||
```
|
||||
|
||||
### Tests failing
|
||||
```bash
|
||||
# Check application status
|
||||
./manage.sh status
|
||||
|
||||
# Restart application
|
||||
./manage.sh restart
|
||||
sleep 10
|
||||
|
||||
# Try tests again
|
||||
./test-api.sh
|
||||
```
|
||||
|
||||
### Can't get token
|
||||
```bash
|
||||
# Check credentials (case-sensitive)
|
||||
# Username: admin
|
||||
# Password: admin123
|
||||
|
||||
# Verify endpoint is accessible
|
||||
curl -X POST http://localhost:8080/api/auth/login \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"username":"admin","password":"admin123"}'
|
||||
```
|
||||
|
||||
## 📚 Additional Resources
|
||||
|
||||
- Full documentation: `README.md`
|
||||
- Implementation details: `IMPLEMENTATION_SUMMARY.md`
|
||||
- Application logs: `app.log` (when using manage.sh)
|
||||
- H2 Console: http://localhost:8080/h2-console
|
||||
|
||||
## ⚡ Pro Tips
|
||||
|
||||
1. **Save token to variable**: Makes testing easier
|
||||
```bash
|
||||
TOKEN=$(curl -s ... | grep -o '"token":"[^"]*' | cut -d'"' -f4)
|
||||
```
|
||||
|
||||
2. **Pretty print JSON**: Use python3 json.tool
|
||||
```bash
|
||||
curl ... | python3 -m json.tool
|
||||
```
|
||||
|
||||
3. **Check logs in real-time**:
|
||||
```bash
|
||||
./manage.sh logs
|
||||
```
|
||||
|
||||
4. **Quick product count**:
|
||||
```bash
|
||||
curl -s http://localhost:8080/api/products \
|
||||
-H "Authorization: Bearer $TOKEN" | \
|
||||
python3 -c "import sys, json; print(len(json.load(sys.stdin)))"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
**Version**: 1.0.0
|
||||
**Last Updated**: 2026-01-23
|
||||
**Status**: ✅ Ready for use
|
||||
353
README.md
Archivo normal
353
README.md
Archivo normal
@@ -0,0 +1,353 @@
|
||||
# API Sessions - Spring Boot REST API with JWT Authentication
|
||||
|
||||
A complete CRUD REST API implementation using Spring Boot 4.x with JWT authentication, H2 database, and Spring Security session management.
|
||||
|
||||
## Features
|
||||
|
||||
- **JWT Authentication**: Secure API endpoints with JSON Web Tokens
|
||||
- **Spring Security Integration**: JWT tokens linked to Spring Security sessions
|
||||
- **CRUD Operations**: Full Create, Read, Update, Delete operations for products
|
||||
- **H2 Database**: In-memory database with auto-initialization
|
||||
- **RESTful API**: Clean REST architecture with proper HTTP methods
|
||||
- **Data Initialization**: Sample data loaded on startup for testing
|
||||
- **Comprehensive Testing**: Shell scripts for API testing
|
||||
|
||||
## Technologies Used
|
||||
|
||||
- **Spring Boot 4.1.0-M1** (Java 21)
|
||||
- **Spring Security** - Authentication and authorization
|
||||
- **Spring Data JPA** - Database access
|
||||
- **H2 Database** - In-memory database
|
||||
- **JWT (JJWT 0.12.6)** - Token generation and validation
|
||||
- **Lombok** - Reduce boilerplate code
|
||||
- **Maven** - Dependency management
|
||||
|
||||
## Project Structure
|
||||
|
||||
```
|
||||
src/main/java/com/manalejandro/api_sessions/
|
||||
├── config/
|
||||
│ ├── SecurityConfig.java # Spring Security configuration
|
||||
│ └── DataInitializer.java # Database initialization
|
||||
├── controller/
|
||||
│ ├── AuthController.java # Authentication endpoints
|
||||
│ └── ProductController.java # Product CRUD endpoints
|
||||
├── dto/
|
||||
│ ├── AuthRequest.java # Login request DTO
|
||||
│ ├── AuthResponse.java # Login response DTO
|
||||
│ └── ProductDto.java # Product DTO
|
||||
├── entity/
|
||||
│ ├── User.java # User entity
|
||||
│ └── Product.java # Product entity
|
||||
├── repository/
|
||||
│ ├── UserRepository.java # User data access
|
||||
│ └── ProductRepository.java # Product data access
|
||||
├── security/
|
||||
│ ├── JwtTokenUtil.java # JWT utility functions
|
||||
│ └── JwtRequestFilter.java # JWT filter for requests
|
||||
└── service/
|
||||
├── CustomUserDetailsService.java # User authentication service
|
||||
└── ProductService.java # Product business logic
|
||||
```
|
||||
|
||||
## Getting Started
|
||||
|
||||
### Prerequisites
|
||||
|
||||
- Java 21 or higher
|
||||
- Maven 3.6+
|
||||
- curl (for testing scripts)
|
||||
|
||||
### Installation
|
||||
|
||||
1. Clone the repository:
|
||||
```bash
|
||||
git clone <repository-url>
|
||||
cd api-sessions
|
||||
```
|
||||
|
||||
2. Build the project:
|
||||
```bash
|
||||
./mvnw clean install
|
||||
```
|
||||
|
||||
3. Run the application:
|
||||
```bash
|
||||
./mvnw spring-boot:run
|
||||
```
|
||||
|
||||
The application will start on `http://localhost:8080`
|
||||
|
||||
## Initial Data
|
||||
|
||||
### Users
|
||||
The application initializes with the following users:
|
||||
|
||||
| Username | Password | Role | Email |
|
||||
|----------|-----------|------------|--------------------|
|
||||
| admin | admin123 | ROLE_ADMIN | admin@example.com |
|
||||
| user | user123 | ROLE_USER | user@example.com |
|
||||
| john | john123 | ROLE_USER | john@example.com |
|
||||
|
||||
### Products
|
||||
8 sample products are created in categories: Electronics, Accessories, Audio, and Gaming.
|
||||
|
||||
## API Endpoints
|
||||
|
||||
### Authentication
|
||||
|
||||
#### Login
|
||||
```bash
|
||||
POST /api/auth/login
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"username": "admin",
|
||||
"password": "admin123"
|
||||
}
|
||||
|
||||
Response:
|
||||
{
|
||||
"token": "eyJhbGciOiJIUzI1NiJ9...",
|
||||
"username": "admin",
|
||||
"message": "Authentication successful"
|
||||
}
|
||||
```
|
||||
|
||||
#### Check Status
|
||||
```bash
|
||||
GET /api/auth/status
|
||||
Authorization: Bearer <token>
|
||||
|
||||
Response:
|
||||
{
|
||||
"username": "admin",
|
||||
"message": "Authenticated"
|
||||
}
|
||||
```
|
||||
|
||||
### Products (All require JWT token)
|
||||
|
||||
#### Get All Products
|
||||
```bash
|
||||
GET /api/products
|
||||
Authorization: Bearer <token>
|
||||
```
|
||||
|
||||
#### Get Product by ID
|
||||
```bash
|
||||
GET /api/products/{id}
|
||||
Authorization: Bearer <token>
|
||||
```
|
||||
|
||||
#### Search Products by Name
|
||||
```bash
|
||||
GET /api/products/search?name=laptop
|
||||
Authorization: Bearer <token>
|
||||
```
|
||||
|
||||
#### Get Products by Category
|
||||
```bash
|
||||
GET /api/products/category/{category}
|
||||
Authorization: Bearer <token>
|
||||
```
|
||||
|
||||
#### Create Product
|
||||
```bash
|
||||
POST /api/products
|
||||
Authorization: Bearer <token>
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"name": "New Product",
|
||||
"description": "Product description",
|
||||
"price": 99.99,
|
||||
"stock": 10,
|
||||
"category": "Electronics"
|
||||
}
|
||||
```
|
||||
|
||||
#### Update Product
|
||||
```bash
|
||||
PUT /api/products/{id}
|
||||
Authorization: Bearer <token>
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"name": "Updated Product",
|
||||
"description": "Updated description",
|
||||
"price": 149.99,
|
||||
"stock": 15,
|
||||
"category": "Electronics"
|
||||
}
|
||||
```
|
||||
|
||||
#### Delete Product
|
||||
```bash
|
||||
DELETE /api/products/{id}
|
||||
Authorization: Bearer <token>
|
||||
```
|
||||
|
||||
## Testing Scripts
|
||||
|
||||
### Full Test Suite
|
||||
Run comprehensive tests covering all CRUD operations:
|
||||
```bash
|
||||
chmod +x test-api.sh
|
||||
./test-api.sh
|
||||
```
|
||||
|
||||
This script tests:
|
||||
- ✓ Authentication (Login)
|
||||
- ✓ Authorization (JWT Token)
|
||||
- ✓ CREATE - New product
|
||||
- ✓ READ - All products, by ID, search, by category
|
||||
- ✓ UPDATE - Existing product
|
||||
- ✓ DELETE - Product removal
|
||||
- ✓ Security - Unauthorized access protection
|
||||
|
||||
### Quick Test
|
||||
Run a quick test of basic operations:
|
||||
```bash
|
||||
chmod +x quick-test.sh
|
||||
./quick-test.sh
|
||||
```
|
||||
|
||||
### Manual Testing Examples
|
||||
|
||||
1. **Login and save token:**
|
||||
```bash
|
||||
TOKEN=$(curl -s -X POST http://localhost:8080/api/auth/login \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"username":"admin","password":"admin123"}' | \
|
||||
grep -o '"token":"[^"]*' | cut -d'"' -f4)
|
||||
echo $TOKEN
|
||||
```
|
||||
|
||||
2. **Get all products:**
|
||||
```bash
|
||||
curl -X GET http://localhost:8080/api/products \
|
||||
-H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
3. **Create a product:**
|
||||
```bash
|
||||
curl -X POST http://localhost:8080/api/products \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{
|
||||
"name": "Smart Watch",
|
||||
"description": "Fitness tracker with heart rate monitor",
|
||||
"price": 199.99,
|
||||
"stock": 25,
|
||||
"category": "Wearables"
|
||||
}'
|
||||
```
|
||||
|
||||
4. **Update a product:**
|
||||
```bash
|
||||
curl -X PUT http://localhost:8080/api/products/1 \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{
|
||||
"name": "Updated Product Name",
|
||||
"description": "Updated description",
|
||||
"price": 299.99,
|
||||
"stock": 30,
|
||||
"category": "Electronics"
|
||||
}'
|
||||
```
|
||||
|
||||
5. **Delete a product:**
|
||||
```bash
|
||||
curl -X DELETE http://localhost:8080/api/products/9 \
|
||||
-H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
## H2 Database Console
|
||||
|
||||
Access the H2 console at: `http://localhost:8080/h2-console`
|
||||
|
||||
**Connection settings:**
|
||||
- JDBC URL: `jdbc:h2:mem:testdb`
|
||||
- Username: `sa`
|
||||
- Password: (leave empty)
|
||||
|
||||
## Configuration
|
||||
|
||||
The application can be configured in `src/main/resources/application.properties`:
|
||||
|
||||
```properties
|
||||
# Server Configuration
|
||||
server.port=8080
|
||||
|
||||
# H2 Database
|
||||
spring.datasource.url=jdbc:h2:mem:testdb
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
|
||||
# JWT Configuration
|
||||
jwt.secret=MySecretKeyForJWTTokenGenerationAndValidationMustBeAtLeast256BitsLong
|
||||
jwt.expiration=86400000 # 24 hours in milliseconds
|
||||
```
|
||||
|
||||
## Security Features
|
||||
|
||||
- **Stateless Sessions**: JWT-based authentication with stateless session management
|
||||
- **Password Encryption**: BCrypt password hashing
|
||||
- **Token Validation**: JWT tokens validated on every request
|
||||
- **Authorization**: Protected endpoints require valid JWT token
|
||||
- **CSRF Protection**: Disabled for REST API
|
||||
- **H2 Console Access**: Publicly accessible for development (disable in production)
|
||||
|
||||
## Error Handling
|
||||
|
||||
The API returns appropriate HTTP status codes:
|
||||
- `200 OK` - Successful operation
|
||||
- `201 Created` - Resource created successfully
|
||||
- `401 Unauthorized` - Invalid or missing JWT token
|
||||
- `404 Not Found` - Resource not found
|
||||
- `500 Internal Server Error` - Server error
|
||||
|
||||
## Development Notes
|
||||
|
||||
### JWT Token
|
||||
- Tokens expire after 24 hours (configurable)
|
||||
- Tokens are stateless and contain user information
|
||||
- Each request validates the token signature
|
||||
|
||||
### Database
|
||||
- H2 in-memory database is reset on application restart
|
||||
- Data is re-initialized on each startup
|
||||
- Use JPA configuration to switch to persistent database
|
||||
|
||||
### Logging
|
||||
- Debug logging enabled for security and application packages
|
||||
- View detailed authentication flow in console logs
|
||||
|
||||
## Production Considerations
|
||||
|
||||
Before deploying to production:
|
||||
|
||||
1. **Change JWT Secret**: Use a strong, randomly generated secret key
|
||||
2. **Use Persistent Database**: Replace H2 with PostgreSQL/MySQL
|
||||
3. **Disable H2 Console**: Set `spring.h2.console.enabled=false`
|
||||
4. **Configure HTTPS**: Enable SSL/TLS for secure communication
|
||||
5. **Adjust Token Expiration**: Set appropriate token lifetime
|
||||
6. **Add Rate Limiting**: Prevent brute force attacks
|
||||
7. **Implement Refresh Tokens**: For better token management
|
||||
8. **Add Input Validation**: Validate all user inputs
|
||||
9. **Configure CORS**: Set appropriate CORS policies
|
||||
10. **Add Monitoring**: Implement logging and monitoring solutions
|
||||
|
||||
## License
|
||||
|
||||
This is a demonstration project for educational purposes.
|
||||
|
||||
## Author
|
||||
|
||||
Developed as a Spring Boot REST API demonstration project with JWT authentication and CRUD operations.
|
||||
|
||||
## Support
|
||||
|
||||
For issues or questions, please check the application logs or review the test scripts for usage examples.
|
||||
185
manage.sh
Archivo ejecutable
185
manage.sh
Archivo ejecutable
@@ -0,0 +1,185 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Project Management Script for API Sessions
|
||||
|
||||
print_header() {
|
||||
echo "================================================"
|
||||
echo "$1"
|
||||
echo "================================================"
|
||||
}
|
||||
|
||||
show_help() {
|
||||
echo ""
|
||||
print_header "API Sessions - Project Commands"
|
||||
echo ""
|
||||
echo "Usage: ./manage.sh [command]"
|
||||
echo ""
|
||||
echo "Commands:"
|
||||
echo " start - Start the Spring Boot application"
|
||||
echo " stop - Stop the running application"
|
||||
echo " restart - Restart the application"
|
||||
echo " test - Run comprehensive API tests"
|
||||
echo " quick-test - Run quick API tests"
|
||||
echo " clean - Clean Maven build"
|
||||
echo " build - Build the project"
|
||||
echo " console - Show H2 console URL"
|
||||
echo " logs - Show application logs (tail)"
|
||||
echo " status - Check if application is running"
|
||||
echo " help - Show this help message"
|
||||
echo ""
|
||||
echo "Examples:"
|
||||
echo " ./manage.sh start"
|
||||
echo " ./manage.sh test"
|
||||
echo " ./manage.sh status"
|
||||
echo ""
|
||||
}
|
||||
|
||||
start_app() {
|
||||
print_header "Starting Application"
|
||||
if pgrep -f "api-sessions" > /dev/null; then
|
||||
echo "Application is already running!"
|
||||
echo "Use './manage.sh stop' to stop it first."
|
||||
else
|
||||
echo "Starting Spring Boot application..."
|
||||
./mvnw spring-boot:run > app.log 2>&1 &
|
||||
echo "Application starting in background..."
|
||||
echo "Logs are being written to app.log"
|
||||
echo "Use './manage.sh logs' to view logs"
|
||||
sleep 5
|
||||
if pgrep -f "api-sessions" > /dev/null; then
|
||||
echo "✓ Application started successfully!"
|
||||
echo "Server running at: http://localhost:8080"
|
||||
else
|
||||
echo "✗ Failed to start application. Check app.log for errors."
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
stop_app() {
|
||||
print_header "Stopping Application"
|
||||
if pgrep -f "api-sessions" > /dev/null; then
|
||||
pkill -f "api-sessions"
|
||||
echo "✓ Application stopped"
|
||||
else
|
||||
echo "Application is not running"
|
||||
fi
|
||||
}
|
||||
|
||||
restart_app() {
|
||||
stop_app
|
||||
sleep 2
|
||||
start_app
|
||||
}
|
||||
|
||||
run_tests() {
|
||||
print_header "Running Comprehensive Tests"
|
||||
if ! pgrep -f "api-sessions" > /dev/null; then
|
||||
echo "Application is not running!"
|
||||
echo "Starting application first..."
|
||||
start_app
|
||||
sleep 10
|
||||
fi
|
||||
./test-api.sh
|
||||
}
|
||||
|
||||
run_quick_test() {
|
||||
print_header "Running Quick Tests"
|
||||
if ! pgrep -f "api-sessions" > /dev/null; then
|
||||
echo "Application is not running!"
|
||||
echo "Starting application first..."
|
||||
start_app
|
||||
sleep 10
|
||||
fi
|
||||
./quick-test.sh
|
||||
}
|
||||
|
||||
clean_build() {
|
||||
print_header "Cleaning Build"
|
||||
./mvnw clean
|
||||
echo "✓ Build cleaned"
|
||||
}
|
||||
|
||||
build_project() {
|
||||
print_header "Building Project"
|
||||
./mvnw clean install
|
||||
}
|
||||
|
||||
show_console() {
|
||||
print_header "H2 Database Console"
|
||||
echo "URL: http://localhost:8080/h2-console"
|
||||
echo ""
|
||||
echo "Connection Settings:"
|
||||
echo " JDBC URL: jdbc:h2:mem:testdb"
|
||||
echo " Username: sa"
|
||||
echo " Password: (leave empty)"
|
||||
echo ""
|
||||
}
|
||||
|
||||
show_logs() {
|
||||
if [ -f app.log ]; then
|
||||
tail -f app.log
|
||||
else
|
||||
echo "No log file found. Application might not have been started with './manage.sh start'"
|
||||
fi
|
||||
}
|
||||
|
||||
check_status() {
|
||||
print_header "Application Status"
|
||||
if pgrep -f "api-sessions" > /dev/null; then
|
||||
echo "✓ Application is RUNNING"
|
||||
echo "PID: $(pgrep -f api-sessions)"
|
||||
echo "URL: http://localhost:8080"
|
||||
echo ""
|
||||
echo "Testing connectivity..."
|
||||
if curl -s http://localhost:8080/api/auth/login > /dev/null 2>&1; then
|
||||
echo "✓ API is responding"
|
||||
else
|
||||
echo "✗ API is not responding"
|
||||
fi
|
||||
else
|
||||
echo "✗ Application is NOT running"
|
||||
echo "Use './manage.sh start' to start it"
|
||||
fi
|
||||
}
|
||||
|
||||
# Main command handler
|
||||
case "$1" in
|
||||
start)
|
||||
start_app
|
||||
;;
|
||||
stop)
|
||||
stop_app
|
||||
;;
|
||||
restart)
|
||||
restart_app
|
||||
;;
|
||||
test)
|
||||
run_tests
|
||||
;;
|
||||
quick-test)
|
||||
run_quick_test
|
||||
;;
|
||||
clean)
|
||||
clean_build
|
||||
;;
|
||||
build)
|
||||
build_project
|
||||
;;
|
||||
console)
|
||||
show_console
|
||||
;;
|
||||
logs)
|
||||
show_logs
|
||||
;;
|
||||
status)
|
||||
check_status
|
||||
;;
|
||||
help|--help|-h|"")
|
||||
show_help
|
||||
;;
|
||||
*)
|
||||
echo "Unknown command: $1"
|
||||
show_help
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
295
mvnw
vendido
Archivo ejecutable
295
mvnw
vendido
Archivo ejecutable
@@ -0,0 +1,295 @@
|
||||
#!/bin/sh
|
||||
# ----------------------------------------------------------------------------
|
||||
# Licensed to the Apache Software Foundation (ASF) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The ASF licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
# ----------------------------------------------------------------------------
|
||||
|
||||
# ----------------------------------------------------------------------------
|
||||
# Apache Maven Wrapper startup batch script, version 3.3.4
|
||||
#
|
||||
# Optional ENV vars
|
||||
# -----------------
|
||||
# JAVA_HOME - location of a JDK home dir, required when download maven via java source
|
||||
# MVNW_REPOURL - repo url base for downloading maven distribution
|
||||
# MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven
|
||||
# MVNW_VERBOSE - true: enable verbose log; debug: trace the mvnw script; others: silence the output
|
||||
# ----------------------------------------------------------------------------
|
||||
|
||||
set -euf
|
||||
[ "${MVNW_VERBOSE-}" != debug ] || set -x
|
||||
|
||||
# OS specific support.
|
||||
native_path() { printf %s\\n "$1"; }
|
||||
case "$(uname)" in
|
||||
CYGWIN* | MINGW*)
|
||||
[ -z "${JAVA_HOME-}" ] || JAVA_HOME="$(cygpath --unix "$JAVA_HOME")"
|
||||
native_path() { cygpath --path --windows "$1"; }
|
||||
;;
|
||||
esac
|
||||
|
||||
# set JAVACMD and JAVACCMD
|
||||
set_java_home() {
|
||||
# For Cygwin and MinGW, ensure paths are in Unix format before anything is touched
|
||||
if [ -n "${JAVA_HOME-}" ]; then
|
||||
if [ -x "$JAVA_HOME/jre/sh/java" ]; then
|
||||
# IBM's JDK on AIX uses strange locations for the executables
|
||||
JAVACMD="$JAVA_HOME/jre/sh/java"
|
||||
JAVACCMD="$JAVA_HOME/jre/sh/javac"
|
||||
else
|
||||
JAVACMD="$JAVA_HOME/bin/java"
|
||||
JAVACCMD="$JAVA_HOME/bin/javac"
|
||||
|
||||
if [ ! -x "$JAVACMD" ] || [ ! -x "$JAVACCMD" ]; then
|
||||
echo "The JAVA_HOME environment variable is not defined correctly, so mvnw cannot run." >&2
|
||||
echo "JAVA_HOME is set to \"$JAVA_HOME\", but \"\$JAVA_HOME/bin/java\" or \"\$JAVA_HOME/bin/javac\" does not exist." >&2
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
else
|
||||
JAVACMD="$(
|
||||
'set' +e
|
||||
'unset' -f command 2>/dev/null
|
||||
'command' -v java
|
||||
)" || :
|
||||
JAVACCMD="$(
|
||||
'set' +e
|
||||
'unset' -f command 2>/dev/null
|
||||
'command' -v javac
|
||||
)" || :
|
||||
|
||||
if [ ! -x "${JAVACMD-}" ] || [ ! -x "${JAVACCMD-}" ]; then
|
||||
echo "The java/javac command does not exist in PATH nor is JAVA_HOME set, so mvnw cannot run." >&2
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# hash string like Java String::hashCode
|
||||
hash_string() {
|
||||
str="${1:-}" h=0
|
||||
while [ -n "$str" ]; do
|
||||
char="${str%"${str#?}"}"
|
||||
h=$(((h * 31 + $(LC_CTYPE=C printf %d "'$char")) % 4294967296))
|
||||
str="${str#?}"
|
||||
done
|
||||
printf %x\\n $h
|
||||
}
|
||||
|
||||
verbose() { :; }
|
||||
[ "${MVNW_VERBOSE-}" != true ] || verbose() { printf %s\\n "${1-}"; }
|
||||
|
||||
die() {
|
||||
printf %s\\n "$1" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
trim() {
|
||||
# MWRAPPER-139:
|
||||
# Trims trailing and leading whitespace, carriage returns, tabs, and linefeeds.
|
||||
# Needed for removing poorly interpreted newline sequences when running in more
|
||||
# exotic environments such as mingw bash on Windows.
|
||||
printf "%s" "${1}" | tr -d '[:space:]'
|
||||
}
|
||||
|
||||
scriptDir="$(dirname "$0")"
|
||||
scriptName="$(basename "$0")"
|
||||
|
||||
# parse distributionUrl and optional distributionSha256Sum, requires .mvn/wrapper/maven-wrapper.properties
|
||||
while IFS="=" read -r key value; do
|
||||
case "${key-}" in
|
||||
distributionUrl) distributionUrl=$(trim "${value-}") ;;
|
||||
distributionSha256Sum) distributionSha256Sum=$(trim "${value-}") ;;
|
||||
esac
|
||||
done <"$scriptDir/.mvn/wrapper/maven-wrapper.properties"
|
||||
[ -n "${distributionUrl-}" ] || die "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties"
|
||||
|
||||
case "${distributionUrl##*/}" in
|
||||
maven-mvnd-*bin.*)
|
||||
MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/
|
||||
case "${PROCESSOR_ARCHITECTURE-}${PROCESSOR_ARCHITEW6432-}:$(uname -a)" in
|
||||
*AMD64:CYGWIN* | *AMD64:MINGW*) distributionPlatform=windows-amd64 ;;
|
||||
:Darwin*x86_64) distributionPlatform=darwin-amd64 ;;
|
||||
:Darwin*arm64) distributionPlatform=darwin-aarch64 ;;
|
||||
:Linux*x86_64*) distributionPlatform=linux-amd64 ;;
|
||||
*)
|
||||
echo "Cannot detect native platform for mvnd on $(uname)-$(uname -m), use pure java version" >&2
|
||||
distributionPlatform=linux-amd64
|
||||
;;
|
||||
esac
|
||||
distributionUrl="${distributionUrl%-bin.*}-$distributionPlatform.zip"
|
||||
;;
|
||||
maven-mvnd-*) MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ ;;
|
||||
*) MVN_CMD="mvn${scriptName#mvnw}" _MVNW_REPO_PATTERN=/org/apache/maven/ ;;
|
||||
esac
|
||||
|
||||
# apply MVNW_REPOURL and calculate MAVEN_HOME
|
||||
# maven home pattern: ~/.m2/wrapper/dists/{apache-maven-<version>,maven-mvnd-<version>-<platform>}/<hash>
|
||||
[ -z "${MVNW_REPOURL-}" ] || distributionUrl="$MVNW_REPOURL$_MVNW_REPO_PATTERN${distributionUrl#*"$_MVNW_REPO_PATTERN"}"
|
||||
distributionUrlName="${distributionUrl##*/}"
|
||||
distributionUrlNameMain="${distributionUrlName%.*}"
|
||||
distributionUrlNameMain="${distributionUrlNameMain%-bin}"
|
||||
MAVEN_USER_HOME="${MAVEN_USER_HOME:-${HOME}/.m2}"
|
||||
MAVEN_HOME="${MAVEN_USER_HOME}/wrapper/dists/${distributionUrlNameMain-}/$(hash_string "$distributionUrl")"
|
||||
|
||||
exec_maven() {
|
||||
unset MVNW_VERBOSE MVNW_USERNAME MVNW_PASSWORD MVNW_REPOURL || :
|
||||
exec "$MAVEN_HOME/bin/$MVN_CMD" "$@" || die "cannot exec $MAVEN_HOME/bin/$MVN_CMD"
|
||||
}
|
||||
|
||||
if [ -d "$MAVEN_HOME" ]; then
|
||||
verbose "found existing MAVEN_HOME at $MAVEN_HOME"
|
||||
exec_maven "$@"
|
||||
fi
|
||||
|
||||
case "${distributionUrl-}" in
|
||||
*?-bin.zip | *?maven-mvnd-?*-?*.zip) ;;
|
||||
*) die "distributionUrl is not valid, must match *-bin.zip or maven-mvnd-*.zip, but found '${distributionUrl-}'" ;;
|
||||
esac
|
||||
|
||||
# prepare tmp dir
|
||||
if TMP_DOWNLOAD_DIR="$(mktemp -d)" && [ -d "$TMP_DOWNLOAD_DIR" ]; then
|
||||
clean() { rm -rf -- "$TMP_DOWNLOAD_DIR"; }
|
||||
trap clean HUP INT TERM EXIT
|
||||
else
|
||||
die "cannot create temp dir"
|
||||
fi
|
||||
|
||||
mkdir -p -- "${MAVEN_HOME%/*}"
|
||||
|
||||
# Download and Install Apache Maven
|
||||
verbose "Couldn't find MAVEN_HOME, downloading and installing it ..."
|
||||
verbose "Downloading from: $distributionUrl"
|
||||
verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName"
|
||||
|
||||
# select .zip or .tar.gz
|
||||
if ! command -v unzip >/dev/null; then
|
||||
distributionUrl="${distributionUrl%.zip}.tar.gz"
|
||||
distributionUrlName="${distributionUrl##*/}"
|
||||
fi
|
||||
|
||||
# verbose opt
|
||||
__MVNW_QUIET_WGET=--quiet __MVNW_QUIET_CURL=--silent __MVNW_QUIET_UNZIP=-q __MVNW_QUIET_TAR=''
|
||||
[ "${MVNW_VERBOSE-}" != true ] || __MVNW_QUIET_WGET='' __MVNW_QUIET_CURL='' __MVNW_QUIET_UNZIP='' __MVNW_QUIET_TAR=v
|
||||
|
||||
# normalize http auth
|
||||
case "${MVNW_PASSWORD:+has-password}" in
|
||||
'') MVNW_USERNAME='' MVNW_PASSWORD='' ;;
|
||||
has-password) [ -n "${MVNW_USERNAME-}" ] || MVNW_USERNAME='' MVNW_PASSWORD='' ;;
|
||||
esac
|
||||
|
||||
if [ -z "${MVNW_USERNAME-}" ] && command -v wget >/dev/null; then
|
||||
verbose "Found wget ... using wget"
|
||||
wget ${__MVNW_QUIET_WGET:+"$__MVNW_QUIET_WGET"} "$distributionUrl" -O "$TMP_DOWNLOAD_DIR/$distributionUrlName" || die "wget: Failed to fetch $distributionUrl"
|
||||
elif [ -z "${MVNW_USERNAME-}" ] && command -v curl >/dev/null; then
|
||||
verbose "Found curl ... using curl"
|
||||
curl ${__MVNW_QUIET_CURL:+"$__MVNW_QUIET_CURL"} -f -L -o "$TMP_DOWNLOAD_DIR/$distributionUrlName" "$distributionUrl" || die "curl: Failed to fetch $distributionUrl"
|
||||
elif set_java_home; then
|
||||
verbose "Falling back to use Java to download"
|
||||
javaSource="$TMP_DOWNLOAD_DIR/Downloader.java"
|
||||
targetZip="$TMP_DOWNLOAD_DIR/$distributionUrlName"
|
||||
cat >"$javaSource" <<-END
|
||||
public class Downloader extends java.net.Authenticator
|
||||
{
|
||||
protected java.net.PasswordAuthentication getPasswordAuthentication()
|
||||
{
|
||||
return new java.net.PasswordAuthentication( System.getenv( "MVNW_USERNAME" ), System.getenv( "MVNW_PASSWORD" ).toCharArray() );
|
||||
}
|
||||
public static void main( String[] args ) throws Exception
|
||||
{
|
||||
setDefault( new Downloader() );
|
||||
java.nio.file.Files.copy( java.net.URI.create( args[0] ).toURL().openStream(), java.nio.file.Paths.get( args[1] ).toAbsolutePath().normalize() );
|
||||
}
|
||||
}
|
||||
END
|
||||
# For Cygwin/MinGW, switch paths to Windows format before running javac and java
|
||||
verbose " - Compiling Downloader.java ..."
|
||||
"$(native_path "$JAVACCMD")" "$(native_path "$javaSource")" || die "Failed to compile Downloader.java"
|
||||
verbose " - Running Downloader.java ..."
|
||||
"$(native_path "$JAVACMD")" -cp "$(native_path "$TMP_DOWNLOAD_DIR")" Downloader "$distributionUrl" "$(native_path "$targetZip")"
|
||||
fi
|
||||
|
||||
# If specified, validate the SHA-256 sum of the Maven distribution zip file
|
||||
if [ -n "${distributionSha256Sum-}" ]; then
|
||||
distributionSha256Result=false
|
||||
if [ "$MVN_CMD" = mvnd.sh ]; then
|
||||
echo "Checksum validation is not supported for maven-mvnd." >&2
|
||||
echo "Please disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2
|
||||
exit 1
|
||||
elif command -v sha256sum >/dev/null; then
|
||||
if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | sha256sum -c - >/dev/null 2>&1; then
|
||||
distributionSha256Result=true
|
||||
fi
|
||||
elif command -v shasum >/dev/null; then
|
||||
if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | shasum -a 256 -c >/dev/null 2>&1; then
|
||||
distributionSha256Result=true
|
||||
fi
|
||||
else
|
||||
echo "Checksum validation was requested but neither 'sha256sum' or 'shasum' are available." >&2
|
||||
echo "Please install either command, or disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2
|
||||
exit 1
|
||||
fi
|
||||
if [ $distributionSha256Result = false ]; then
|
||||
echo "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised." >&2
|
||||
echo "If you updated your Maven version, you need to update the specified distributionSha256Sum property." >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# unzip and move
|
||||
if command -v unzip >/dev/null; then
|
||||
unzip ${__MVNW_QUIET_UNZIP:+"$__MVNW_QUIET_UNZIP"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -d "$TMP_DOWNLOAD_DIR" || die "failed to unzip"
|
||||
else
|
||||
tar xzf${__MVNW_QUIET_TAR:+"$__MVNW_QUIET_TAR"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -C "$TMP_DOWNLOAD_DIR" || die "failed to untar"
|
||||
fi
|
||||
|
||||
# Find the actual extracted directory name (handles snapshots where filename != directory name)
|
||||
actualDistributionDir=""
|
||||
|
||||
# First try the expected directory name (for regular distributions)
|
||||
if [ -d "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" ]; then
|
||||
if [ -f "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain/bin/$MVN_CMD" ]; then
|
||||
actualDistributionDir="$distributionUrlNameMain"
|
||||
fi
|
||||
fi
|
||||
|
||||
# If not found, search for any directory with the Maven executable (for snapshots)
|
||||
if [ -z "$actualDistributionDir" ]; then
|
||||
# enable globbing to iterate over items
|
||||
set +f
|
||||
for dir in "$TMP_DOWNLOAD_DIR"/*; do
|
||||
if [ -d "$dir" ]; then
|
||||
if [ -f "$dir/bin/$MVN_CMD" ]; then
|
||||
actualDistributionDir="$(basename "$dir")"
|
||||
break
|
||||
fi
|
||||
fi
|
||||
done
|
||||
set -f
|
||||
fi
|
||||
|
||||
if [ -z "$actualDistributionDir" ]; then
|
||||
verbose "Contents of $TMP_DOWNLOAD_DIR:"
|
||||
verbose "$(ls -la "$TMP_DOWNLOAD_DIR")"
|
||||
die "Could not find Maven distribution directory in extracted archive"
|
||||
fi
|
||||
|
||||
verbose "Found extracted Maven distribution directory: $actualDistributionDir"
|
||||
printf %s\\n "$distributionUrl" >"$TMP_DOWNLOAD_DIR/$actualDistributionDir/mvnw.url"
|
||||
mv -- "$TMP_DOWNLOAD_DIR/$actualDistributionDir" "$MAVEN_HOME" || [ -d "$MAVEN_HOME" ] || die "fail to move MAVEN_HOME"
|
||||
|
||||
clean || :
|
||||
exec_maven "$@"
|
||||
189
mvnw.cmd
vendido
Archivo normal
189
mvnw.cmd
vendido
Archivo normal
@@ -0,0 +1,189 @@
|
||||
<# : batch portion
|
||||
@REM ----------------------------------------------------------------------------
|
||||
@REM Licensed to the Apache Software Foundation (ASF) under one
|
||||
@REM or more contributor license agreements. See the NOTICE file
|
||||
@REM distributed with this work for additional information
|
||||
@REM regarding copyright ownership. The ASF licenses this file
|
||||
@REM to you under the Apache License, Version 2.0 (the
|
||||
@REM "License"); you may not use this file except in compliance
|
||||
@REM with the License. You may obtain a copy of the License at
|
||||
@REM
|
||||
@REM http://www.apache.org/licenses/LICENSE-2.0
|
||||
@REM
|
||||
@REM Unless required by applicable law or agreed to in writing,
|
||||
@REM software distributed under the License is distributed on an
|
||||
@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
@REM KIND, either express or implied. See the License for the
|
||||
@REM specific language governing permissions and limitations
|
||||
@REM under the License.
|
||||
@REM ----------------------------------------------------------------------------
|
||||
|
||||
@REM ----------------------------------------------------------------------------
|
||||
@REM Apache Maven Wrapper startup batch script, version 3.3.4
|
||||
@REM
|
||||
@REM Optional ENV vars
|
||||
@REM MVNW_REPOURL - repo url base for downloading maven distribution
|
||||
@REM MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven
|
||||
@REM MVNW_VERBOSE - true: enable verbose log; others: silence the output
|
||||
@REM ----------------------------------------------------------------------------
|
||||
|
||||
@IF "%__MVNW_ARG0_NAME__%"=="" (SET __MVNW_ARG0_NAME__=%~nx0)
|
||||
@SET __MVNW_CMD__=
|
||||
@SET __MVNW_ERROR__=
|
||||
@SET __MVNW_PSMODULEP_SAVE=%PSModulePath%
|
||||
@SET PSModulePath=
|
||||
@FOR /F "usebackq tokens=1* delims==" %%A IN (`powershell -noprofile "& {$scriptDir='%~dp0'; $script='%__MVNW_ARG0_NAME__%'; icm -ScriptBlock ([Scriptblock]::Create((Get-Content -Raw '%~f0'))) -NoNewScope}"`) DO @(
|
||||
IF "%%A"=="MVN_CMD" (set __MVNW_CMD__=%%B) ELSE IF "%%B"=="" (echo %%A) ELSE (echo %%A=%%B)
|
||||
)
|
||||
@SET PSModulePath=%__MVNW_PSMODULEP_SAVE%
|
||||
@SET __MVNW_PSMODULEP_SAVE=
|
||||
@SET __MVNW_ARG0_NAME__=
|
||||
@SET MVNW_USERNAME=
|
||||
@SET MVNW_PASSWORD=
|
||||
@IF NOT "%__MVNW_CMD__%"=="" ("%__MVNW_CMD__%" %*)
|
||||
@echo Cannot start maven from wrapper >&2 && exit /b 1
|
||||
@GOTO :EOF
|
||||
: end batch / begin powershell #>
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
if ($env:MVNW_VERBOSE -eq "true") {
|
||||
$VerbosePreference = "Continue"
|
||||
}
|
||||
|
||||
# calculate distributionUrl, requires .mvn/wrapper/maven-wrapper.properties
|
||||
$distributionUrl = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionUrl
|
||||
if (!$distributionUrl) {
|
||||
Write-Error "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties"
|
||||
}
|
||||
|
||||
switch -wildcard -casesensitive ( $($distributionUrl -replace '^.*/','') ) {
|
||||
"maven-mvnd-*" {
|
||||
$USE_MVND = $true
|
||||
$distributionUrl = $distributionUrl -replace '-bin\.[^.]*$',"-windows-amd64.zip"
|
||||
$MVN_CMD = "mvnd.cmd"
|
||||
break
|
||||
}
|
||||
default {
|
||||
$USE_MVND = $false
|
||||
$MVN_CMD = $script -replace '^mvnw','mvn'
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
# apply MVNW_REPOURL and calculate MAVEN_HOME
|
||||
# maven home pattern: ~/.m2/wrapper/dists/{apache-maven-<version>,maven-mvnd-<version>-<platform>}/<hash>
|
||||
if ($env:MVNW_REPOURL) {
|
||||
$MVNW_REPO_PATTERN = if ($USE_MVND -eq $False) { "/org/apache/maven/" } else { "/maven/mvnd/" }
|
||||
$distributionUrl = "$env:MVNW_REPOURL$MVNW_REPO_PATTERN$($distributionUrl -replace "^.*$MVNW_REPO_PATTERN",'')"
|
||||
}
|
||||
$distributionUrlName = $distributionUrl -replace '^.*/',''
|
||||
$distributionUrlNameMain = $distributionUrlName -replace '\.[^.]*$','' -replace '-bin$',''
|
||||
|
||||
$MAVEN_M2_PATH = "$HOME/.m2"
|
||||
if ($env:MAVEN_USER_HOME) {
|
||||
$MAVEN_M2_PATH = "$env:MAVEN_USER_HOME"
|
||||
}
|
||||
|
||||
if (-not (Test-Path -Path $MAVEN_M2_PATH)) {
|
||||
New-Item -Path $MAVEN_M2_PATH -ItemType Directory | Out-Null
|
||||
}
|
||||
|
||||
$MAVEN_WRAPPER_DISTS = $null
|
||||
if ((Get-Item $MAVEN_M2_PATH).Target[0] -eq $null) {
|
||||
$MAVEN_WRAPPER_DISTS = "$MAVEN_M2_PATH/wrapper/dists"
|
||||
} else {
|
||||
$MAVEN_WRAPPER_DISTS = (Get-Item $MAVEN_M2_PATH).Target[0] + "/wrapper/dists"
|
||||
}
|
||||
|
||||
$MAVEN_HOME_PARENT = "$MAVEN_WRAPPER_DISTS/$distributionUrlNameMain"
|
||||
$MAVEN_HOME_NAME = ([System.Security.Cryptography.SHA256]::Create().ComputeHash([byte[]][char[]]$distributionUrl) | ForEach-Object {$_.ToString("x2")}) -join ''
|
||||
$MAVEN_HOME = "$MAVEN_HOME_PARENT/$MAVEN_HOME_NAME"
|
||||
|
||||
if (Test-Path -Path "$MAVEN_HOME" -PathType Container) {
|
||||
Write-Verbose "found existing MAVEN_HOME at $MAVEN_HOME"
|
||||
Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD"
|
||||
exit $?
|
||||
}
|
||||
|
||||
if (! $distributionUrlNameMain -or ($distributionUrlName -eq $distributionUrlNameMain)) {
|
||||
Write-Error "distributionUrl is not valid, must end with *-bin.zip, but found $distributionUrl"
|
||||
}
|
||||
|
||||
# prepare tmp dir
|
||||
$TMP_DOWNLOAD_DIR_HOLDER = New-TemporaryFile
|
||||
$TMP_DOWNLOAD_DIR = New-Item -Itemtype Directory -Path "$TMP_DOWNLOAD_DIR_HOLDER.dir"
|
||||
$TMP_DOWNLOAD_DIR_HOLDER.Delete() | Out-Null
|
||||
trap {
|
||||
if ($TMP_DOWNLOAD_DIR.Exists) {
|
||||
try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null }
|
||||
catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" }
|
||||
}
|
||||
}
|
||||
|
||||
New-Item -Itemtype Directory -Path "$MAVEN_HOME_PARENT" -Force | Out-Null
|
||||
|
||||
# Download and Install Apache Maven
|
||||
Write-Verbose "Couldn't find MAVEN_HOME, downloading and installing it ..."
|
||||
Write-Verbose "Downloading from: $distributionUrl"
|
||||
Write-Verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName"
|
||||
|
||||
$webclient = New-Object System.Net.WebClient
|
||||
if ($env:MVNW_USERNAME -and $env:MVNW_PASSWORD) {
|
||||
$webclient.Credentials = New-Object System.Net.NetworkCredential($env:MVNW_USERNAME, $env:MVNW_PASSWORD)
|
||||
}
|
||||
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
|
||||
$webclient.DownloadFile($distributionUrl, "$TMP_DOWNLOAD_DIR/$distributionUrlName") | Out-Null
|
||||
|
||||
# If specified, validate the SHA-256 sum of the Maven distribution zip file
|
||||
$distributionSha256Sum = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionSha256Sum
|
||||
if ($distributionSha256Sum) {
|
||||
if ($USE_MVND) {
|
||||
Write-Error "Checksum validation is not supported for maven-mvnd. `nPlease disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties."
|
||||
}
|
||||
Import-Module $PSHOME\Modules\Microsoft.PowerShell.Utility -Function Get-FileHash
|
||||
if ((Get-FileHash "$TMP_DOWNLOAD_DIR/$distributionUrlName" -Algorithm SHA256).Hash.ToLower() -ne $distributionSha256Sum) {
|
||||
Write-Error "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised. If you updated your Maven version, you need to update the specified distributionSha256Sum property."
|
||||
}
|
||||
}
|
||||
|
||||
# unzip and move
|
||||
Expand-Archive "$TMP_DOWNLOAD_DIR/$distributionUrlName" -DestinationPath "$TMP_DOWNLOAD_DIR" | Out-Null
|
||||
|
||||
# Find the actual extracted directory name (handles snapshots where filename != directory name)
|
||||
$actualDistributionDir = ""
|
||||
|
||||
# First try the expected directory name (for regular distributions)
|
||||
$expectedPath = Join-Path "$TMP_DOWNLOAD_DIR" "$distributionUrlNameMain"
|
||||
$expectedMvnPath = Join-Path "$expectedPath" "bin/$MVN_CMD"
|
||||
if ((Test-Path -Path $expectedPath -PathType Container) -and (Test-Path -Path $expectedMvnPath -PathType Leaf)) {
|
||||
$actualDistributionDir = $distributionUrlNameMain
|
||||
}
|
||||
|
||||
# If not found, search for any directory with the Maven executable (for snapshots)
|
||||
if (!$actualDistributionDir) {
|
||||
Get-ChildItem -Path "$TMP_DOWNLOAD_DIR" -Directory | ForEach-Object {
|
||||
$testPath = Join-Path $_.FullName "bin/$MVN_CMD"
|
||||
if (Test-Path -Path $testPath -PathType Leaf) {
|
||||
$actualDistributionDir = $_.Name
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$actualDistributionDir) {
|
||||
Write-Error "Could not find Maven distribution directory in extracted archive"
|
||||
}
|
||||
|
||||
Write-Verbose "Found extracted Maven distribution directory: $actualDistributionDir"
|
||||
Rename-Item -Path "$TMP_DOWNLOAD_DIR/$actualDistributionDir" -NewName $MAVEN_HOME_NAME | Out-Null
|
||||
try {
|
||||
Move-Item -Path "$TMP_DOWNLOAD_DIR/$MAVEN_HOME_NAME" -Destination $MAVEN_HOME_PARENT | Out-Null
|
||||
} catch {
|
||||
if (! (Test-Path -Path "$MAVEN_HOME" -PathType Container)) {
|
||||
Write-Error "fail to move MAVEN_HOME"
|
||||
}
|
||||
} finally {
|
||||
try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null }
|
||||
catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" }
|
||||
}
|
||||
|
||||
Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD"
|
||||
133
pom.xml
Archivo normal
133
pom.xml
Archivo normal
@@ -0,0 +1,133 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>4.1.0-M1</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>com.manalejandro</groupId>
|
||||
<artifactId>api-sessions</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>war</packaging>
|
||||
<name>api-sessions</name>
|
||||
<description>Demo project for Spring Boot with API and Sessions</description>
|
||||
<url/>
|
||||
<licenses>
|
||||
<license/>
|
||||
</licenses>
|
||||
<developers>
|
||||
<developer/>
|
||||
</developers>
|
||||
<scm>
|
||||
<connection/>
|
||||
<developerConnection/>
|
||||
<tag/>
|
||||
<url/>
|
||||
</scm>
|
||||
<properties>
|
||||
<java.version>21</java.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-h2console</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-security</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webmvc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
<artifactId>jjwt-api</artifactId>
|
||||
<version>0.12.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
<artifactId>jjwt-impl</artifactId>
|
||||
<version>0.12.6</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
<artifactId>jjwt-jackson</artifactId>
|
||||
<version>0.12.6</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-tomcat</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-security-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webmvc-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<annotationProcessorPaths>
|
||||
<path>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</path>
|
||||
</annotationProcessorPaths>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
82
quick-test.sh
Archivo ejecutable
82
quick-test.sh
Archivo ejecutable
@@ -0,0 +1,82 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Quick Test Script - Simple API Testing
|
||||
|
||||
BASE_URL="http://localhost:8080"
|
||||
|
||||
echo "========================================="
|
||||
echo "Quick API Test"
|
||||
echo "========================================="
|
||||
echo ""
|
||||
|
||||
# Step 1: Login
|
||||
echo "1. Login to get JWT token..."
|
||||
LOGIN_RESPONSE=$(curl -s -X POST "$BASE_URL/api/auth/login" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"username":"admin","password":"admin123"}')
|
||||
|
||||
TOKEN=$(echo "$LOGIN_RESPONSE" | grep -o '"token":"[^"]*' | cut -d'"' -f4)
|
||||
|
||||
if [ -z "$TOKEN" ]; then
|
||||
echo "ERROR: Login failed!"
|
||||
echo "Response: $LOGIN_RESPONSE"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "✓ Login successful!"
|
||||
echo "Token: ${TOKEN:0:20}..."
|
||||
echo ""
|
||||
|
||||
# Step 2: Get all products
|
||||
echo "2. Getting all products..."
|
||||
curl -s -X GET "$BASE_URL/api/products" \
|
||||
-H "Authorization: Bearer $TOKEN" | \
|
||||
python3 -m json.tool 2>/dev/null || cat
|
||||
echo ""
|
||||
echo "✓ Products retrieved!"
|
||||
echo ""
|
||||
|
||||
# Step 3: Create a product
|
||||
echo "3. Creating a new product..."
|
||||
CREATE_RESPONSE=$(curl -s -X POST "$BASE_URL/api/products" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{
|
||||
"name": "Test Product",
|
||||
"description": "Created by test script",
|
||||
"price": 99.99,
|
||||
"stock": 5,
|
||||
"category": "Test"
|
||||
}')
|
||||
echo "$CREATE_RESPONSE" | python3 -m json.tool 2>/dev/null || echo "$CREATE_RESPONSE"
|
||||
echo "✓ Product created!"
|
||||
echo ""
|
||||
|
||||
# Step 4: Get product by ID
|
||||
echo "4. Getting product by ID (1)..."
|
||||
curl -s -X GET "$BASE_URL/api/products/1" \
|
||||
-H "Authorization: Bearer $TOKEN" | \
|
||||
python3 -m json.tool 2>/dev/null || cat
|
||||
echo ""
|
||||
echo "✓ Product retrieved by ID!"
|
||||
echo ""
|
||||
|
||||
# Step 5: Update product
|
||||
echo "5. Updating product (ID=1)..."
|
||||
UPDATE_RESPONSE=$(curl -s -X PUT "$BASE_URL/api/products/1" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{
|
||||
"name": "Updated Product",
|
||||
"description": "This product was updated",
|
||||
"price": 199.99,
|
||||
"stock": 10,
|
||||
"category": "Electronics"
|
||||
}')
|
||||
echo "$UPDATE_RESPONSE" | python3 -m json.tool 2>/dev/null || echo "$UPDATE_RESPONSE"
|
||||
echo "✓ Product updated!"
|
||||
echo ""
|
||||
|
||||
echo "========================================="
|
||||
echo "All tests completed successfully!"
|
||||
echo "========================================="
|
||||
13
src/main/java/com/manalejandro/api_sessions/ApiSessionsApplication.java
Archivo normal
13
src/main/java/com/manalejandro/api_sessions/ApiSessionsApplication.java
Archivo normal
@@ -0,0 +1,13 @@
|
||||
package com.manalejandro.api_sessions;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ApiSessionsApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ApiSessionsApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
13
src/main/java/com/manalejandro/api_sessions/ServletInitializer.java
Archivo normal
13
src/main/java/com/manalejandro/api_sessions/ServletInitializer.java
Archivo normal
@@ -0,0 +1,13 @@
|
||||
package com.manalejandro.api_sessions;
|
||||
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
|
||||
|
||||
public class ServletInitializer extends SpringBootServletInitializer {
|
||||
|
||||
@Override
|
||||
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
|
||||
return application.sources(ApiSessionsApplication.class);
|
||||
}
|
||||
|
||||
}
|
||||
168
src/main/java/com/manalejandro/api_sessions/config/DataInitializer.java
Archivo normal
168
src/main/java/com/manalejandro/api_sessions/config/DataInitializer.java
Archivo normal
@@ -0,0 +1,168 @@
|
||||
package com.manalejandro.api_sessions.config;
|
||||
|
||||
import com.manalejandro.api_sessions.entity.Product;
|
||||
import com.manalejandro.api_sessions.entity.User;
|
||||
import com.manalejandro.api_sessions.repository.ProductRepository;
|
||||
import com.manalejandro.api_sessions.repository.UserRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Database initialization component.
|
||||
* Populates the database with initial data when the application starts.
|
||||
*/
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class DataInitializer implements CommandLineRunner {
|
||||
|
||||
private final UserRepository userRepository;
|
||||
private final ProductRepository productRepository;
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
|
||||
@Override
|
||||
public void run(String... args) {
|
||||
log.info("Starting database initialization...");
|
||||
|
||||
// Initialize users
|
||||
initializeUsers();
|
||||
|
||||
// Initialize products
|
||||
initializeProducts();
|
||||
|
||||
log.info("Database initialization completed!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize users in the database.
|
||||
*/
|
||||
private void initializeUsers() {
|
||||
if (userRepository.count() == 0) {
|
||||
List<User> users = Arrays.asList(
|
||||
User.builder()
|
||||
.username("admin")
|
||||
.password(passwordEncoder.encode("admin123"))
|
||||
.email("admin@example.com")
|
||||
.role("ROLE_ADMIN")
|
||||
.createdAt(LocalDateTime.now())
|
||||
.updatedAt(LocalDateTime.now())
|
||||
.build(),
|
||||
User.builder()
|
||||
.username("user")
|
||||
.password(passwordEncoder.encode("user123"))
|
||||
.email("user@example.com")
|
||||
.role("ROLE_USER")
|
||||
.createdAt(LocalDateTime.now())
|
||||
.updatedAt(LocalDateTime.now())
|
||||
.build(),
|
||||
User.builder()
|
||||
.username("john")
|
||||
.password(passwordEncoder.encode("john123"))
|
||||
.email("john@example.com")
|
||||
.role("ROLE_USER")
|
||||
.createdAt(LocalDateTime.now())
|
||||
.updatedAt(LocalDateTime.now())
|
||||
.build()
|
||||
);
|
||||
|
||||
userRepository.saveAll(users);
|
||||
log.info("Initialized {} users", users.size());
|
||||
users.forEach(user -> log.info("Created user: {} with role: {}", user.getUsername(), user.getRole()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize products in the database.
|
||||
*/
|
||||
private void initializeProducts() {
|
||||
if (productRepository.count() == 0) {
|
||||
List<Product> products = Arrays.asList(
|
||||
Product.builder()
|
||||
.name("Laptop Dell XPS 15")
|
||||
.description("High-performance laptop with 16GB RAM and 512GB SSD")
|
||||
.price(new BigDecimal("1299.99"))
|
||||
.stock(15)
|
||||
.category("Electronics")
|
||||
.createdAt(LocalDateTime.now())
|
||||
.updatedAt(LocalDateTime.now())
|
||||
.build(),
|
||||
Product.builder()
|
||||
.name("iPhone 15 Pro")
|
||||
.description("Latest Apple smartphone with advanced features")
|
||||
.price(new BigDecimal("999.99"))
|
||||
.stock(25)
|
||||
.category("Electronics")
|
||||
.createdAt(LocalDateTime.now())
|
||||
.updatedAt(LocalDateTime.now())
|
||||
.build(),
|
||||
Product.builder()
|
||||
.name("Wireless Mouse Logitech MX Master 3")
|
||||
.description("Ergonomic wireless mouse for professionals")
|
||||
.price(new BigDecimal("99.99"))
|
||||
.stock(50)
|
||||
.category("Accessories")
|
||||
.createdAt(LocalDateTime.now())
|
||||
.updatedAt(LocalDateTime.now())
|
||||
.build(),
|
||||
Product.builder()
|
||||
.name("Mechanical Keyboard")
|
||||
.description("RGB mechanical keyboard with Cherry MX switches")
|
||||
.price(new BigDecimal("149.99"))
|
||||
.stock(30)
|
||||
.category("Accessories")
|
||||
.createdAt(LocalDateTime.now())
|
||||
.updatedAt(LocalDateTime.now())
|
||||
.build(),
|
||||
Product.builder()
|
||||
.name("27-inch 4K Monitor")
|
||||
.description("Ultra HD monitor with HDR support")
|
||||
.price(new BigDecimal("449.99"))
|
||||
.stock(20)
|
||||
.category("Electronics")
|
||||
.createdAt(LocalDateTime.now())
|
||||
.updatedAt(LocalDateTime.now())
|
||||
.build(),
|
||||
Product.builder()
|
||||
.name("USB-C Hub")
|
||||
.description("7-in-1 USB-C hub with HDMI and card reader")
|
||||
.price(new BigDecimal("49.99"))
|
||||
.stock(100)
|
||||
.category("Accessories")
|
||||
.createdAt(LocalDateTime.now())
|
||||
.updatedAt(LocalDateTime.now())
|
||||
.build(),
|
||||
Product.builder()
|
||||
.name("Wireless Headphones Sony WH-1000XM5")
|
||||
.description("Premium noise-canceling wireless headphones")
|
||||
.price(new BigDecimal("349.99"))
|
||||
.stock(40)
|
||||
.category("Audio")
|
||||
.createdAt(LocalDateTime.now())
|
||||
.updatedAt(LocalDateTime.now())
|
||||
.build(),
|
||||
Product.builder()
|
||||
.name("Webcam Logitech C920")
|
||||
.description("Full HD webcam for video conferencing")
|
||||
.price(new BigDecimal("79.99"))
|
||||
.stock(60)
|
||||
.category("Accessories")
|
||||
.createdAt(LocalDateTime.now())
|
||||
.updatedAt(LocalDateTime.now())
|
||||
.build()
|
||||
);
|
||||
|
||||
productRepository.saveAll(products);
|
||||
log.info("Initialized {} products", products.size());
|
||||
products.forEach(product -> log.info("Created product: {} - ${} (Stock: {})",
|
||||
product.getName(), product.getPrice(), product.getStock()));
|
||||
}
|
||||
}
|
||||
}
|
||||
81
src/main/java/com/manalejandro/api_sessions/config/SecurityConfig.java
Archivo normal
81
src/main/java/com/manalejandro/api_sessions/config/SecurityConfig.java
Archivo normal
@@ -0,0 +1,81 @@
|
||||
package com.manalejandro.api_sessions.config;
|
||||
|
||||
import com.manalejandro.api_sessions.security.JwtRequestFilter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.security.authentication.AuthenticationProvider;
|
||||
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
|
||||
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
|
||||
import org.springframework.security.config.http.SessionCreationPolicy;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
||||
|
||||
/**
|
||||
* Security configuration class that sets up Spring Security with JWT authentication.
|
||||
* Configures authentication, authorization, and session management.
|
||||
*/
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
@RequiredArgsConstructor
|
||||
public class SecurityConfig {
|
||||
|
||||
private final JwtRequestFilter jwtRequestFilter;
|
||||
private final UserDetailsService userDetailsService;
|
||||
|
||||
/**
|
||||
* Configure security filter chain.
|
||||
* Sets up authorization rules, session management, and JWT filter.
|
||||
*
|
||||
* @param http the HttpSecurity object
|
||||
* @return the configured SecurityFilterChain
|
||||
* @throws Exception if configuration fails
|
||||
*/
|
||||
@Bean
|
||||
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
||||
http
|
||||
.csrf(AbstractHttpConfigurer::disable)
|
||||
.authorizeHttpRequests(auth -> auth
|
||||
.requestMatchers("/api/auth/**", "/h2-console/**").permitAll()
|
||||
.anyRequest().authenticated()
|
||||
)
|
||||
.sessionManagement(session -> session
|
||||
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
|
||||
)
|
||||
.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
|
||||
|
||||
// Allow H2 console frames
|
||||
http.headers(headers -> headers.frameOptions(frameOptions -> frameOptions.disable()));
|
||||
|
||||
return http.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure password encoder.
|
||||
*
|
||||
* @return BCryptPasswordEncoder instance
|
||||
*/
|
||||
@Bean
|
||||
public PasswordEncoder passwordEncoder() {
|
||||
return new BCryptPasswordEncoder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure authentication manager.
|
||||
*
|
||||
* @param config the authentication configuration
|
||||
* @return the AuthenticationManager
|
||||
* @throws Exception if configuration fails
|
||||
*/
|
||||
@Bean
|
||||
public AuthenticationManager authenticationManager(AuthenticationConfiguration config) throws Exception {
|
||||
return config.getAuthenticationManager();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package com.manalejandro.api_sessions.controller;
|
||||
|
||||
import com.manalejandro.api_sessions.dto.AuthRequest;
|
||||
import com.manalejandro.api_sessions.dto.AuthResponse;
|
||||
import com.manalejandro.api_sessions.security.JwtTokenUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.security.authentication.BadCredentialsException;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* REST controller for authentication operations.
|
||||
* Handles user login and JWT token generation.
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/auth")
|
||||
@RequiredArgsConstructor
|
||||
public class AuthController {
|
||||
|
||||
private final AuthenticationManager authenticationManager;
|
||||
private final JwtTokenUtil jwtTokenUtil;
|
||||
private final UserDetailsService userDetailsService;
|
||||
|
||||
/**
|
||||
* Authenticate user and generate JWT token.
|
||||
*
|
||||
* @param authRequest the authentication request containing username and password
|
||||
* @return ResponseEntity with JWT token and user information
|
||||
*/
|
||||
@PostMapping("/login")
|
||||
public ResponseEntity<?> login(@RequestBody AuthRequest authRequest) {
|
||||
try {
|
||||
// Authenticate the user
|
||||
Authentication authentication = authenticationManager.authenticate(
|
||||
new UsernamePasswordAuthenticationToken(
|
||||
authRequest.getUsername(),
|
||||
authRequest.getPassword()
|
||||
)
|
||||
);
|
||||
|
||||
// Load user details
|
||||
UserDetails userDetails = userDetailsService.loadUserByUsername(authRequest.getUsername());
|
||||
|
||||
// Generate JWT token
|
||||
String token = jwtTokenUtil.generateToken(userDetails);
|
||||
|
||||
// Return response with token
|
||||
AuthResponse response = AuthResponse.builder()
|
||||
.token(token)
|
||||
.username(userDetails.getUsername())
|
||||
.message("Authentication successful")
|
||||
.build();
|
||||
|
||||
return ResponseEntity.ok(response);
|
||||
|
||||
} catch (BadCredentialsException e) {
|
||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
|
||||
.body(AuthResponse.builder()
|
||||
.message("Invalid username or password")
|
||||
.build());
|
||||
} catch (Exception e) {
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
.body(AuthResponse.builder()
|
||||
.message("Authentication failed: " + e.getMessage())
|
||||
.build());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check authentication status.
|
||||
*
|
||||
* @return ResponseEntity with authentication status
|
||||
*/
|
||||
@GetMapping("/status")
|
||||
public ResponseEntity<?> checkStatus(Authentication authentication) {
|
||||
if (authentication != null && authentication.isAuthenticated()) {
|
||||
return ResponseEntity.ok(AuthResponse.builder()
|
||||
.username(authentication.getName())
|
||||
.message("Authenticated")
|
||||
.build());
|
||||
}
|
||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
|
||||
.body(AuthResponse.builder()
|
||||
.message("Not authenticated")
|
||||
.build());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
package com.manalejandro.api_sessions.controller;
|
||||
|
||||
import com.manalejandro.api_sessions.dto.ProductDto;
|
||||
import com.manalejandro.api_sessions.entity.Product;
|
||||
import com.manalejandro.api_sessions.service.ProductService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* REST controller for Product CRUD operations.
|
||||
* Provides endpoints for managing products.
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/products")
|
||||
@RequiredArgsConstructor
|
||||
public class ProductController {
|
||||
|
||||
private final ProductService productService;
|
||||
|
||||
/**
|
||||
* Get all products.
|
||||
*
|
||||
* @return list of all products
|
||||
*/
|
||||
@GetMapping
|
||||
public ResponseEntity<List<ProductDto>> getAllProducts() {
|
||||
List<Product> products = productService.getAllProducts();
|
||||
List<ProductDto> productDtos = products.stream()
|
||||
.map(this::convertToDto)
|
||||
.collect(Collectors.toList());
|
||||
return ResponseEntity.ok(productDtos);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a product by ID.
|
||||
*
|
||||
* @param id the product ID
|
||||
* @return the product if found
|
||||
*/
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<?> getProductById(@PathVariable Long id) {
|
||||
return productService.getProductById(id)
|
||||
.map(product -> ResponseEntity.ok(convertToDto(product)))
|
||||
.orElse(ResponseEntity.notFound().build());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get products by category.
|
||||
*
|
||||
* @param category the category to filter by
|
||||
* @return list of products in the category
|
||||
*/
|
||||
@GetMapping("/category/{category}")
|
||||
public ResponseEntity<List<ProductDto>> getProductsByCategory(@PathVariable String category) {
|
||||
List<Product> products = productService.getProductsByCategory(category);
|
||||
List<ProductDto> productDtos = products.stream()
|
||||
.map(this::convertToDto)
|
||||
.collect(Collectors.toList());
|
||||
return ResponseEntity.ok(productDtos);
|
||||
}
|
||||
|
||||
/**
|
||||
* Search products by name.
|
||||
*
|
||||
* @param name the name to search for
|
||||
* @return list of matching products
|
||||
*/
|
||||
@GetMapping("/search")
|
||||
public ResponseEntity<List<ProductDto>> searchProducts(@RequestParam String name) {
|
||||
List<Product> products = productService.searchProductsByName(name);
|
||||
List<ProductDto> productDtos = products.stream()
|
||||
.map(this::convertToDto)
|
||||
.collect(Collectors.toList());
|
||||
return ResponseEntity.ok(productDtos);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new product.
|
||||
*
|
||||
* @param productDto the product data
|
||||
* @return the created product
|
||||
*/
|
||||
@PostMapping
|
||||
public ResponseEntity<ProductDto> createProduct(@RequestBody ProductDto productDto) {
|
||||
Product product = convertToEntity(productDto);
|
||||
Product savedProduct = productService.createProduct(product);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(convertToDto(savedProduct));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an existing product.
|
||||
*
|
||||
* @param id the product ID
|
||||
* @param productDto the updated product data
|
||||
* @return the updated product
|
||||
*/
|
||||
@PutMapping("/{id}")
|
||||
public ResponseEntity<?> updateProduct(@PathVariable Long id, @RequestBody ProductDto productDto) {
|
||||
Product product = convertToEntity(productDto);
|
||||
return productService.updateProduct(id, product)
|
||||
.map(updatedProduct -> ResponseEntity.ok(convertToDto(updatedProduct)))
|
||||
.orElse(ResponseEntity.notFound().build());
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a product by ID.
|
||||
*
|
||||
* @param id the product ID
|
||||
* @return response indicating success or failure
|
||||
*/
|
||||
@DeleteMapping("/{id}")
|
||||
public ResponseEntity<?> deleteProduct(@PathVariable Long id) {
|
||||
if (productService.deleteProduct(id)) {
|
||||
return ResponseEntity.ok().body("Product deleted successfully");
|
||||
}
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert Product entity to ProductDto.
|
||||
*
|
||||
* @param product the product entity
|
||||
* @return the product DTO
|
||||
*/
|
||||
private ProductDto convertToDto(Product product) {
|
||||
return ProductDto.builder()
|
||||
.id(product.getId())
|
||||
.name(product.getName())
|
||||
.description(product.getDescription())
|
||||
.price(product.getPrice())
|
||||
.stock(product.getStock())
|
||||
.category(product.getCategory())
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert ProductDto to Product entity.
|
||||
*
|
||||
* @param productDto the product DTO
|
||||
* @return the product entity
|
||||
*/
|
||||
private Product convertToEntity(ProductDto productDto) {
|
||||
return Product.builder()
|
||||
.id(productDto.getId())
|
||||
.name(productDto.getName())
|
||||
.description(productDto.getDescription())
|
||||
.price(productDto.getPrice())
|
||||
.stock(productDto.getStock())
|
||||
.category(productDto.getCategory())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
19
src/main/java/com/manalejandro/api_sessions/dto/AuthRequest.java
Archivo normal
19
src/main/java/com/manalejandro/api_sessions/dto/AuthRequest.java
Archivo normal
@@ -0,0 +1,19 @@
|
||||
package com.manalejandro.api_sessions.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* DTO for authentication requests.
|
||||
* Used for login operations.
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class AuthRequest {
|
||||
private String username;
|
||||
private String password;
|
||||
}
|
||||
20
src/main/java/com/manalejandro/api_sessions/dto/AuthResponse.java
Archivo normal
20
src/main/java/com/manalejandro/api_sessions/dto/AuthResponse.java
Archivo normal
@@ -0,0 +1,20 @@
|
||||
package com.manalejandro.api_sessions.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* DTO for authentication responses.
|
||||
* Contains JWT token and user information.
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class AuthResponse {
|
||||
private String token;
|
||||
private String username;
|
||||
private String message;
|
||||
}
|
||||
25
src/main/java/com/manalejandro/api_sessions/dto/ProductDto.java
Archivo normal
25
src/main/java/com/manalejandro/api_sessions/dto/ProductDto.java
Archivo normal
@@ -0,0 +1,25 @@
|
||||
package com.manalejandro.api_sessions.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* DTO for Product entity.
|
||||
* Used for creating and updating products.
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class ProductDto {
|
||||
private Long id;
|
||||
private String name;
|
||||
private String description;
|
||||
private BigDecimal price;
|
||||
private Integer stock;
|
||||
private String category;
|
||||
}
|
||||
59
src/main/java/com/manalejandro/api_sessions/entity/Product.java
Archivo normal
59
src/main/java/com/manalejandro/api_sessions/entity/Product.java
Archivo normal
@@ -0,0 +1,59 @@
|
||||
package com.manalejandro.api_sessions.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* Product entity representing products in the inventory.
|
||||
* This entity is used for CRUD operations demonstration.
|
||||
*/
|
||||
@Entity
|
||||
@Table(name = "products")
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Product {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(nullable = false, length = 100)
|
||||
private String name;
|
||||
|
||||
@Column(length = 500)
|
||||
private String description;
|
||||
|
||||
@Column(nullable = false, precision = 10, scale = 2)
|
||||
private BigDecimal price;
|
||||
|
||||
@Column(nullable = false)
|
||||
private Integer stock;
|
||||
|
||||
@Column(nullable = false, length = 50)
|
||||
private String category;
|
||||
|
||||
@Column(name = "created_at", nullable = false, updatable = false)
|
||||
private LocalDateTime createdAt;
|
||||
|
||||
@Column(name = "updated_at")
|
||||
private LocalDateTime updatedAt;
|
||||
|
||||
@PrePersist
|
||||
protected void onCreate() {
|
||||
createdAt = LocalDateTime.now();
|
||||
updatedAt = LocalDateTime.now();
|
||||
}
|
||||
|
||||
@PreUpdate
|
||||
protected void onUpdate() {
|
||||
updatedAt = LocalDateTime.now();
|
||||
}
|
||||
}
|
||||
55
src/main/java/com/manalejandro/api_sessions/entity/User.java
Archivo normal
55
src/main/java/com/manalejandro/api_sessions/entity/User.java
Archivo normal
@@ -0,0 +1,55 @@
|
||||
package com.manalejandro.api_sessions.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* User entity representing authenticated users in the system.
|
||||
* Stores user credentials and authentication information.
|
||||
*/
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(unique = true, nullable = false, length = 50)
|
||||
private String username;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String password;
|
||||
|
||||
@Column(nullable = false, length = 100)
|
||||
private String email;
|
||||
|
||||
@Column(nullable = false, length = 20)
|
||||
private String role;
|
||||
|
||||
@Column(name = "created_at", nullable = false, updatable = false)
|
||||
private LocalDateTime createdAt;
|
||||
|
||||
@Column(name = "updated_at")
|
||||
private LocalDateTime updatedAt;
|
||||
|
||||
@PrePersist
|
||||
protected void onCreate() {
|
||||
createdAt = LocalDateTime.now();
|
||||
updatedAt = LocalDateTime.now();
|
||||
}
|
||||
|
||||
@PreUpdate
|
||||
protected void onUpdate() {
|
||||
updatedAt = LocalDateTime.now();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.manalejandro.api_sessions.repository;
|
||||
|
||||
import com.manalejandro.api_sessions.entity.Product;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Repository interface for Product entity.
|
||||
* Provides database access methods for product management.
|
||||
*/
|
||||
@Repository
|
||||
public interface ProductRepository extends JpaRepository<Product, Long> {
|
||||
|
||||
/**
|
||||
* Find products by category.
|
||||
*
|
||||
* @param category the category to filter by
|
||||
* @return a list of products in the specified category
|
||||
*/
|
||||
List<Product> findByCategory(String category);
|
||||
|
||||
/**
|
||||
* Find products with name containing the specified text (case-insensitive).
|
||||
*
|
||||
* @param name the text to search for in product names
|
||||
* @return a list of matching products
|
||||
*/
|
||||
List<Product> findByNameContainingIgnoreCase(String name);
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.manalejandro.api_sessions.repository;
|
||||
|
||||
import com.manalejandro.api_sessions.entity.User;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Repository interface for User entity.
|
||||
* Provides database access methods for user management.
|
||||
*/
|
||||
@Repository
|
||||
public interface UserRepository extends JpaRepository<User, Long> {
|
||||
|
||||
/**
|
||||
* Find a user by username.
|
||||
*
|
||||
* @param username the username to search for
|
||||
* @return an Optional containing the user if found
|
||||
*/
|
||||
Optional<User> findByUsername(String username);
|
||||
|
||||
/**
|
||||
* Check if a user exists by username.
|
||||
*
|
||||
* @param username the username to check
|
||||
* @return true if the user exists, false otherwise
|
||||
*/
|
||||
boolean existsByUsername(String username);
|
||||
|
||||
/**
|
||||
* Check if a user exists by email.
|
||||
*
|
||||
* @param email the email to check
|
||||
* @return true if the user exists, false otherwise
|
||||
*/
|
||||
boolean existsByEmail(String email);
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.manalejandro.api_sessions.security;
|
||||
|
||||
import jakarta.servlet.FilterChain;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* JWT request filter that intercepts incoming requests and validates JWT tokens.
|
||||
* If a valid token is present, it sets up Spring Security authentication.
|
||||
*/
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class JwtRequestFilter extends OncePerRequestFilter {
|
||||
|
||||
private final UserDetailsService userDetailsService;
|
||||
private final JwtTokenUtil jwtTokenUtil;
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
|
||||
throws ServletException, IOException {
|
||||
|
||||
final String authorizationHeader = request.getHeader("Authorization");
|
||||
|
||||
String username = null;
|
||||
String jwt = null;
|
||||
|
||||
// Extract JWT token from Authorization header
|
||||
if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
|
||||
jwt = authorizationHeader.substring(7);
|
||||
try {
|
||||
username = jwtTokenUtil.extractUsername(jwt);
|
||||
} catch (Exception e) {
|
||||
logger.warn("Unable to extract username from JWT token: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// Validate token and set up Spring Security authentication
|
||||
if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
|
||||
UserDetails userDetails = this.userDetailsService.loadUserByUsername(username);
|
||||
|
||||
if (jwtTokenUtil.validateToken(jwt, userDetails)) {
|
||||
UsernamePasswordAuthenticationToken authenticationToken =
|
||||
new UsernamePasswordAuthenticationToken(
|
||||
userDetails, null, userDetails.getAuthorities()
|
||||
);
|
||||
authenticationToken.setDetails(
|
||||
new WebAuthenticationDetailsSource().buildDetails(request)
|
||||
);
|
||||
|
||||
// Set authentication in Spring Security context
|
||||
SecurityContextHolder.getContext().setAuthentication(authenticationToken);
|
||||
}
|
||||
}
|
||||
|
||||
chain.doFilter(request, response);
|
||||
}
|
||||
}
|
||||
139
src/main/java/com/manalejandro/api_sessions/security/JwtTokenUtil.java
Archivo normal
139
src/main/java/com/manalejandro/api_sessions/security/JwtTokenUtil.java
Archivo normal
@@ -0,0 +1,139 @@
|
||||
package com.manalejandro.api_sessions.security;
|
||||
|
||||
import io.jsonwebtoken.Claims;
|
||||
import io.jsonwebtoken.Jwts;
|
||||
import io.jsonwebtoken.security.Keys;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.crypto.SecretKey;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Utility class for JWT token operations.
|
||||
* Handles token generation, validation, and extraction of claims.
|
||||
*/
|
||||
@Component
|
||||
public class JwtTokenUtil {
|
||||
|
||||
@Value("${jwt.secret}")
|
||||
private String secret;
|
||||
|
||||
@Value("${jwt.expiration}")
|
||||
private Long expiration;
|
||||
|
||||
/**
|
||||
* Extract username from JWT token.
|
||||
*
|
||||
* @param token the JWT token
|
||||
* @return the username contained in the token
|
||||
*/
|
||||
public String extractUsername(String token) {
|
||||
return extractClaim(token, Claims::getSubject);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract expiration date from JWT token.
|
||||
*
|
||||
* @param token the JWT token
|
||||
* @return the expiration date
|
||||
*/
|
||||
public Date extractExpiration(String token) {
|
||||
return extractClaim(token, Claims::getExpiration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract a specific claim from JWT token.
|
||||
*
|
||||
* @param token the JWT token
|
||||
* @param claimsResolver the function to extract the claim
|
||||
* @param <T> the type of the claim
|
||||
* @return the extracted claim
|
||||
*/
|
||||
public <T> T extractClaim(String token, Function<Claims, T> claimsResolver) {
|
||||
final Claims claims = extractAllClaims(token);
|
||||
return claimsResolver.apply(claims);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract all claims from JWT token.
|
||||
*
|
||||
* @param token the JWT token
|
||||
* @return all claims contained in the token
|
||||
*/
|
||||
private Claims extractAllClaims(String token) {
|
||||
return Jwts.parser()
|
||||
.verifyWith(getSigningKey())
|
||||
.build()
|
||||
.parseSignedClaims(token)
|
||||
.getPayload();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the JWT token is expired.
|
||||
*
|
||||
* @param token the JWT token
|
||||
* @return true if the token is expired, false otherwise
|
||||
*/
|
||||
private Boolean isTokenExpired(String token) {
|
||||
return extractExpiration(token).before(new Date());
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a JWT token for the given user.
|
||||
*
|
||||
* @param userDetails the user details
|
||||
* @return the generated JWT token
|
||||
*/
|
||||
public String generateToken(UserDetails userDetails) {
|
||||
Map<String, Object> claims = new HashMap<>();
|
||||
return createToken(claims, userDetails.getUsername());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a JWT token with the specified claims and subject.
|
||||
*
|
||||
* @param claims additional claims to include in the token
|
||||
* @param subject the subject (username) of the token
|
||||
* @return the created JWT token
|
||||
*/
|
||||
private String createToken(Map<String, Object> claims, String subject) {
|
||||
Date now = new Date();
|
||||
Date expirationDate = new Date(now.getTime() + expiration);
|
||||
|
||||
return Jwts.builder()
|
||||
.claims(claims)
|
||||
.subject(subject)
|
||||
.issuedAt(now)
|
||||
.expiration(expirationDate)
|
||||
.signWith(getSigningKey())
|
||||
.compact();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the JWT token against the user details.
|
||||
*
|
||||
* @param token the JWT token
|
||||
* @param userDetails the user details to validate against
|
||||
* @return true if the token is valid, false otherwise
|
||||
*/
|
||||
public Boolean validateToken(String token, UserDetails userDetails) {
|
||||
final String username = extractUsername(token);
|
||||
return (username.equals(userDetails.getUsername()) && !isTokenExpired(token));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the signing key for JWT operations.
|
||||
*
|
||||
* @return the secret key
|
||||
*/
|
||||
private SecretKey getSigningKey() {
|
||||
byte[] keyBytes = secret.getBytes(StandardCharsets.UTF_8);
|
||||
return Keys.hmacShaKeyFor(keyBytes);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.manalejandro.api_sessions.service;
|
||||
|
||||
import com.manalejandro.api_sessions.entity.User;
|
||||
import com.manalejandro.api_sessions.repository.UserRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* Custom UserDetailsService implementation for Spring Security.
|
||||
* Loads user details from the database for authentication.
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class CustomUserDetailsService implements UserDetailsService {
|
||||
|
||||
private final UserRepository userRepository;
|
||||
|
||||
@Override
|
||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||
User user = userRepository.findByUsername(username)
|
||||
.orElseThrow(() -> new UsernameNotFoundException("User not found: " + username));
|
||||
|
||||
return new org.springframework.security.core.userdetails.User(
|
||||
user.getUsername(),
|
||||
user.getPassword(),
|
||||
Collections.singletonList(new SimpleGrantedAuthority(user.getRole()))
|
||||
);
|
||||
}
|
||||
}
|
||||
100
src/main/java/com/manalejandro/api_sessions/service/ProductService.java
Archivo normal
100
src/main/java/com/manalejandro/api_sessions/service/ProductService.java
Archivo normal
@@ -0,0 +1,100 @@
|
||||
package com.manalejandro.api_sessions.service;
|
||||
|
||||
import com.manalejandro.api_sessions.entity.Product;
|
||||
import com.manalejandro.api_sessions.repository.ProductRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Service class for Product entity operations.
|
||||
* Handles business logic for product management.
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class ProductService {
|
||||
|
||||
private final ProductRepository productRepository;
|
||||
|
||||
/**
|
||||
* Get all products.
|
||||
*
|
||||
* @return list of all products
|
||||
*/
|
||||
public List<Product> getAllProducts() {
|
||||
return productRepository.findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a product by ID.
|
||||
*
|
||||
* @param id the product ID
|
||||
* @return the product if found
|
||||
*/
|
||||
public Optional<Product> getProductById(Long id) {
|
||||
return productRepository.findById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get products by category.
|
||||
*
|
||||
* @param category the category to filter by
|
||||
* @return list of products in the category
|
||||
*/
|
||||
public List<Product> getProductsByCategory(String category) {
|
||||
return productRepository.findByCategory(category);
|
||||
}
|
||||
|
||||
/**
|
||||
* Search products by name.
|
||||
*
|
||||
* @param name the name to search for
|
||||
* @return list of matching products
|
||||
*/
|
||||
public List<Product> searchProductsByName(String name) {
|
||||
return productRepository.findByNameContainingIgnoreCase(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new product.
|
||||
*
|
||||
* @param product the product to create
|
||||
* @return the created product
|
||||
*/
|
||||
public Product createProduct(Product product) {
|
||||
return productRepository.save(product);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an existing product.
|
||||
*
|
||||
* @param id the product ID
|
||||
* @param productDetails the updated product details
|
||||
* @return the updated product if found
|
||||
*/
|
||||
public Optional<Product> updateProduct(Long id, Product productDetails) {
|
||||
return productRepository.findById(id).map(product -> {
|
||||
product.setName(productDetails.getName());
|
||||
product.setDescription(productDetails.getDescription());
|
||||
product.setPrice(productDetails.getPrice());
|
||||
product.setStock(productDetails.getStock());
|
||||
product.setCategory(productDetails.getCategory());
|
||||
return productRepository.save(product);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a product by ID.
|
||||
*
|
||||
* @param id the product ID
|
||||
* @return true if the product was deleted, false otherwise
|
||||
*/
|
||||
public boolean deleteProduct(Long id) {
|
||||
return productRepository.findById(id).map(product -> {
|
||||
productRepository.delete(product);
|
||||
return true;
|
||||
}).orElse(false);
|
||||
}
|
||||
}
|
||||
28
src/main/resources/application.properties
Archivo normal
28
src/main/resources/application.properties
Archivo normal
@@ -0,0 +1,28 @@
|
||||
spring.application.name=api-sessions
|
||||
|
||||
# Server Configuration
|
||||
server.port=8080
|
||||
|
||||
# H2 Database Configuration
|
||||
spring.datasource.url=jdbc:h2:mem:testdb
|
||||
spring.datasource.driverClassName=org.h2.Driver
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
|
||||
# JPA/Hibernate Configuration
|
||||
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
|
||||
spring.jpa.hibernate.ddl-auto=create-drop
|
||||
spring.jpa.show-sql=true
|
||||
spring.jpa.properties.hibernate.format_sql=true
|
||||
|
||||
# H2 Console Configuration (accessible at http://localhost:8080/h2-console)
|
||||
spring.h2.console.enabled=true
|
||||
spring.h2.console.path=/h2-console
|
||||
|
||||
# JWT Configuration
|
||||
jwt.secret=MySecretKeyForJWTTokenGenerationAndValidationMustBeAtLeast256BitsLong
|
||||
jwt.expiration=86400000
|
||||
|
||||
# Logging Configuration
|
||||
logging.level.com.manalejandro.api_sessions=DEBUG
|
||||
logging.level.org.springframework.security=DEBUG
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.manalejandro.api_sessions;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class ApiSessionsApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
312
test-api.sh
Archivo ejecutable
312
test-api.sh
Archivo ejecutable
@@ -0,0 +1,312 @@
|
||||
#!/bin/sh
|
||||
|
||||
# API Testing Script for Product CRUD Operations
|
||||
# This script tests all endpoints of the API with JWT authentication
|
||||
|
||||
BASE_URL="http://localhost:8080"
|
||||
TOKEN=""
|
||||
|
||||
# Colors for output
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
# Print colored output
|
||||
print_header() {
|
||||
echo "${BLUE}================================================${NC}"
|
||||
echo "${BLUE}$1${NC}"
|
||||
echo "${BLUE}================================================${NC}"
|
||||
}
|
||||
|
||||
print_success() {
|
||||
echo "${GREEN}✓ $1${NC}"
|
||||
}
|
||||
|
||||
print_error() {
|
||||
echo "${RED}✗ $1${NC}"
|
||||
}
|
||||
|
||||
print_info() {
|
||||
echo "${YELLOW}→ $1${NC}"
|
||||
}
|
||||
|
||||
# Function to pretty print JSON
|
||||
print_json() {
|
||||
if command -v python3 >/dev/null 2>&1; then
|
||||
echo "$1" | python3 -m json.tool 2>/dev/null || echo "$1"
|
||||
else
|
||||
echo "$1"
|
||||
fi
|
||||
}
|
||||
|
||||
# Test 1: Login and get JWT token
|
||||
test_login() {
|
||||
print_header "TEST 1: LOGIN - Get JWT Token"
|
||||
|
||||
print_info "Attempting to login with username: admin, password: admin123"
|
||||
|
||||
RESPONSE=$(curl -s -X POST "$BASE_URL/api/auth/login" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"username":"admin","password":"admin123"}')
|
||||
|
||||
echo "Response:"
|
||||
print_json "$RESPONSE"
|
||||
|
||||
TOKEN=$(echo "$RESPONSE" | grep -o '"token":"[^"]*' | cut -d'"' -f4)
|
||||
|
||||
if [ -n "$TOKEN" ]; then
|
||||
print_success "Login successful! Token obtained."
|
||||
echo "Token: $TOKEN"
|
||||
else
|
||||
print_error "Login failed! No token received."
|
||||
exit 1
|
||||
fi
|
||||
echo ""
|
||||
}
|
||||
|
||||
# Test 2: Check authentication status
|
||||
test_auth_status() {
|
||||
print_header "TEST 2: CHECK AUTHENTICATION STATUS"
|
||||
|
||||
RESPONSE=$(curl -s -X GET "$BASE_URL/api/auth/status" \
|
||||
-H "Authorization: Bearer $TOKEN")
|
||||
|
||||
echo "Response:"
|
||||
print_json "$RESPONSE"
|
||||
print_success "Authentication status checked"
|
||||
echo ""
|
||||
}
|
||||
|
||||
# Test 3: Get all products
|
||||
test_get_all_products() {
|
||||
print_header "TEST 3: GET ALL PRODUCTS"
|
||||
|
||||
RESPONSE=$(curl -s -X GET "$BASE_URL/api/products" \
|
||||
-H "Authorization: Bearer $TOKEN")
|
||||
|
||||
echo "Response:"
|
||||
print_json "$RESPONSE"
|
||||
print_success "Retrieved all products"
|
||||
echo ""
|
||||
}
|
||||
|
||||
# Test 4: Get product by ID
|
||||
test_get_product_by_id() {
|
||||
print_header "TEST 4: GET PRODUCT BY ID (ID=1)"
|
||||
|
||||
RESPONSE=$(curl -s -X GET "$BASE_URL/api/products/1" \
|
||||
-H "Authorization: Bearer $TOKEN")
|
||||
|
||||
echo "Response:"
|
||||
print_json "$RESPONSE"
|
||||
print_success "Retrieved product with ID 1"
|
||||
echo ""
|
||||
}
|
||||
|
||||
# Test 5: Search products by name
|
||||
test_search_products() {
|
||||
print_header "TEST 5: SEARCH PRODUCTS (name=laptop)"
|
||||
|
||||
RESPONSE=$(curl -s -X GET "$BASE_URL/api/products/search?name=laptop" \
|
||||
-H "Authorization: Bearer $TOKEN")
|
||||
|
||||
echo "Response:"
|
||||
print_json "$RESPONSE"
|
||||
print_success "Search completed"
|
||||
echo ""
|
||||
}
|
||||
|
||||
# Test 6: Get products by category
|
||||
test_get_by_category() {
|
||||
print_header "TEST 6: GET PRODUCTS BY CATEGORY (Electronics)"
|
||||
|
||||
RESPONSE=$(curl -s -X GET "$BASE_URL/api/products/category/Electronics" \
|
||||
-H "Authorization: Bearer $TOKEN")
|
||||
|
||||
echo "Response:"
|
||||
print_json "$RESPONSE"
|
||||
print_success "Retrieved products in Electronics category"
|
||||
echo ""
|
||||
}
|
||||
|
||||
# Test 7: Create a new product
|
||||
test_create_product() {
|
||||
print_header "TEST 7: CREATE NEW PRODUCT"
|
||||
|
||||
print_info "Creating product: Gaming Console PlayStation 5"
|
||||
|
||||
RESPONSE=$(curl -s -X POST "$BASE_URL/api/products" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{
|
||||
"name": "Gaming Console PlayStation 5",
|
||||
"description": "Next-gen gaming console with 4K support",
|
||||
"price": 499.99,
|
||||
"stock": 10,
|
||||
"category": "Gaming"
|
||||
}')
|
||||
|
||||
echo "Response:"
|
||||
print_json "$RESPONSE"
|
||||
print_success "Product created successfully"
|
||||
echo ""
|
||||
}
|
||||
|
||||
# Test 8: Update a product
|
||||
test_update_product() {
|
||||
print_header "TEST 8: UPDATE PRODUCT (ID=1)"
|
||||
|
||||
print_info "Updating product ID 1: Changing price and stock"
|
||||
|
||||
RESPONSE=$(curl -s -X PUT "$BASE_URL/api/products/1" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{
|
||||
"name": "Laptop Dell XPS 15 (Updated)",
|
||||
"description": "High-performance laptop with 32GB RAM and 1TB SSD - UPDATED!",
|
||||
"price": 1499.99,
|
||||
"stock": 20,
|
||||
"category": "Electronics"
|
||||
}')
|
||||
|
||||
echo "Response:"
|
||||
print_json "$RESPONSE"
|
||||
print_success "Product updated successfully"
|
||||
echo ""
|
||||
}
|
||||
|
||||
# Test 9: Get updated product to verify changes
|
||||
test_verify_update() {
|
||||
print_header "TEST 9: VERIFY UPDATE (Get Product ID=1)"
|
||||
|
||||
RESPONSE=$(curl -s -X GET "$BASE_URL/api/products/1" \
|
||||
-H "Authorization: Bearer $TOKEN")
|
||||
|
||||
echo "Response:"
|
||||
print_json "$RESPONSE"
|
||||
print_success "Verified product update"
|
||||
echo ""
|
||||
}
|
||||
|
||||
# Test 10: Get all products after creation
|
||||
test_get_all_after_create() {
|
||||
print_header "TEST 10: GET ALL PRODUCTS (After Create)"
|
||||
|
||||
RESPONSE=$(curl -s -X GET "$BASE_URL/api/products" \
|
||||
-H "Authorization: Bearer $TOKEN")
|
||||
|
||||
echo "Response:"
|
||||
print_json "$RESPONSE"
|
||||
print_success "Retrieved all products (should see new product)"
|
||||
echo ""
|
||||
}
|
||||
|
||||
# Test 11: Delete a product
|
||||
test_delete_product() {
|
||||
print_header "TEST 11: DELETE PRODUCT (ID=9)"
|
||||
|
||||
print_info "Deleting the newly created product (ID=9)"
|
||||
|
||||
RESPONSE=$(curl -s -X DELETE "$BASE_URL/api/products/9" \
|
||||
-H "Authorization: Bearer $TOKEN")
|
||||
|
||||
echo "Response:"
|
||||
echo "$RESPONSE"
|
||||
print_success "Product deleted successfully"
|
||||
echo ""
|
||||
}
|
||||
|
||||
# Test 12: Verify deletion
|
||||
test_verify_deletion() {
|
||||
print_header "TEST 12: VERIFY DELETION (Try to get deleted product ID=9)"
|
||||
|
||||
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" -X GET "$BASE_URL/api/products/9" \
|
||||
-H "Authorization: Bearer $TOKEN")
|
||||
|
||||
echo "HTTP Status Code: $HTTP_CODE"
|
||||
|
||||
if [ "$HTTP_CODE" = "404" ]; then
|
||||
print_success "Product successfully deleted (404 Not Found)"
|
||||
else
|
||||
print_error "Product may still exist (Status: $HTTP_CODE)"
|
||||
fi
|
||||
echo ""
|
||||
}
|
||||
|
||||
# Test 13: Get all products after deletion
|
||||
test_get_all_final() {
|
||||
print_header "TEST 13: GET ALL PRODUCTS (Final State)"
|
||||
|
||||
RESPONSE=$(curl -s -X GET "$BASE_URL/api/products" \
|
||||
-H "Authorization: Bearer $TOKEN")
|
||||
|
||||
echo "Response:"
|
||||
print_json "$RESPONSE"
|
||||
print_success "Retrieved final product list"
|
||||
echo ""
|
||||
}
|
||||
|
||||
# Test 14: Test authentication failure (without token)
|
||||
test_unauthorized_access() {
|
||||
print_header "TEST 14: TEST UNAUTHORIZED ACCESS (No Token)"
|
||||
|
||||
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" -X GET "$BASE_URL/api/products")
|
||||
|
||||
echo "HTTP Status Code: $HTTP_CODE"
|
||||
|
||||
if [ "$HTTP_CODE" = "401" ] || [ "$HTTP_CODE" = "403" ]; then
|
||||
print_success "Access correctly denied without token (Status: $HTTP_CODE)"
|
||||
else
|
||||
print_error "Unexpected status code: $HTTP_CODE"
|
||||
fi
|
||||
echo ""
|
||||
}
|
||||
|
||||
# Main execution
|
||||
main() {
|
||||
echo "${GREEN}"
|
||||
echo "╔═══════════════════════════════════════════════════════════╗"
|
||||
echo "║ API SESSIONS - CRUD TEST SUITE ║"
|
||||
echo "║ Testing JWT Authentication & Product API ║"
|
||||
echo "╚═══════════════════════════════════════════════════════════╝"
|
||||
echo "${NC}"
|
||||
echo ""
|
||||
|
||||
print_info "Starting API tests..."
|
||||
print_info "Base URL: $BASE_URL"
|
||||
echo ""
|
||||
|
||||
# Run all tests
|
||||
test_login
|
||||
test_auth_status
|
||||
test_get_all_products
|
||||
test_get_product_by_id
|
||||
test_search_products
|
||||
test_get_by_category
|
||||
test_create_product
|
||||
test_update_product
|
||||
test_verify_update
|
||||
test_get_all_after_create
|
||||
test_delete_product
|
||||
test_verify_deletion
|
||||
test_get_all_final
|
||||
test_unauthorized_access
|
||||
|
||||
print_header "ALL TESTS COMPLETED!"
|
||||
echo "${GREEN}All CRUD operations have been tested successfully!${NC}"
|
||||
echo ""
|
||||
echo "${YELLOW}Summary of tested operations:${NC}"
|
||||
echo " ✓ Authentication (Login)"
|
||||
echo " ✓ Authorization (JWT Token)"
|
||||
echo " ✓ CREATE - New product"
|
||||
echo " ✓ READ - All products, by ID, search, by category"
|
||||
echo " ✓ UPDATE - Existing product"
|
||||
echo " ✓ DELETE - Product removal"
|
||||
echo " ✓ Security - Unauthorized access protection"
|
||||
echo ""
|
||||
}
|
||||
|
||||
# Run main function
|
||||
main
|
||||
Referencia en una nueva incidencia
Block a user