|
| 1 | +# Jazzy Framework - Authentication Example |
| 2 | + |
| 3 | +Bu örnek uygulama, `@EnableJazzyAuth` annotation'ının nasıl kullanılacağını gösterir. |
| 4 | + |
| 5 | +## Özellikler |
| 6 | + |
| 7 | +- 📧 **Email tabanlı authentication** |
| 8 | +- 🔐 **JWT token sistemi** |
| 9 | +- 🚀 **Zero-configuration setup** |
| 10 | +- 📝 **Otomatik endpoint'ler** |
| 11 | + |
| 12 | +## Otomatik Oluşturulan Endpoint'ler |
| 13 | + |
| 14 | +`@EnableJazzyAuth` annotation'ı otomatik olarak şu endpoint'leri oluşturur: |
| 15 | + |
| 16 | +### 1. Kullanıcı Kaydı |
| 17 | +``` |
| 18 | +POST /api/auth/register |
| 19 | +Content-Type: application/json |
| 20 | +
|
| 21 | +{ |
| 22 | + |
| 23 | + "password": "password123", |
| 24 | + "name": "John Doe" |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +**Başarılı Yanıt:** |
| 29 | +```json |
| 30 | +{ |
| 31 | + "message": "User registered successfully", |
| 32 | + "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...", |
| 33 | + "user": { |
| 34 | + "id": "1", |
| 35 | + |
| 36 | + } |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +### 2. Kullanıcı Girişi |
| 41 | +``` |
| 42 | +POST /api/auth/login |
| 43 | +Content-Type: application/json |
| 44 | +
|
| 45 | +{ |
| 46 | + |
| 47 | + "password": "password123" |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +**Başarılı Yanıt:** |
| 52 | +```json |
| 53 | +{ |
| 54 | + "message": "Login successful", |
| 55 | + "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...", |
| 56 | + "user": { |
| 57 | + "id": "1", |
| 58 | + |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +### 3. Mevcut Kullanıcı Bilgisi |
| 64 | +``` |
| 65 | +GET /api/auth/me |
| 66 | +Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9... |
| 67 | +``` |
| 68 | + |
| 69 | +**Başarılı Yanıt:** |
| 70 | +```json |
| 71 | +{ |
| 72 | + "user": { |
| 73 | + "id": "1", |
| 74 | + |
| 75 | + "username": null |
| 76 | + } |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +## Nasıl Çalıştırılır |
| 81 | + |
| 82 | +1. **Uygulamayı başlatın:** |
| 83 | + ```bash |
| 84 | + javac -cp "lib/*" src/main/java/examples/auth_example/*.java |
| 85 | + java -cp "lib/*:src/main/java" examples.auth_example.AuthExampleApp |
| 86 | + ``` |
| 87 | + |
| 88 | +2. **Test edin:** |
| 89 | + ```bash |
| 90 | + # Kullanıcı kaydı |
| 91 | + curl -X POST http://localhost:8080/api/auth/register \ |
| 92 | + -H "Content-Type: application/json" \ |
| 93 | + -d '{"email":"[email protected]","password":"123456","name":"Test User"}' |
| 94 | + |
| 95 | + # Giriş yapın |
| 96 | + curl -X POST http://localhost:8080/api/auth/login \ |
| 97 | + -H "Content-Type: application/json" \ |
| 98 | + -d '{"email":"[email protected]","password":"123456"}' |
| 99 | + |
| 100 | + # Token ile kullanıcı bilgisi alın |
| 101 | + curl -X GET http://localhost:8080/api/auth/me \ |
| 102 | + -H "Authorization: Bearer YOUR_TOKEN_HERE" |
| 103 | + ``` |
| 104 | + |
| 105 | +## Configuration |
| 106 | + |
| 107 | +`@EnableJazzyAuth` annotation'ı şu parametreleri destekler: |
| 108 | + |
| 109 | +```java |
| 110 | +@EnableJazzyAuth( |
| 111 | + userClass = User.class, // Kullanıcı entity class'ı |
| 112 | + repositoryClass = UserRepository.class, // Kullanıcı repository interface'i |
| 113 | + loginMethod = LoginMethod.EMAIL, // EMAIL, USERNAME, veya BOTH |
| 114 | + jwtExpirationHours = 24, // JWT token süresi (saat) |
| 115 | + authBasePath = "/api/auth" // Auth endpoint'lerinin base path'i |
| 116 | +) |
| 117 | +``` |
| 118 | + |
| 119 | +## Gereksinimler |
| 120 | + |
| 121 | +### User Entity |
| 122 | + |
| 123 | +User class'ınız şu field'lara sahip olmalı: |
| 124 | + |
| 125 | +- `id` (Long) - Kullanıcı ID'si |
| 126 | +- `email` (String) - Email adresi (EMAIL veya BOTH login method için) |
| 127 | +- `username` (String) - Kullanıcı adı (USERNAME veya BOTH login method için) |
| 128 | +- `password` (String) - Şifre (otomatik hash'lenir) |
| 129 | + |
| 130 | +### User Repository |
| 131 | + |
| 132 | +User repository interface'iniz `BaseRepository`'yi extend etmelidir: |
| 133 | + |
| 134 | +```java |
| 135 | +public interface UserRepository extends BaseRepository<User, Long> { |
| 136 | + // İsteğe bağlı özel query methodları ekleyebilirsiniz |
| 137 | + // Optional<User> findByEmail(String email); |
| 138 | + // Optional<User> findByUsername(String username); |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +**Not:** Repository interface'i otomatik olarak Jazzy Framework'ün DI container'ında register edilir. |
| 143 | + |
| 144 | +## Güvenlik Konfigürasyonu (Opsiyonel) |
| 145 | + |
| 146 | +Endpoint'leri korumak için `SecurityConfig` class'ı oluşturabilirsiniz: |
| 147 | + |
| 148 | +```java |
| 149 | +@Component |
| 150 | +public class AppSecurityConfig extends SecurityConfig { |
| 151 | + |
| 152 | + @Override |
| 153 | + public void configure() { |
| 154 | + // Public endpoint'ler (authentication gerektirmez) |
| 155 | + publicEndpoints( |
| 156 | + "/", // Ana sayfa |
| 157 | + "/api/auth/**" // Tüm auth endpoint'leri |
| 158 | + ); |
| 159 | + |
| 160 | + // Secure endpoint'ler (authentication gerektirir) |
| 161 | + requireAuth( |
| 162 | + "/api/protected", // Korumalı endpoint |
| 163 | + "/api/user/**" // Kullanıcıya özel endpoint'ler |
| 164 | + ); |
| 165 | + |
| 166 | + // Admin endpoint'leri (ADMIN rolü gerektirir) |
| 167 | + requireRole("ADMIN", |
| 168 | + "/api/admin/**" // Admin endpoint'leri |
| 169 | + ); |
| 170 | + } |
| 171 | +} |
| 172 | +``` |
| 173 | + |
| 174 | +### Wildcard Destek |
| 175 | + |
| 176 | +SecurityConfig şu wildcard'ları destekler: |
| 177 | + |
| 178 | +- `*` - Tek path segment'i ile eşleşir |
| 179 | +- `**` - Herhangi sayıda path segment'i ile eşleşir |
| 180 | + |
| 181 | +**Örnekler:** |
| 182 | +- `/api/auth/**` → `/api/auth/login`, `/api/auth/register`, `/api/auth/user/profile` ile eşleşir |
| 183 | +- `/user/*` → `/user/123` ile eşleşir ama `/user/123/profile` ile eşleşmez |
| 184 | + |
| 185 | +## Avantajları |
| 186 | + |
| 187 | +- ✅ **Zero Configuration** - Sadece annotation ekleyin |
| 188 | +- ✅ **Otomatik JWT** - Token üretimi ve doğrulama otomatik |
| 189 | +- ✅ **Flexible User Entity** - Kendi User class'ınızı kullanın |
| 190 | +- ✅ **Multiple Login Methods** - Email, username veya her ikisi |
| 191 | +- ✅ **Standard Java** - External dependency yok |
0 commit comments