@chc880/everything-antigravity 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +54 -0
  3. package/assets/rules/common/coding-style.md +53 -0
  4. package/assets/rules/common/git-workflow.md +47 -0
  5. package/assets/rules/common/patterns.md +36 -0
  6. package/assets/rules/common/performance.md +21 -0
  7. package/assets/rules/common/security.md +34 -0
  8. package/assets/rules/common/testing.md +29 -0
  9. package/assets/rules/golang/coding-style.md +40 -0
  10. package/assets/rules/golang/patterns.md +44 -0
  11. package/assets/rules/golang/security.md +33 -0
  12. package/assets/rules/golang/testing.md +30 -0
  13. package/assets/rules/python/coding-style.md +52 -0
  14. package/assets/rules/python/patterns.md +39 -0
  15. package/assets/rules/python/security.md +30 -0
  16. package/assets/rules/python/testing.md +38 -0
  17. package/assets/rules/typescript/coding-style.md +44 -0
  18. package/assets/rules/typescript/patterns.md +50 -0
  19. package/assets/rules/typescript/security.md +27 -0
  20. package/assets/rules/typescript/testing.md +24 -0
  21. package/assets/skills/agent-guides/SKILL.md +40 -0
  22. package/assets/skills/agent-guides/references/architect.md +209 -0
  23. package/assets/skills/agent-guides/references/build-error-resolver.md +530 -0
  24. package/assets/skills/agent-guides/references/code-reviewer.md +102 -0
  25. package/assets/skills/agent-guides/references/database-reviewer.md +652 -0
  26. package/assets/skills/agent-guides/references/doc-updater.md +450 -0
  27. package/assets/skills/agent-guides/references/e2e-runner.md +795 -0
  28. package/assets/skills/agent-guides/references/go-build-resolver.md +366 -0
  29. package/assets/skills/agent-guides/references/go-reviewer.md +265 -0
  30. package/assets/skills/agent-guides/references/planner.md +117 -0
  31. package/assets/skills/agent-guides/references/python-reviewer.md +467 -0
  32. package/assets/skills/agent-guides/references/refactor-cleaner.md +304 -0
  33. package/assets/skills/agent-guides/references/security-reviewer.md +543 -0
  34. package/assets/skills/agent-guides/references/tdd-guide.md +278 -0
  35. package/assets/skills/backend-patterns/SKILL.md +587 -0
  36. package/assets/skills/clickhouse-io/SKILL.md +429 -0
  37. package/assets/skills/coding-standards/SKILL.md +520 -0
  38. package/assets/skills/cpp-testing/SKILL.md +322 -0
  39. package/assets/skills/django-patterns/SKILL.md +733 -0
  40. package/assets/skills/django-security/SKILL.md +592 -0
  41. package/assets/skills/django-tdd/SKILL.md +728 -0
  42. package/assets/skills/django-verification/SKILL.md +460 -0
  43. package/assets/skills/frontend-patterns/SKILL.md +631 -0
  44. package/assets/skills/golang-patterns/SKILL.md +673 -0
  45. package/assets/skills/golang-testing/SKILL.md +719 -0
  46. package/assets/skills/java-coding-standards/SKILL.md +138 -0
  47. package/assets/skills/jpa-patterns/SKILL.md +141 -0
  48. package/assets/skills/knowledge-management/SKILL.md +77 -0
  49. package/assets/skills/nutrient-document-processing/SKILL.md +165 -0
  50. package/assets/skills/postgres-patterns/SKILL.md +146 -0
  51. package/assets/skills/python-patterns/SKILL.md +749 -0
  52. package/assets/skills/python-testing/SKILL.md +815 -0
  53. package/assets/skills/security-hardening/SKILL.md +76 -0
  54. package/assets/skills/security-review/SKILL.md +494 -0
  55. package/assets/skills/security-review/cloud-infrastructure-security.md +361 -0
  56. package/assets/skills/springboot-patterns/SKILL.md +304 -0
  57. package/assets/skills/springboot-security/SKILL.md +119 -0
  58. package/assets/skills/springboot-tdd/SKILL.md +157 -0
  59. package/assets/skills/springboot-verification/SKILL.md +100 -0
  60. package/assets/skills/tdd-workflow/SKILL.md +409 -0
  61. package/assets/workflows/build-fix.md +50 -0
  62. package/assets/workflows/code-review.md +61 -0
  63. package/assets/workflows/e2e.md +65 -0
  64. package/assets/workflows/go-build.md +39 -0
  65. package/assets/workflows/go-review.md +44 -0
  66. package/assets/workflows/go-test.md +61 -0
  67. package/assets/workflows/plan.md +93 -0
  68. package/assets/workflows/python-review.md +95 -0
  69. package/assets/workflows/setup-pm.md +36 -0
  70. package/assets/workflows/tdd.md +75 -0
  71. package/assets/workflows/verify.md +81 -0
  72. package/bin/cli.js +69 -0
  73. package/lib/installer.js +301 -0
  74. package/package.json +34 -0
