Spring Boot REST API for managing hotels with MySQL persistence, JWT authentication, and role-based access control.
This project demonstrates a compact Spring Boot REST application for hotel data management. It uses repository-backed JPA persistence, JWT-based stateless authentication, public user registration, and role-protected hotel endpoints to model a simple secured admin-and-user workflow.
- Spring Boot REST API setup
- Spring Data JPA repository pattern
- MySQL-backed persistence
- Spring Security with JWT authentication
- Stateless session policy with a custom JWT filter
- Method-level authorization with
@PreAuthorize - Public user registration and token-based login flow
- Custom
UserDetailsServicefor username-based lookup POSTendpoint for creating hotel recordsGETendpoint for retrieving a hotel by IDGETendpoint for listing all hotelsDELETEendpoint for removing a hotel by IDGETendpoint for listing registered users
- Java 17
- Spring Boot 2.7
- Spring Web
- Spring Data JPA
- Spring Security
- MySQL
- Maven
- Lombok
- JJWT
- JUnit 5
hotel/
├── CHANGELOG.md
├── README.md
├── pom.xml
├── mvnw
├── mvnw.cmd
└── src/
├── main/
│ ├── java/com/cn/hotel/
│ │ ├── config/
│ │ ├── controller/
│ │ ├── dto/
│ │ ├── jwt/
│ │ ├── model/
│ │ ├── repository/
│ │ ├── security/
│ │ ├── service/
│ │ └── HotelApplication.java
│ └── resources/
│ └── application.yml
└── test/
└── java/com/cn/hotel/
- Open a terminal in the project root.
- Update the MySQL connection values in
src/main/resources/application.ymlif needed. - Run
mvn test. - Run
mvn spring-boot:run. - Register a user with
POST /user/register. - Obtain a token with
POST /auth/login. - Use protected endpoints with
Authorization: Bearer <token>.
Available endpoints:
POST /auth/loginGET /userPOST /user/registerPOST /hotel/createGET /hotel/id/{id}GET /hotel/getAllDELETE /hotel/remove/id/{id}
Access notes:
/user/registerand/auth/loginare publicADMINusers can create, list, and delete hotels, and list usersNORMALusers can retrieve hotels by ID
Example request body for user registration:
{
"username": "john",
"password": "john123"
}Example request body for login:
{
"username": "john",
"password": "john123"
}Example request body for hotel creation:
{
"name": "Sea View Inn",
"rating": 8,
"city": "Goa"
}- Demonstrates the shift from basic auth to stateless JWT authentication in Spring Security
- Shows how a custom filter can authenticate requests from bearer tokens
- Uses JPA repositories to keep persistence simple while focusing on the security flow
- Keeps the API compact and readable for learning role-based endpoint protection
- Suggested repository description:
Spring Boot REST API for hotel record management with MySQL persistence, JWT authentication, and role-based access control. - Suggested topics:
java,java-17,spring-boot,spring-security,spring-data-jpa,mysql,rest-api,hotel-management,jwt,maven,learning-project,portfolio-project