Skip to content

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Sep 2, 2025

This PR implements a complete REST API for user management based on the detailed specification provided in the issue. The implementation includes a Spring Boot application with JWT-based authentication, comprehensive error handling, and full test coverage.

Key Features Implemented

API Endpoint: GET /api/users/{id}

  • Retrieves user information by ID
  • Requires JWT Bearer token authentication
  • Validates path parameters (ID must be Long >= 0)
  • Returns user data in JSON format with id, name, email, and role fields

Authentication & Authorization:

  • JWT-based security implementation using Spring Security
  • Custom JWT authentication filter
  • Proper token validation and user role extraction
  • Helper endpoint /auth/token for generating test tokens

Error Handling:

  • 400 Bad Request: Invalid ID format or negative values
  • 401 Unauthorized: Missing or invalid JWT token
  • 403 Forbidden: Insufficient permissions (handled by security config)
  • 404 Not Found: User does not exist

Database Integration:

  • H2 in-memory database for demonstration
  • JPA/Hibernate entity mapping
  • Sample data initialization with Japanese user data
  • Repository pattern for data access

Example Usage

# Generate JWT token
curl -X POST "http://localhost:8080/auth/token" \
  -H "Content-Type: application/json" \
  -d '{"username": "testuser", "role": "admin"}'

# Retrieve user data
curl -X GET "http://localhost:8080/api/users/1" \
  -H "Authorization: Bearer <JWT_TOKEN>"

# Response:
{
  "id": 1,
  "name": "田中 太郎",
  "email": "tanaka@example.com", 
  "role": "admin"
}

Testing

The implementation includes comprehensive test coverage:

  • Successful user retrieval with valid authentication
  • User not found scenarios (404 response)
  • Authentication failures (401 response)
  • Input validation for invalid ID formats (400 response)
  • Negative ID validation (400 response)
  • Invalid JWT token handling (401 response)

All tests pass and validate the exact behavior specified in the requirements.

Technology Stack

  • Spring Boot 3.2.0 - Main application framework
  • Spring Security - Authentication and authorization
  • Spring Data JPA - Database access layer
  • H2 Database - In-memory database for demo
  • JJWT - JWT token implementation
  • JUnit 5 - Testing framework

The implementation follows Spring Boot best practices with proper separation of concerns, comprehensive error handling, and production-ready security configuration.

Fixes #5.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: yus04 <49590084+yus04@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] 詳細設計書に基づいた REST API の実装 Implement REST API for user management with JWT authentication Sep 2, 2025
Copilot finished work on behalf of yus04 September 2, 2025 06:58
@Copilot Copilot AI requested a review from yus04 September 2, 2025 06:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

詳細設計書に基づいた REST API の実装
2 participants