@@ -0,0 +1,361 @@
1
+ | name | description |
2
+ |------|-------------|
3
+ | cloud-infrastructure-security | Use this skill when deploying to cloud platforms, configuring infrastructure, managing IAM policies, setting up logging/monitoring, or implementing CI/CD pipelines. Provides cloud security checklist aligned with best practices. |
4
+
5
+ # Cloud & Infrastructure Security Skill
6
+
7
+ This skill ensures cloud infrastructure, CI/CD pipelines, and deployment configurations follow security best practices and comply with industry standards.
8
+
9
+ ## When to Activate
10
+
11
+ - Deploying applications to cloud platforms (AWS, Vercel, Railway, Cloudflare)
12
+ - Configuring IAM roles and permissions
13
+ - Setting up CI/CD pipelines
14
+ - Implementing infrastructure as code (Terraform, CloudFormation)
15
+ - Configuring logging and monitoring
16
+ - Managing secrets in cloud environments
17
+ - Setting up CDN and edge security
18
+ - Implementing disaster recovery and backup strategies
19
+
20
+ ## Cloud Security Checklist
21
+
22
+ ### 1. IAM & Access Control
23
+
24
+ #### Principle of Least Privilege
25
+
26
+ ```yaml
27
+ # ✅ CORRECT: Minimal permissions
28
+ iam_role:
29
+ permissions:
30
+ - s3:GetObject # Only read access
31
+ - s3:ListBucket
32
+ resources:
33
+ - arn:aws:s3:::my-bucket/* # Specific bucket only
34
+
35
+ # ❌ WRONG: Overly broad permissions
36
+ iam_role:
37
+ permissions:
38
+ - s3:* # All S3 actions
39
+ resources:
40
+ - "*" # All resources
41
+ ```
42
+
43
+ #### Multi-Factor Authentication (MFA)
44
+
45
+ ```bash
46
+ # ALWAYS enable MFA for root/admin accounts
47
+ aws iam enable-mfa-device \
48
+ --user-name admin \
49
+ --serial-number arn:aws:iam::123456789:mfa/admin \
50
+ --authentication-code1 123456 \
51
+ --authentication-code2 789012
52
+ ```
53
+
54
+ #### Verification Steps
55
+
56
+ - [ ] No root account usage in production
57
+ - [ ] MFA enabled for all privileged accounts
58
+ - [ ] Service accounts use roles, not long-lived credentials
59
+ - [ ] IAM policies follow least privilege
60
+ - [ ] Regular access reviews conducted
61
+ - [ ] Unused credentials rotated or removed
62
+
63
+ ### 2. Secrets Management
64
+
65
+ #### Cloud Secrets Managers
66
+
67
+ ```typescript
68
+ // ✅ CORRECT: Use cloud secrets manager
69
+ import { SecretsManager } from '@aws-sdk/client-secrets-manager';
70
+
71
+ const client = new SecretsManager({ region: 'us-east-1' });
72
+ const secret = await client.getSecretValue({ SecretId: 'prod/api-key' });
73
+ const apiKey = JSON.parse(secret.SecretString).key;
74
+
75
+ // ❌ WRONG: Hardcoded or in environment variables only
76
+ const apiKey = process.env.API_KEY; // Not rotated, not audited
77
+ ```
78
+
79
+ #### Secrets Rotation
80
+
81
+ ```bash
82
+ # Set up automatic rotation for database credentials
83
+ aws secretsmanager rotate-secret \
84
+ --secret-id prod/db-password \
85
+ --rotation-lambda-arn arn:aws:lambda:region:account:function:rotate \
86
+ --rotation-rules AutomaticallyAfterDays=30
87
+ ```
88
+
89
+ #### Verification Steps
90
+
91
+ - [ ] All secrets stored in cloud secrets manager (AWS Secrets Manager, Vercel Secrets)
92
+ - [ ] Automatic rotation enabled for database credentials
93
+ - [ ] API keys rotated at least quarterly
94
+ - [ ] No secrets in code, logs, or error messages
95
+ - [ ] Audit logging enabled for secret access
96
+
97
+ ### 3. Network Security
98
+
99
+ #### VPC and Firewall Configuration
100
+
101
+ ```terraform
102
+ # ✅ CORRECT: Restricted security group
103
+ resource "aws_security_group" "app" {
104
+ name = "app-sg"
105
+
106
+ ingress {
107
+ from_port = 443
108
+ to_port = 443
109
+ protocol = "tcp"
110
+ cidr_blocks = ["10.0.0.0/16"] # Internal VPC only
111
+ }
112
+
113
+ egress {
114
+ from_port = 443
115
+ to_port = 443
116
+ protocol = "tcp"
117
+ cidr_blocks = ["0.0.0.0/0"] # Only HTTPS outbound
118
+ }
119
+ }
120
+
121
+ # ❌ WRONG: Open to the internet
122
+ resource "aws_security_group" "bad" {
123
+ ingress {
124
+ from_port = 0
125
+ to_port = 65535
126
+ protocol = "tcp"
127
+ cidr_blocks = ["0.0.0.0/0"] # All ports, all IPs!
128
+ }
129
+ }
130
+ ```
131
+
132
+ #### Verification Steps
133
+
134
+ - [ ] Database not publicly accessible
135
+ - [ ] SSH/RDP ports restricted to VPN/bastion only
136
+ - [ ] Security groups follow least privilege
137
+ - [ ] Network ACLs configured
138
+ - [ ] VPC flow logs enabled
139
+
140
+ ### 4. Logging & Monitoring
141
+
142
+ #### CloudWatch/Logging Configuration
143
+
144
+ ```typescript
145
+ // ✅ CORRECT: Comprehensive logging
146
+ import { CloudWatchLogsClient, CreateLogStreamCommand } from '@aws-sdk/client-cloudwatch-logs';
147
+
148
+ const logSecurityEvent = async (event: SecurityEvent) => {
149
+ await cloudwatch.putLogEvents({
150
+ logGroupName: '/aws/security/events',
151
+ logStreamName: 'authentication',
152
+ logEvents: [{
153
+ timestamp: Date.now(),
154
+ message: JSON.stringify({
155
+ type: event.type,
156
+ userId: event.userId,
157
+ ip: event.ip,
158
+ result: event.result,
159
+ // Never log sensitive data
160
+ })
161
+ }]
162
+ });
163
+ };
164
+ ```
165
+
166
+ #### Verification Steps
167
+
168
+ - [ ] CloudWatch/logging enabled for all services
169
+ - [ ] Failed authentication attempts logged
170
+ - [ ] Admin actions audited
171
+ - [ ] Log retention configured (90+ days for compliance)
172
+ - [ ] Alerts configured for suspicious activity
173
+ - [ ] Logs centralized and tamper-proof
174
+
175
+ ### 5. CI/CD Pipeline Security
176
+
177
+ #### Secure Pipeline Configuration
178
+
179
+ ```yaml
180
+ # ✅ CORRECT: Secure GitHub Actions workflow
181
+ name: Deploy
182
+
183
+ on:
184
+ push:
185
+ branches: [main]
186
+
187
+ jobs:
188
+ deploy:
189
+ runs-on: ubuntu-latest
190
+ permissions:
191
+ contents: read # Minimal permissions
192
+
193
+ steps:
194
+ - uses: actions/checkout@v4
195
+
196
+ # Scan for secrets
197
+ - name: Secret scanning
198
+ uses: trufflesecurity/trufflehog@main
199
+
200
+ # Dependency audit
201
+ - name: Audit dependencies
202
+ run: npm audit --audit-level=high
203
+
204
+ # Use OIDC, not long-lived tokens
205
+ - name: Configure AWS credentials
206
+ uses: aws-actions/configure-aws-credentials@v4
207
+ with:
208
+ role-to-assume: arn:aws:iam::123456789:role/GitHubActionsRole
209
+ aws-region: us-east-1
210
+ ```
211
+
212
+ #### Supply Chain Security
213
+
214
+ ```json
215
+ // package.json - Use lock files and integrity checks
216
+ {
217
+ "scripts": {
218
+ "install": "npm ci", // Use ci for reproducible builds
219
+ "audit": "npm audit --audit-level=moderate",
220
+ "check": "npm outdated"
221
+ }
222
+ }
223
+ ```
224
+
225
+ #### Verification Steps
226
+
227
+ - [ ] OIDC used instead of long-lived credentials
228
+ - [ ] Secrets scanning in pipeline
229
+ - [ ] Dependency vulnerability scanning
230
+ - [ ] Container image scanning (if applicable)
231
+ - [ ] Branch protection rules enforced
232
+ - [ ] Code review required before merge
233
+ - [ ] Signed commits enforced
234
+
235
+ ### 6. Cloudflare & CDN Security
236
+
237
+ #### Cloudflare Security Configuration
238
+
239
+ ```typescript
240
+ // ✅ CORRECT: Cloudflare Workers with security headers
241
+ export default {
242
+ async fetch(request: Request): Promise<Response> {
243
+ const response = await fetch(request);
244
+
245
+ // Add security headers
246
+ const headers = new Headers(response.headers);
247
+ headers.set('X-Frame-Options', 'DENY');
248
+ headers.set('X-Content-Type-Options', 'nosniff');
249
+ headers.set('Referrer-Policy', 'strict-origin-when-cross-origin');
250
+ headers.set('Permissions-Policy', 'geolocation=(), microphone=()');
251
+
252
+ return new Response(response.body, {
253
+ status: response.status,
254
+ headers
255
+ });
256
+ }
257
+ };
258
+ ```
259
+
260
+ #### WAF Rules
261
+
262
+ ```bash
263
+ # Enable Cloudflare WAF managed rules
264
+ # - OWASP Core Ruleset
265
+ # - Cloudflare Managed Ruleset
266
+ # - Rate limiting rules
267
+ # - Bot protection
268
+ ```
269
+
270
+ #### Verification Steps
271
+
272
+ - [ ] WAF enabled with OWASP rules
273
+ - [ ] Rate limiting configured
274
+ - [ ] Bot protection active
275
+ - [ ] DDoS protection enabled
276
+ - [ ] Security headers configured
277
+ - [ ] SSL/TLS strict mode enabled
278
+
279
+ ### 7. Backup & Disaster Recovery
280
+
281
+ #### Automated Backups
282
+
283
+ ```terraform
284
+ # ✅ CORRECT: Automated RDS backups
285
+ resource "aws_db_instance" "main" {
286
+ allocated_storage = 20
287
+ engine = "postgres"
288
+
289
+ backup_retention_period = 30 # 30 days retention
290
+ backup_window = "03:00-04:00"
291
+ maintenance_window = "mon:04:00-mon:05:00"
292
+
293
+ enabled_cloudwatch_logs_exports = ["postgresql"]
294
+
295
+ deletion_protection = true # Prevent accidental deletion
296
+ }
297
+ ```
298
+
299
+ #### Verification Steps
300
+
301
+ - [ ] Automated daily backups configured
302
+ - [ ] Backup retention meets compliance requirements
303
+ - [ ] Point-in-time recovery enabled
304
+ - [ ] Backup testing performed quarterly
305
+ - [ ] Disaster recovery plan documented
306
+ - [ ] RPO and RTO defined and tested
307
+
308
+ ## Pre-Deployment Cloud Security Checklist
309
+
310
+ Before ANY production cloud deployment:
311
+
312
+ - [ ] **IAM**: Root account not used, MFA enabled, least privilege policies
313
+ - [ ] **Secrets**: All secrets in cloud secrets manager with rotation
314
+ - [ ] **Network**: Security groups restricted, no public databases
315
+ - [ ] **Logging**: CloudWatch/logging enabled with retention
316
+ - [ ] **Monitoring**: Alerts configured for anomalies
317
+ - [ ] **CI/CD**: OIDC auth, secrets scanning, dependency audits
318
+ - [ ] **CDN/WAF**: Cloudflare WAF enabled with OWASP rules
319
+ - [ ] **Encryption**: Data encrypted at rest and in transit
320
+ - [ ] **Backups**: Automated backups with tested recovery
321
+ - [ ] **Compliance**: GDPR/HIPAA requirements met (if applicable)
322
+ - [ ] **Documentation**: Infrastructure documented, runbooks created
323
+ - [ ] **Incident Response**: Security incident plan in place
324
+
325
+ ## Common Cloud Security Misconfigurations
326
+
327
+ ### S3 Bucket Exposure
328
+
329
+ ```bash
330
+ # ❌ WRONG: Public bucket
331
+ aws s3api put-bucket-acl --bucket my-bucket --acl public-read
332
+
333
+ # ✅ CORRECT: Private bucket with specific access
334
+ aws s3api put-bucket-acl --bucket my-bucket --acl private
335
+ aws s3api put-bucket-policy --bucket my-bucket --policy file://policy.json
336
+ ```
337
+
338
+ ### RDS Public Access
339
+
340
+ ```terraform
341
+ # ❌ WRONG
342
+ resource "aws_db_instance" "bad" {
343
+ publicly_accessible = true # NEVER do this!
344
+ }
345
+
346
+ # ✅ CORRECT
347
+ resource "aws_db_instance" "good" {
348
+ publicly_accessible = false
349
+ vpc_security_group_ids = [aws_security_group.db.id]
350
+ }
351
+ ```
352
+
353
+ ## Resources
354
+
355
+ - [AWS Security Best Practices](https://aws.amazon.com/security/best-practices/)
356
+ - [CIS AWS Foundations Benchmark](https://www.cisecurity.org/benchmark/amazon_web_services)
357
+ - [Cloudflare Security Documentation](https://developers.cloudflare.com/security/)
358
+ - [OWASP Cloud Security](https://owasp.org/www-project-cloud-security/)
359
+ - [Terraform Security Best Practices](https://www.terraform.io/docs/cloud/guides/recommended-practices/)
360
+
361
+ **Remember**: Cloud misconfigurations are the leading cause of data breaches. A single exposed S3 bucket or overly permissive IAM policy can compromise your entire infrastructure. Always follow the principle of least privilege and defense in depth.
@@ -0,0 +1,304 @@
1
+ ---
2
+ name: springboot-patterns
3
+ description: Spring Boot architecture patterns, REST API design, layered services, data access, caching, async processing, and logging. Use for Java Spring Boot backend work.
4
+ ---
5
+
6
+ # Spring Boot Development Patterns
7
+
8
+ Spring Boot architecture and API patterns for scalable, production-grade services.
9
+
10
+ ## REST API Structure
11
+
12
+ ```java
13
+ @RestController
14
+ @RequestMapping("/api/markets")
15
+ @Validated
16
+ class MarketController {
17
+ private final MarketService marketService;
18
+
19
+ MarketController(MarketService marketService) {
20
+ this.marketService = marketService;
21
+ }
22
+
23
+ @GetMapping
24
+ ResponseEntity<Page<MarketResponse>> list(
25
+ @RequestParam(defaultValue = "0") int page,
26
+ @RequestParam(defaultValue = "20") int size) {
27
+ Page<Market> markets = marketService.list(PageRequest.of(page, size));
28
+ return ResponseEntity.ok(markets.map(MarketResponse::from));
29
+ }
30
+
31
+ @PostMapping
32
+ ResponseEntity<MarketResponse> create(@Valid @RequestBody CreateMarketRequest request) {
33
+ Market market = marketService.create(request);
34
+ return ResponseEntity.status(HttpStatus.CREATED).body(MarketResponse.from(market));
35
+ }
36
+ }
37
+ ```
38
+
39
+ ## Repository Pattern (Spring Data JPA)
40
+
41
+ ```java
42
+ public interface MarketRepository extends JpaRepository<MarketEntity, Long> {
43
+ @Query("select m from MarketEntity m where m.status = :status order by m.volume desc")
44
+ List<MarketEntity> findActive(@Param("status") MarketStatus status, Pageable pageable);
45
+ }
46
+ ```
47
+
48
+ ## Service Layer with Transactions
49
+
50
+ ```java
51
+ @Service
52
+ public class MarketService {
53
+ private final MarketRepository repo;
54
+
55
+ public MarketService(MarketRepository repo) {
56
+ this.repo = repo;
57
+ }
58
+
59
+ @Transactional
60
+ public Market create(CreateMarketRequest request) {
61
+ MarketEntity entity = MarketEntity.from(request);
62
+ MarketEntity saved = repo.save(entity);
63
+ return Market.from(saved);
64
+ }
65
+ }
66
+ ```
67
+
68
+ ## DTOs and Validation
69
+
70
+ ```java
71
+ public record CreateMarketRequest(
72
+ @NotBlank @Size(max = 200) String name,
73
+ @NotBlank @Size(max = 2000) String description,
74
+ @NotNull @FutureOrPresent Instant endDate,
75
+ @NotEmpty List<@NotBlank String> categories) {}
76
+
77
+ public record MarketResponse(Long id, String name, MarketStatus status) {
78
+ static MarketResponse from(Market market) {
79
+ return new MarketResponse(market.id(), market.name(), market.status());
80
+ }
81
+ }
82
+ ```
83
+
84
+ ## Exception Handling
85
+
86
+ ```java
87
+ @ControllerAdvice
88
+ class GlobalExceptionHandler {
89
+ @ExceptionHandler(MethodArgumentNotValidException.class)
90
+ ResponseEntity<ApiError> handleValidation(MethodArgumentNotValidException ex) {
91
+ String message = ex.getBindingResult().getFieldErrors().stream()
92
+ .map(e -> e.getField() + ": " + e.getDefaultMessage())
93
+ .collect(Collectors.joining(", "));
94
+ return ResponseEntity.badRequest().body(ApiError.validation(message));
95
+ }
96
+
97
+ @ExceptionHandler(AccessDeniedException.class)
98
+ ResponseEntity<ApiError> handleAccessDenied() {
99
+ return ResponseEntity.status(HttpStatus.FORBIDDEN).body(ApiError.of("Forbidden"));
100
+ }
101
+
102
+ @ExceptionHandler(Exception.class)
103
+ ResponseEntity<ApiError> handleGeneric(Exception ex) {
104
+ // Log unexpected errors with stack traces
105
+ return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
106
+ .body(ApiError.of("Internal server error"));
107
+ }
108
+ }
109
+ ```
110
+
111
+ ## Caching
112
+
113
+ Requires `@EnableCaching` on a configuration class.
114
+
115
+ ```java
116
+ @Service
117
+ public class MarketCacheService {
118
+ private final MarketRepository repo;
119
+
120
+ public MarketCacheService(MarketRepository repo) {
121
+ this.repo = repo;
122
+ }
123
+
124
+ @Cacheable(value = "market", key = "#id")
125
+ public Market getById(Long id) {
126
+ return repo.findById(id)
127
+ .map(Market::from)
128
+ .orElseThrow(() -> new EntityNotFoundException("Market not found"));
129
+ }
130
+
131
+ @CacheEvict(value = "market", key = "#id")
132
+ public void evict(Long id) {}
133
+ }
134
+ ```
135
+
136
+ ## Async Processing
137
+
138
+ Requires `@EnableAsync` on a configuration class.
139
+
140
+ ```java
141
+ @Service
142
+ public class NotificationService {
143
+ @Async
144
+ public CompletableFuture<Void> sendAsync(Notification notification) {
145
+ // send email/SMS
146
+ return CompletableFuture.completedFuture(null);
147
+ }
148
+ }
149
+ ```
150
+
151
+ ## Logging (SLF4J)
152
+
153
+ ```java
154
+ @Service
155
+ public class ReportService {
156
+ private static final Logger log = LoggerFactory.getLogger(ReportService.class);
157
+
158
+ public Report generate(Long marketId) {
159
+ log.info("generate_report marketId={}", marketId);
160
+ try {
161
+ // logic
162
+ } catch (Exception ex) {
163
+ log.error("generate_report_failed marketId={}", marketId, ex);
164
+ throw ex;
165
+ }
166
+ return new Report();
167
+ }
168
+ }
169
+ ```
170
+
171
+ ## Middleware / Filters
172
+
173
+ ```java
174
+ @Component
175
+ public class RequestLoggingFilter extends OncePerRequestFilter {
176
+ private static final Logger log = LoggerFactory.getLogger(RequestLoggingFilter.class);
177
+
178
+ @Override
179
+ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
180
+ FilterChain filterChain) throws ServletException, IOException {
181
+ long start = System.currentTimeMillis();
182
+ try {
183
+ filterChain.doFilter(request, response);
184
+ } finally {
185
+ long duration = System.currentTimeMillis() - start;
186
+ log.info("req method={} uri={} status={} durationMs={}",
187
+ request.getMethod(), request.getRequestURI(), response.getStatus(), duration);
188
+ }
189
+ }
190
+ }
191
+ ```
192
+
193
+ ## Pagination and Sorting
194
+
195
+ ```java
196
+ PageRequest page = PageRequest.of(pageNumber, pageSize, Sort.by("createdAt").descending());
197
+ Page<Market> results = marketService.list(page);
198
+ ```
199
+
200
+ ## Error-Resilient External Calls
201
+
202
+ ```java
203
+ public <T> T withRetry(Supplier<T> supplier, int maxRetries) {
204
+ int attempts = 0;
205
+ while (true) {
206
+ try {
207
+ return supplier.get();
208
+ } catch (Exception ex) {
209
+ attempts++;
210
+ if (attempts >= maxRetries) {
211
+ throw ex;
212
+ }
213
+ try {
214
+ Thread.sleep((long) Math.pow(2, attempts) * 100L);
215
+ } catch (InterruptedException ie) {
216
+ Thread.currentThread().interrupt();
217
+ throw ex;
218
+ }
219
+ }
220
+ }
221
+ }
222
+ ```
223
+
224
+ ## Rate Limiting (Filter + Bucket4j)
225
+
226
+ **Security Note**: The `X-Forwarded-For` header is untrusted by default because clients can spoof it.
227
+ Only use forwarded headers when:
228
+ 1. Your app is behind a trusted reverse proxy (nginx, AWS ALB, etc.)
229
+ 2. You have registered `ForwardedHeaderFilter` as a bean
230
+ 3. You have configured `server.forward-headers-strategy=NATIVE` or `FRAMEWORK` in application properties
231
+ 4. Your proxy is configured to overwrite (not append to) the `X-Forwarded-For` header
232
+
233
+ When `ForwardedHeaderFilter` is properly configured, `request.getRemoteAddr()` will automatically
234
+ return the correct client IP from the forwarded headers. Without this configuration, use
235
+ `request.getRemoteAddr()` directly—it returns the immediate connection IP, which is the only
236
+ trustworthy value.
237
+
238
+ ```java
239
+ @Component
240
+ public class RateLimitFilter extends OncePerRequestFilter {
241
+ private final Map<String, Bucket> buckets = new ConcurrentHashMap<>();
242
+
243
+ /*
244
+ * SECURITY: This filter uses request.getRemoteAddr() to identify clients for rate limiting.
245
+ *
246
+ * If your application is behind a reverse proxy (nginx, AWS ALB, etc.), you MUST configure
247
+ * Spring to handle forwarded headers properly for accurate client IP detection:
248
+ *
249
+ * 1. Set server.forward-headers-strategy=NATIVE (for cloud platforms) or FRAMEWORK in
250
+ * application.properties/yaml
251
+ * 2. If using FRAMEWORK strategy, register ForwardedHeaderFilter:
252
+ *
253
+ * @Bean
254
+ * ForwardedHeaderFilter forwardedHeaderFilter() {
255
+ * return new ForwardedHeaderFilter();
256
+ * }
257
+ *
258
+ * 3. Ensure your proxy overwrites (not appends) the X-Forwarded-For header to prevent spoofing
259
+ * 4. Configure server.tomcat.remoteip.trusted-proxies or equivalent for your container
260
+ *
261
+ * Without this configuration, request.getRemoteAddr() returns the proxy IP, not the client IP.
262
+ * Do NOT read X-Forwarded-For directly—it is trivially spoofable without trusted proxy handling.
263
+ */
264
+ @Override
265
+ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
266
+ FilterChain filterChain) throws ServletException, IOException {
267
+ // Use getRemoteAddr() which returns the correct client IP when ForwardedHeaderFilter
268
+ // is configured, or the direct connection IP otherwise. Never trust X-Forwarded-For
269
+ // headers directly without proper proxy configuration.
270
+ String clientIp = request.getRemoteAddr();
271
+
272
+ Bucket bucket = buckets.computeIfAbsent(clientIp,
273
+ k -> Bucket.builder()
274
+ .addLimit(Bandwidth.classic(100, Refill.greedy(100, Duration.ofMinutes(1))))
275
+ .build());
276
+
277
+ if (bucket.tryConsume(1)) {
278
+ filterChain.doFilter(request, response);
279
+ } else {
280
+ response.setStatus(HttpStatus.TOO_MANY_REQUESTS.value());
281
+ }
282
+ }
283
+ }
284
+ ```
285
+
286
+ ## Background Jobs
287
+
288
+ Use Spring’s `@Scheduled` or integrate with queues (e.g., Kafka, SQS, RabbitMQ). Keep handlers idempotent and observable.
289
+
290
+ ## Observability
291
+
292
+ - Structured logging (JSON) via Logback encoder
293
+ - Metrics: Micrometer + Prometheus/OTel
294
+ - Tracing: Micrometer Tracing with OpenTelemetry or Brave backend
295
+
296
+ ## Production Defaults
297
+
298
+ - Prefer constructor injection, avoid field injection
299
+ - Enable `spring.mvc.problemdetails.enabled=true` for RFC 7807 errors (Spring Boot 3+)
300
+ - Configure HikariCP pool sizes for workload, set timeouts
301
+ - Use `@Transactional(readOnly = true)` for queries
302
+ - Enforce null-safety via `@NonNull` and `Optional` where appropriate
303
+
304
+ **Remember**: Keep controllers thin, services focused, repositories simple, and errors handled centrally. Optimize for maintainability and testability.