JWT Authentication Example with GoFrame
This example demonstrates how to implement JWT (JSON Web Token) authentication in a GoFrame HTTP server using the github.com/golang-jwt/jwt package.
Features
- User login endpoint that generates JWT tokens
- Protected routes using JWT middleware
- Token validation and parsing
- Example of accessing protected resources
- Standard GoFrame project structure
Project Structure
jwt/
├── api/
│ └── v1/
│ └── auth.go # API interface definitions
├── internal/
│ ├── controller/
│ │ └── auth.go # Business logic implementation
│ └── middleware/
│ └── jwt.go # JWT middleware
└── main.go # Entry pointAPI Endpoints
Login:
POST /login{ "username": "admin", "password": "password" }Protected Resource:
GET /api/protected- Requires Bearer token in Authorization header
- Example:
Authorization: Bearer your-token-here
Running the Example
Start the server:
go run main.goThe server will start on port 8000
Testing the API
Login to get a token:
curl -X POST http://localhost:8000/login \ -H "Content-Type: application/json" \ -d '{"username":"admin","password":"password"}'Access protected endpoint:
curl http://localhost:8000/api/protected \ -H "Authorization: Bearer your-token-here"
Security Notes
- In production, replace the hardcoded secret key with a secure value
- Store user credentials in a database
- Implement proper password hashing
- Consider implementing refresh tokens
- Add rate limiting for login attempts
References
For more detailed information about JWT implementation, please refer to the third-party component documentation: